Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和Windows中的PATH环境变量_Python_Windows_Path_Environment Variables - Fatal编程技术网

Python和Windows中的PATH环境变量

Python和Windows中的PATH环境变量,python,windows,path,environment-variables,Python,Windows,Path,Environment Variables,我遵循Pylern2教程,在其中一个步骤中编写了以下内容: 在PATH环境变量中应该有PYLERN2/脚本 所以我补充说: C:\Anaconda\Lib\site packages\pylern2-0.1dev-py2.7.egg\pylern2\scripts\ 指向路径变量 如果我想使用“execfile”函数执行上述文件夹中的某个脚本(例如“train.py”),是否需要再次向其添加路径? 我一直在口译员中尝试: >>> execfile('train.py') 但是

我遵循Pylern2教程,在其中一个步骤中编写了以下内容:

在PATH环境变量中应该有PYLERN2/脚本

所以我补充说:

C:\Anaconda\Lib\site packages\pylern2-0.1dev-py2.7.egg\pylern2\scripts\

指向路径变量

如果我想使用“execfile”函数执行上述文件夹中的某个脚本(例如“train.py”),是否需要再次向其添加路径? 我一直在口译员中尝试:

>>> execfile('train.py')
但是,我收到了错误消息:

IOError: [Errno 2] No such file or directory: 'train.py'
python不应该在path变量的目录路径中查找脚本吗

如果可以,请帮助我。

否,不搜索
路径。它只需要一个普通的文件名(可以是相对的,也可以是绝对的),并以与任何其他文件处理函数完全相同的方式打开它

除此之外,您很少想使用
execfile
。在这种情况下,您可能应该从cmd(“DOS-box”)提示符运行脚本,而不是从Python提示符运行脚本

如果您真的想使用Python提示符作为“shell”来代替cmd,您可以这样做,但您仍然希望能够通过
路径
找到程序,在单独的解释器实例中运行它们,等等。实现方法是使用。例如:

>>> from subprocess import check_call # you only have to do this once
>>> check_call(['train.py'])
当然,这比您需要从cmd进行的输入要多得多:

C:\> train.py

…但是您不能在cmd上运行任意Python语句,因此需要权衡。

cmd会搜索脚本路径吗?我有一个包含许多脚本的文件夹,包括train.py,我想从另一个目录调用train.py,但不必键入train.py.Yes的整个路径,
cmd.exe
搜索
path
以查找名为
train.py
的任何可执行文件。因此,如果您设置了
foo.py
文件被视为可执行文件(我相信在Python安装程序中默认选中该复选框),那么一切都会神奇地工作。