How do I get an installed script to ignore PYTHONP

2019-07-09 00:15发布

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.

标签: python pip
2条回答
Evening l夕情丶
2楼-- · 2019-07-09 00:58

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(':')]]
查看更多
该账号已被封号
3楼-- · 2019-07-09 01:16

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.

查看更多
登录 后发表回答