简体   繁体   English

Python venv 模块未找到

[英]Python venv module not found

I am trying to create a portable venv on my stick to copy paste it to someone, so he doens't need to install python on the pc to run my python script.我正在尝试在我的棒上创建一个便携式 venv 以将其复制粘贴给某人,因此他不需要在 PC 上安装 python 即可运行我的 python 脚本。 I tried searching here for a solution, but maybe because I am new to python and environments I can't find the right answer.我尝试在这里搜索解决方案,但可能是因为我是 python 和环境的新手,我找不到正确的答案。

I installed Python version 3.9.1 from the website in my folder (Python) and created an environment with it:我从我的文件夹(Python)中的网站安装了 Python 版本 3.9.1,并用它创建了一个环境:

Python\pyver\py391\python -m venv pyenv

running where in venv shows:运行where in venv 显示:

(pyenv) D:\CD_Inhalt\UpperLimb>where python
D:\CD_Inhalt\UpperLimb\Python\pyenv\Scripts\python.exe

running my script show an error:运行我的脚本显示错误:

(pyenv) D:\CD_Inhalt\UpperLimb>UpperLimb.py
Traceback (most recent call last):
File "D:\CD_Inhalt\UpperLimb\UpperLimb.py", line 11, in <module>
import c3d
ModuleNotFoundError: No module named 'c3d'

But I know that I installed c3d:但我知道我安装了 c3d:

(pyenv) D:\CD_Inhalt\UpperLimb>pip list
Package         Version
--------------- ---------
appdirs         1.4.4
c3d             0.3.0
certifi         2020.12.5
chardet         4.0.0
...

This Computer doens't have Python installed.这台电脑没有安装 Python。 Running print(sys.path) shows following:运行 print(sys.path) 显示如下:

(pyenv) D:\CD_Inhalt\UpperLimb>python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', 'D:\\CD_Inhalt\\UpperLimb\\Python\\pyver\\py391\\python39.zip', 'D:\\CD_Inhalt\\UpperLimb        
\\Python\\pyver\\py391\\DLLs', 'D:\\CD_Inhalt\\UpperLimb\\Python\\pyver\\py391\\lib',     
'D:\\CD_Inhalt\\UpperLimb\\Python\\pyver\\py391', 'D:\\CD_Inhalt\\UpperLimb\\Python\\pyenv', 
'D:\\CD_Inhalt\\UpperLimb\\Python\\pyenv\\lib\\site-packages']

So where excatly did I made an error?那么我到底在哪里犯了错误? I am confused.我很困惑。 Appreciate any help:) Greetings感谢任何帮助:) 问候

Edit 1: like hoefling mentioned it.编辑 1:就像 hoefling 提到的那样。 If I write python UpperLimb.py it works.如果我写python UpperLimb.py它可以工作。 How can I run it directily as an executable?如何直接将其作为可执行文件运行? Without python before?之前没有python

This is a XY-problem .这是一个XY 问题 Let me first tell how you can solve your problem, and then explain in the Appendix what was the reason for the problems you were facing.让我先告诉您如何解决您的问题,然后在附录中解释您遇到问题的原因。

1) Solving the problem: Creating portable application 1)解决问题:创建可移植应用程序

Your original problem is in the first paragraph: You want to share your python script/app with someone, and not to force the user to install python.您最初的问题在第一段:您想与某人共享您的 python 脚本/应用程序,而不是强迫用户安装 python。 Virtual environments are not solution to your problem .虚拟环境不能解决您的问题 Although, they are part of the solution since any application development should be done inside a virtual environment to make your projects isolated.虽然,它们是解决方案的一部分,因为任何应用程序开发都应该在虚拟环境中完成,以使您的项目隔离。 Virtual environments are not portable;虚拟环境不可移植; they are dependent on the python installation on the machine they were created on .它们依赖于创建它们的机器上的 python 安装

If you want to share your code, you have three different options如果你想分享你的代码,你有三种不同的选择

(1) Creating and executable (1) 创建和执行
Many people like to use tools like cx_Freeze , pyinstaller or Nuitka to create an executable from the python code.许多人喜欢使用cx_FreezepyinstallerNuitka等工具从 python 代码创建可执行文件。 There are gotchas, though.不过,也有一些陷阱。 These tools work quite well with simple apps, but can be a pain with larger and more complex applications that use lot of static files, extension modules (.dlls), or depend on the __file__ attribute of the module to access a file on filesystem.这些工具适用于简单的应用程序,但对于使用大量 static 文件、扩展模块 (.dlls) 或依赖模块的__file__属性来访问文件系统上的文件的更大和更复杂的应用程序可能会很痛苦。 Just do a simple search on SO with the toolname to see some example cases.只需使用工具名在 SO 上进行简单搜索即可查看一些示例案例。

(2) Sharing code with portable python (2) 与便携式 python 共享代码
There are portable python versions.有便携式 python 版本。 For example WinPython , or the Embeddable Python from Python.org.例如WinPython或来自 Python.org 的可嵌入Python。 You could pack them with your application code and simple .bat file that runs the python.exe myscript (using relative paths).您可以将它们与您的应用程序代码和运行python.exe myscript (使用相对路径)的简单.bat文件一起打包。 If you have external dependencies (some packages), you could include their .whl files (download from PyPI), and make your .bat install them, too.如果您有外部依赖项(某些包),您可以包含它们的.whl文件(从 PyPI 下载),并让您的.bat也安装它们。 I have done this successfully previously with very complex python programs, and handled my programs to end-users that have no clue about python.我之前使用非常复杂的 python 程序成功地完成了这项工作,并将我的程序处理给对 python 毫无头绪的最终用户。 This takes a bit of effort and manual work, though.不过,这需要一些努力和手工工作。

(3) Using pynsist (3) 使用 pynsist
An alternative approach would be to use pynsist for the job.另一种方法是使用pynsist来完成这项工作。 It is used for distributing python apps, but it does not make a single "myapp.exe".它用于分发 python 应用程序,但它不会生成单个“myapp.exe”。 What it does is basically automates the "Sharing code with portable python".它所做的基本上是自动化“使用可移植 python 共享代码”。 It also creates an installer that you can just handle to your friend(s) to use, and the installed program will be visible in Windows Start Menu and Installed Apps.它还创建了一个安装程序,您可以只处理给您的朋友使用,并且安装的程序将在 Windows 开始菜单和已安装的应用程序中可见。 It even creates a shortcut, and your users will never have to install python themselves, or even know that your app is created with Python.它甚至创建了一个快捷方式,您的用户将永远不必自己安装 python,甚至知道您的应用程序是使用 Python 创建的。


Appendix附录

Some explanations on your the problems you were facing.关于你所面临的问题的一些解释。

A1) About launching a python script A1) 关于启动 python 脚本

When you launch a python script, you typically type python myscript.py .当您启动 python 脚本时,您通常键入python myscript.py What happens is that your OS (Windows) will search through a list of folders for python.exe and when it finds it, it uses that python.exe to run your script.发生的情况是您的操作系统 (Windows) 将在文件夹列表中搜索python.exe ,当它找到它时,它会使用python.exe来运行您的脚本。 This list of folders is called the PATH environmental variable.此文件夹列表称为 PATH 环境变量。

What the activate script ( Activate.ps1 for powershell and activate.bat for cmd) does is it adds the folder of the virtual environment that contains the python.exe to the top of the path, so when you run激活脚本(powershell 的Activate.ps1和 cmd 的activate.bat )的作用是将包含python.exe的虚拟环境的文件夹添加到路径的顶部,所以当你运行

python myscript.py

The python.exe in your virtual environment (here pyenv/Scripts/python.exe ) will be used, which guarantees also that the packages installed in your virtual environment are found.将使用虚拟环境中的python.exe (此处pyenv/Scripts/python.exe ),这也保证了安装在虚拟环境中的包被找到。

A2) Running just myscript.py A2) 只运行myscript.py

On Windows, the standard Python Installer associates.py files with Python executable .在 Windows 上,标准 Python 安装程序关联.py 文件与 Python 可执行文件 I tested it and it seems to be the special Python Launcher for Windows ( py.exe ) that is used.我对其进行了测试,它似乎是使用的 Windows ( py.exe ) 的特殊Python Launcher So, when you just type所以,当你输入

myscript.py

on command line, it will be run as <full-path-to-py>/py.exe myscript.py .在命令行上,它将作为<full-path-to-py>/py.exe myscript.py运行。 Exactly the same thing happens if you just double-click the myscript.py in File Explorer.如果您只是在文件资源管理器中双击myscript.py ,则会发生完全相同的事情。 You can change the default program by right clicking a.py-file -> "Open With" -> "Choose application" -> Check "Always use this application", if you wish, but this does not help you with your problem.如果您愿意,您可以通过右键单击 a.py 文件 ->“打开方式”->“选择应用程序”->选中“始终使用此应用程序”来更改默认程序,但这对您的问题没有帮助。

If you want to run the script directly without using python before, then you have to tell the code that which program it should use to run this code.如果您想直接运行脚本而不使用之前的 python,那么您必须告诉代码它应该使用哪个程序来运行此代码。

Let's say if you have python installed at /usr/bin/python then you have to write in the python file which program to use.假设您在 /usr/bin/python 上安装了 python,那么您必须在 python 文件中写入要使用的程序。 In this case, it is python.在这种情况下,它是 python。

So, the first line in the UpperLimb.py file will be #!/usr/bin/python .因此,UpperLimb.py 文件中的第一行将是#!/usr/bin/python This line will tell the program to use the python program at /usr/bin/python.这一行将告诉程序使用 /usr/bin/python 中的 python 程序。

After this, you need to make this script executable.在此之后,您需要使该脚本可执行。 You can use the following command to make this file executable.您可以使用以下命令使该文件可执行。

$ chmod +x UpperLimb.py

Now, you can run this file as follows using the command ./UpperLimb.py现在,您可以使用命令./UpperLimb.py按如下方式运行此文件

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

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