简体   繁体   中英

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. 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. The script is successfully installed into my virtualenv Scripts folder, but after activating my virtualenv, if I call

(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 ). 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?

(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%.)

> python my_script.py

will only work if the current directory is in your PATH environment variable. If it is not, you must specify the directory it is un, either completely, or relative to your working directory.

so you could do:

> python .\myscript.py

or update your PATH and add "." for current directory. http://en.wikipedia.org/wiki/PATH_(variable)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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