Python - 在命令行模块运行期间添加 PYTHONPATH
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4580101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - add PYTHONPATH during command line module run
提问by orokusaki
I want to run:
我想跑:
python somescript.py somecommand
But, when I run this I need PYTHONPATHto include a certain directory. I can't just add it to my environment variables because the directory I want to add changes based on what project I'm running. Is there a way to alter PYTHONPATHwhile running a script? Note: I don't even have a PYTHONPATHvariable, so I don't need to worry about appending to it vs overriding it during running of this script.
但是,当我运行它时,我需要PYTHONPATH包含一个特定的目录。我不能只是将它添加到我的环境变量中,因为我想添加的目录会根据我正在运行的项目而改变。有没有办法PYTHONPATH在运行脚本时进行更改?注意:我什至没有PYTHONPATH变量,所以我不需要担心在运行此脚本期间附加到它与覆盖它。
采纳答案by ismail
For Mac/Linux;
适用于 Mac/Linux;
PYTHONPATH=/foo/bar/baz python somescript.py somecommand
For Windows, setup a wrapper pythonpath.bat;
对于 Windows,设置一个包装器pythonpath.bat;
@ECHO OFF
setlocal
set PYTHONPATH=%1
python %2 %3
endlocal
and call pythonpath.batscript file like;
并调用pythonpath.bat脚本文件;
pythonpath.bat /foo/bar/baz somescript.py somecommand
回答by Piotr Czapla
import sys
sys.path.append('your certain directory')
Basically sys.path is a list with all the search paths for python modules. It is initialized by the interpreter. The content of PYTHONPATH is automatically added to the end of that list.
基本上 sys.path 是一个包含所有 python 模块搜索路径的列表。它由解释器初始化。PYTHONPATH 的内容会自动添加到该列表的末尾。
回答by Ned Deily
If you are running the command from a POSIX-compliant shell, like bash, you can set the environment variable like this:
如果您从符合 POSIX 的 shell 运行命令,例如bash,您可以像这样设置环境变量:
PYTHONPATH="/path/to" python somescript.py somecommand
If it's all on one line, the PYTHONPATH environment value applies only to that one command.
如果全部在一行上,则 PYTHONPATH 环境值仅适用于该命令。
$ echo $PYTHONPATH
$ python -c 'import sys;print("/tmp/pydir" in sys.path)'
False
$ PYTHONPATH=/tmp/pydir python -c 'import sys;print("/tmp/pydir" in sys.path)'
True
$ echo $PYTHONPATH
回答by Hyman
This is for windows:
这是用于窗户的:
For example, I have a folder named "mygrapher" on my desktop. Inside, there's folders called "calculation" and "graphing" that contain Python files that my main file "grapherMain.py" needs. Also, "grapherMain.py" is stored in "graphing". To run everything without moving files, I can make a batch script. Let's call this batch file "rungraph.bat".
例如,我的桌面上有一个名为“mygrapher”的文件夹。在里面,有名为“calculation”和“graphing”的文件夹,其中包含我的主文件“grapherMain.py”需要的 Python 文件。此外,“grapherMain.py”存储在“graphing”中。要在不移动文件的情况下运行所有内容,我可以制作一个批处理脚本。我们称这个批处理文件为“rungraph.bat”。
@ECHO OFF
setlocal
set PYTHONPATH=%cd%\grapher;%cd%\calculation
python %cd%\grapher\grapherMain.py
endlocal
This script is located in "mygrapher". To run things, I would get into my command prompt, then do:
该脚本位于“mygrapher”中。要运行东西,我会进入我的命令提示符,然后执行:
>cd Desktop\mygrapher (this navigates into the "mygrapher" folder)
>rungraph.bat (this executes the batch file)
回答by Marco Rossi
You may try this to execute a function inside your script
你可以试试这个在你的脚本中执行一个函数
python -c "import sys; sys.path.append('/your/script/path'); import yourscript; yourscript.yourfunction()"

