Provide temporary PYTHONPATH on the commandline?

為{幸葍}努か 提交于 2020-08-24 15:27:21

问题


I'm thinking of something like

python3 my_script.py --pythonpath /path/to/some/necessary/modules

Is there something like this? I know (I think) that Pycharm temporarily modifies PYTHONPATH when you use it to execute scripts; how does Pycharm do it?

Reasons I want to do this (you don't really need to read the following)

The reason I want to do this is that I have some code that usually needs to run on my own machine (which is fine because I use Pycharm to run it) but sometimes needs to run on a remote server (on the commandline), and it doesn't work because the remote server doesn't have the PYTHONPATHs that Pycharm automatically temporarily adds. I don't want to export PYTHONPATH=[...] because it's a big hassle to change it often (and suppose it really does need to change often).


回答1:


You can specify the python path in an environment variable like so:

PYTHONPATH=/path/to/some/necessary/modules python3 my_script.py



回答2:


Not sure how much effort you want to put into this temporary python path thing but you could always use a python virtual environment for running scripts or whatever you need.



来源:https://stackoverflow.com/questions/39022629/provide-temporary-pythonpath-on-the-commandline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!