简体   繁体   English

Windows中的Python和PATH环境变量

[英]Python and the PATH environment variable in Windows

I am following the Pylearn2 tutorial and in one of the steps the following is written: 我正在关注Pylearn2教程,并在其中一个步骤中编写了以下内容:

You should have pylearn2/scripts in your PATH enviroment variable. 您应该在PATH环境变量中有pylearn2 / scripts。

So i added: 所以我补充说:

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

to the PATH variable. 到PATH变量。

If i want to execute one of the scripts that is in the mentioned folder (for example 'train.py') by using the 'execfile' function, do i need to add the path to it again? 如果我想使用“ execfile”功能执行提到的文件夹中的脚本之一(例如“ train.py”),是否需要再次向其添加路径? I have been trying this in the interpreter: 我一直在解释器中尝试此操作:

>>> execfile('train.py')

However, i get the error message: 但是,我收到错误消息:

IOError: [Errno 2] No such file or directory: 'train.py'

Shouldn't python look for the script in the directory path in the PATH variable? python是否不应该在PATH变量的目录路径中查找脚本?

Please help me if you can. 如果可以,请你帮助我。

No, execfile does not search the PATH . 不, execfile不会搜索PATH It just takes a normal filename (which can be relative or absolute) and opens it exactly the same as any other file-handling function. 它只需要一个普通文件名(可以是相对文件名,也可以是绝对文件名),然后将其与其他任何文件处理函数完全相同地打开。

On top of that, you very rarely want to use execfile . 最重要的是,您很少要使用execfile In this particular case, what you should probably be doing is running the script from the cmd ("DOS box") prompt, not the Python prompt. 在这种情况下,您可能应该从cmd(“ DOS框”)提示符而不是Python提示符运行脚本。

If you really want to use the Python prompt as your "shell" in place of cmd, you can do that, but you still want to be able to find programs via the PATH , run them in a separate interpreter instance, etc. The way to do that is with subprocess . 如果您确实希望将Python提示符用作cmd的“外壳”,则可以这样做,但是您仍然希望能够通过PATH查找程序,并在单独的解释器实例中运行它们,等等。要做到这一点是与subprocess For example: 例如:

>>> from subprocess import check_call # you only have to do this once
>>> check_call(['train.py'])

That's a lot more typing than you need to do from cmd, of course: 当然,这比您需要从cmd进行的键入要多得多:

C:\> train.py

… but then you can't run arbitrary Python statements at cmd, so there's a tradeoff. …但是您不能在cmd上运行任意的Python语句,因此需要进行权衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM