简体   繁体   English

没有完整路径,为什么我安装的(在虚拟环境中的Windows上)python脚本无法运行?

[英]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.

相关问题 为什么我的Python脚本不起作用? - Why doesn't my Python script work? python workon 命令未激活 cmd windows 中的虚拟环境 7 - python workon command doesn't activate the virtual environment in cmd windows 7 使用 pybind11 嵌入 python。 虚拟环境不起作用 - Embedding python with pybind11. Virtual environment doesn't work 为什么我的 Python 命令在 Windows 10 CMD 中不起作用? - Why my Python command doesn't work in Windows 10 CMD? 尽管没有在该环境中安装软件包,为什么我的python程序仍在我的虚拟环境中正常运行? - Why is my python programme running correctly in my virtual environment despite not having the packages installed in that environment? vscode 似乎没有激活我的虚拟 python 环境 - vscode doesn't seem to activate my virtual python environment 当我在 Windows 上安装 Python 并且它没有配置路径时 - when I Installed Python on Windows and it doesn’t configure the path 我的脚本适用于 Windows,但不适用于 Linux-关于文件夹路径 - My script works on Windows but doesn't work on Linux-About Folder Path 通过SSH进入我的Vagrant虚拟机以运行python脚本…但是除非我在VM本身中,否则python脚本不起作用 - SSHed into my Vagrant virtual machine to run a python script…but the python script doesn't work unless I'm in the VM itself Flask在虚拟环境中不起作用 - Flask doesn'T work in Virtual Environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM