[英]Why doesn't my installed (on Windows in a virtual environment) python script work without a full path?
I wrote a small python package and then installed it in my Python 2.7 virtual environment using pip. 我编写了一个小型python程序包,然后使用pip将其安装在我的Python 2.7虚拟环境中。 My package includes a command-line utility script, let's call it
my_script.py
that uses functionality from my package via an import mypkg
call. 我的程序包包含一个命令行实用程序脚本,我们称其为
my_script.py
,它通过import mypkg
调用使用了程序包中的功能。 The script is successfully installed into my virtualenv Scripts folder, but after activating my virtualenv, if I call 该脚本已成功安装到我的virtualenv脚本文件夹中,但是在激活我的virtualenv之后,如果我调用
(my_env) > python my_script.py --help
I get the error: 我收到错误:
python: can't open file 'my_script.py': [Errno 2] No such file or directory
and if I call 如果我打电话
(my_env) > my_script.py --help
I get the error 我得到错误
Traceback (most recent call last): File "C:\\PyEnvs\\my_env\\Scripts\\my_script.py", line 1, in <module> import mypkg ImportError: No module named mypkg
(even though mypkg
clearly shows up in the pip list
). (即使
mypkg
清楚地显示在pip list
)。 Only if I call 只有我打电话
python C:\\PyEnvs\\my_env\\Scripts\\my_script.py --help
does the script work as expected. 脚本是否按预期工作。 Am I doing something wrong, or is this a deficiency of virtualenv on Windows?
我是在做错什么,还是Windows上的virtualenv不足?
(Further note that C:\\PyEnvs\\my_env\\Scripts
is in my %PATH%, and I get the same behavior even if C:\\PyEnvs\\my_env\\Scripts
is also in my %PYTHONPATH%.) (还要注意,
C:\\PyEnvs\\my_env\\Scripts
在我的%PATH%中,即使C:\\PyEnvs\\my_env\\Scripts
也在我的%PYTHONPATH%中,我也会得到相同的行为。)
> python my_script.py
will only work if the current directory is in your PATH environment variable. 仅当当前目录在您的PATH环境变量中时才起作用。 If it is not, you must specify the directory it is un, either completely, or relative to your working directory.
如果不是,则必须完全或相对于工作目录将其指定为un。
so you could do: 所以你可以这样做:
> python .\myscript.py
or update your PATH and add "." 或更新您的PATH并添加“。” for current directory.
用于当前目录。 http://en.wikipedia.org/wiki/PATH_(variable)
http://en.wikipedia.org/wiki/PATH_(变量)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.