I know I can get Python to ignore PYTHONPATH
if I start it with the -E
flag.
But how do I get a script installed by pip
to have this flag?
I tried both the "scripts" and the "console_scripts" section of the code and pip
strips the -E if I put it on the #! line.
I generally recommend against this sort of trickery. The target system puts paths in place for a reason. If you want to break out of a virtualenv you should simply recommend not installing in a virtualenv in your documentation.
However you can remove the entry from sys.path
.
import sys
import os
sys.path = [p for p in sys.path if p not in [os.path.abspath(x) for x in os.environ['PYTHONPATH'].split(':')]]
The easiest way right now seems to be to put write a scripts that restarts Python if the flag is not included:
#!/bin/env python
import sys
if not sys.flags.ignore_environment:
import os
os.execv(sys.executable, [sys.executable, '-E'] + sys.argv)
# Run your actual script here
Then in setup.py, put this:
setup(..., scripts=['myscript'], ...)
Don't use entry_points/console_scripts. This should not be used for public modules, just for internal scripts.