I want to run:
我想跑:
python somescript.py somecommand
But, when I run this I need PYTHONPATH
to 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 PYTHONPATH
while running a script? Note: I don't even have a PYTHONPATH
variable, so I don't need to worry about appending to it vs overriding it during running of this script.
但是,当我运行它时,我需要PYTHONPATH来包含某个目录。我不能只将它添加到我的环境变量中,因为我想添加的目录会根据我正在运行的项目而更改。有没有办法在运行脚本时改变PYTHONPATH?注意:我甚至没有PYTHONPATH变量,因此在运行此脚本期间,我不需要担心附加到它而不是覆盖它。
5 个解决方案
#1
103
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.bat
script file like;
并调用pythonpath.bat脚本文件;
pythonpath.bat /foo/bar/baz somescript.py somecommand
#2
40
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的内容会自动添加到该列表的末尾。
#3
6
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
#4
0
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()"
#5
-1
This is for windows:
这是针对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”的文件夹。在里面,有一个名为“计算”和“图形”的文件夹,其中包含我的主文件“grapherMain.py”所需的Python文件。此外,“grapherMain.py”存储在“图形”中。要在不移动文件的情况下运行所有内容,我可以制作批处理脚本我们称这个批处理文件为“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)
#1
103
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.bat
script file like;
并调用pythonpath.bat脚本文件;
pythonpath.bat /foo/bar/baz somescript.py somecommand
#2
40
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的内容会自动添加到该列表的末尾。
#3
6
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
#4
0
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()"
#5
-1
This is for windows:
这是针对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”的文件夹。在里面,有一个名为“计算”和“图形”的文件夹,其中包含我的主文件“grapherMain.py”所需的Python文件。此外,“grapherMain.py”存储在“图形”中。要在不移动文件的情况下运行所有内容,我可以制作批处理脚本我们称这个批处理文件为“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)