Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

pip - 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. I tried searching here for a solution, but maybe because I am new to python and environments I can't find the right answer.

I installed Python version 3.9.1 from the website in my folder (Python) and created an environment with it:

Pythonpyverpy391python -m venv pyenv

running where in venv shows:

(pyenv) D:CD_InhaltUpperLimb>where python
D:CD_InhaltUpperLimbPythonpyenvScriptspython.exe

running my script show an error:

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

But I know that I installed c3d:

(pyenv) D:CD_InhaltUpperLimb>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. Running print(sys.path) shows following:

(pyenv) D:CD_InhaltUpperLimb>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. If I write python UpperLimb.py it works. How can I run it directily as an executable? Without python before?

question from:https://stackoverflow.com/questions/65872033/python-venv-module-not-found

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a XY-problem. 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

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. 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.

If you want to share your code, you have three different options

(1) Creating and executable
Many people like to use tools like cx_Freeze, pyinstaller or Nuitka to create an executable from the python code. 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. Just do a simple search on SO with the toolname to see some example cases.

(2) Sharing code with portable python
There are portable python versions. For example WinPython, or the Embeddable Python from Python.org. You could pack them with your application code and simple .bat file that runs the python.exe myscript (using relative paths). If you have external dependencies (some packages), you could include their .whl files (download from PyPI), and make your .bat install them, too. I have done this successfully previously with very complex python programs, and handled my programs to end-users that have no clue about python. This takes a bit of effort and manual work, though.

(3) Using pynsist
An alternative approach would be to use pynsist for the job. It is used for distributing python apps, but it does not make a single "myapp.exe". What it does is basically automates the "Sharing code with portable 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. 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.


Appendix

Some explanations on your the problems you were facing.

A1) About launching a python script

When you launch a python script, you typically type 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. This list of folders is called the PATH environmental variable.

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

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.

A2) Running just myscript.py

On Windows, the standard Python Installer associates .py files with Python executable. I tested it and it seems to be the special Python Launcher for Windows (py.exe) that is used. So, when you just type

myscript.py

on command line, it will be run as <full-path-to-py>/py.exe myscript.py. Exactly the same thing happens if you just double-click the myscript.py in File Explorer. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...