Python Path conflict
I have found an… odd collision of problems. I have the following packages installed:
- Twisted
- Google protocol buffers (
google.protobuf
)
- The google package from PyPI (not affiliated)
Twisted provides an application system which lets you define .tac
files and then have twistd
handle a bunch of boiler plate (logging, background, reactor selection, etc).
-
twistd
is installed as a Python file to my PATH (ie /usr/bin
)
- google installs
google.py
to my PATH (/usr/bin
)
- When you start
twistd
, Python adds its directory (/usr/bin
) to its path
- When my application imports
google.protobuf
, Python first imports google
- Python finds
/usr/bin/google.py
and imports it as google
- Python can’t find
protobuf
in google
.
So what went wrong?
-
twistd
was installed in such a way to allow unnecessary items into Python’s path
- google installed a
.py
file into the PATH
- I wasn’t using a virtual environment.
I’m now considering the opinion that Python scripts should never be installed directly. Instead, they should be installed as __main__
modules and a shell script of the form exec python2/3 -m mypackage
is installed to your PATH.