Python venv module not found - python

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:
Python\pyver\py391\python -m venv pyenv
running where in venv shows:
(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:
(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. Running print(sys.path) shows following:
(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. If I write python UpperLimb.py it works. How can I run it directily as an executable? Without python before?

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.

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.
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. In this case, it is python.
So, the first line in the UpperLimb.py file will be #!/usr/bin/python. This line will tell the program to use the python program at /usr/bin/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

Related

using #!/usr/bin/env python3 shebang with Windows

I'm trying to run a Python script from the command line as a command on Windows -- so no usage of "Python" or ".py". If my script is named "testing.py", I am attempting to make this name into a command and call "testing" from the command line.
Going through the docs it seems I need to use this shebang #!/usr/bin/env python as long as I have Python in my PATH.
https://docs.python.org/3/using/windows.html#shebang-lines
I also have the script folder in my PATH, so something like
"testing.py" is currently working from the command line.
According to the docs and this tutorial,
https://dbader.org/blog/how-to-make-command-line-commands-with-python
I should be able to evoke my Python script just by "testing" if I have the proper paths within PATH and the above shebang. However, I can't seem to get the script running withouth adding ".py".
The accepted answer by #AKX is incorrect for Windows 10 standard Python 3, certainly in the latest Windows 10 (1903) if not earlier.
(Note: I cannot speak to how this may or may not work under WSL.)
I have several versions of Python installed (2.7, 3.6, 3.7, and most recently Python 3.8b1). I've been using the #!/usr/bin/env shebang for years in my scripts for cross-platform compatibility (usually to distinguish Py2 vs Py3 scripts).
I've created a little script in a folder (C:\so_test\awtest.py):
#!/usr/bin/env python3.6
import sys
print(sys.version)
If I run this with awtest.py or just awtest I get 3.6.x reported (showing it's running with Python 3.6). If I change the shebang to refer to 3.7, I get 3.7.x reported. If I change the shebang to just #!/usr/bin/env python3 I get the latest version of Python installed (3.8).
Now, if I add that folder to my path (path=%PATH%;C:\so_test in the command window you're testing in, or in the main env vars (you will need to restart the command window if you do the latter though)), I can change to a different directory and run awtest or awtest.py and they still work and refer to the folder in the path. If I remove the script folder from the path these files are no longer found.
While I wouldn't necessarily expect this to work on Windows prior to 10 or Python 2.7, this functionality appears to be the way of things going forward.
No, Windows does not support shebang lines.
The documentation you've linked relates to the py launcher installed by Python, which can interpret various shebang lines to choose a Python version to run a script with.
setuptools is able to generate wrapper .exes for your Python scripts, but it gets a little involved and already assumes you have a package with a setup.py and so on.
Locally, if you really, really need this, you probably could add .py to the PATHEXT environment variable, so the Windows command line looks up .pys like it looks up .exes (and various others; the current modern default is .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC). However, this will naturally not scale for distributing apps, as all of your users would need to set that too.
My recommendation is to stick with just that boring old python testing.py, really.
You can use shebang in windows by setting the path of your interpreter as the first line in the file(you will see a marker on VSCode that says 'set as interpreter ' on that line).
Using windows 10,Python version 3.9 see example:
#!C:/Users/waithira/AppData/Local/Programs/Python/Python39/python.exe
print('hello world')
if you're not going to do this often. You can still add a batch file to your path testing.bat containing the command that you need to execute your code.
#echo off
python testing.py
It's a borring workaround but it works without needing to mention the extention since windows interpret batch files the same way it interpret executables.

How to deploy python programs in Anaconda environments on Windows?

I am trying to use Anaconda and conda environments to allow Python programs for data acquisition* etc. to run from the (Anaconda) command line on Windows. The set up will be that the Python programs are installed to a particular location (cloned from Github), within %PATH% or whichever environment variable is more appropriate.
From an Anaconda command prompt in another directory and a particular conda environment, I want (both myself and other users) to be able run either python test.py <args> or test.py <args> (either solution is acceptable) and have a system wide conda environment run its Python to execute the program. test.py can/will have an appropriate shebang set.
Right now the python test.py calls the correct Python within the active conda environment, but cannot find the test.py program as Python won't search the %PATH% or similar looking for the program. test.py does something (Windows does not complain that the executable can't be found, and I've been playing with the file associations to get this far), but doesn't appear to start Python - a simple print function or raise statement as the only entry in the file does nothing.
I've tried setting file associations in Windows, but this hasn't changed anything. I've copied the py.exe/pyw.exe across to the Anaconda environments, with no change.
Is this something that can be done within Anaconda, or am I going to have to fall back on installing base Python directly and trying to use the launcher mechanism there?
Note that I'm also intending to deploy these programs on Raspbian, so any solutions, including non-Anaconda ones, that will work cross platform there would be worth extra effort on my part.
*these programs have significant usage of library packages for accessing external USB/GPIB/serial/ethernet connected lab equipment and use matplotlib, scipy, etc., hence the desire for a cloneable conda environment as the base environment.
It turns out the correct answer to this is fairly simple, but is fairly hard to find explained well. This might be a little clearer than the other answers I found:
Install the standalone launcher from pylauncher and add #!/usr/bin/env python shebangs to your scripts.
This should register .py files to Python.File and will find your Anaconda Pythons in appropriate environments. If you don't have a non-Anaconda python, it will use the Anaconda base environment (these two facts were the key element I was missing from various other answers around this problem that I had looked at, including the documents on python.org).
If you have a Python from python.org installed, then a standalone command line shell will use that, defaulting to Python 2.x, then Python 3.x. With #!/usr/bin/env python shebang, then a regular command shell will try to use python.org pythons first, then the Anaconda base environment. An Anaconda prompt will use the active environment. #! /usr/bin/env python2 or python3 will try to use python.org pythons only and fail if they are not found.
Installing Python 2.7 from python.org installers (and letting the installer set the file associations) will break pylauncher, and reinstalling will not fix it. Instead, set Computer\HKEY_CLASSES_ROOT\Python.File\Shell\open\command default value to "C:\WINDOWS\py.exe" "%L" %* to revert back to the pylauncher set up (assuming you used the launchwin.* packages to install it).

Incorrect module path in Python

I recently had a problem with importing a python and therefore posted this question: Cant seem to import specific module in Python
Based on input I got I did some digging and saw the following:
If I open cygwin and input python --version, I get this
Marc#Marc ~
$ python --version
Python 2.7.12 :: Anaconda 4.2.0 (64-bit)
And if I open python using myCharm and input this:
import sys
print('\n'.join(sys.path))
I get:
C:\Users\Marc\Anaconda3\python.exe C:/Users/Marc/PycharmProjects/clustering/testing.py
C:\Users\Marc\PycharmProjects\clustering
C:\Users\Marc\PycharmProjects\clustering
C:\Users\Marc\Anaconda3\python35.zip
C:\Users\Marc\Anaconda3\DLLs
C:\Users\Marc\Anaconda3\lib
C:\Users\Marc\Anaconda3
C:\Users\Marc\Anaconda3\lib\site-packages
C:\Users\Marc\Anaconda3\lib\site-packages\Sphinx-1.4.6-py3.5.egg
C:\Users\Marc\Anaconda3\lib\site-packages\win32
C:\Users\Marc\Anaconda3\lib\site-packages\win32\lib
C:\Users\Marc\Anaconda3\lib\site-packages\Pythonwin
C:\Users\Marc\Anaconda3\lib\site-packages\setuptools-27.2.0-py3.5.egg
So assume this highlights the problem. Could anybody tell me what is wise to do here? Change the path in my terminal of change the path in python
If you set an envirnment variable called PYTHONPATH to a colon-separated (Windows: semicolon-separated) list of directories, each of them will be added to the interpreter's sys.path. This is the recommended way of making modules available.
So for example, let's suppose you had added Python modules in the directory C:\Documents and Settings\frits\private. Before you run your Python program you would execute the command
SET PYTHONPATH=C:\Documents and Settings\frits\private
No semicolon is required in this case because there's only one directory.
The when you run Python you will find that your sys.path has another entry corresponding to the C:\Documents and Settings\frits\private directory.
In the longer term, to avoid conflicts between the requirements of different projects (e.g. you want to run two programs that require a different version of the same third-party module) you should investigate the use of virtual environments.

How to run python files without using terminal or gui?

Hello gyus i have started python and i want to know how can i excecute python file without using terminal.Just like the most games using (exe) file extension but for py files.I have tried py2exe but it doesn't show anything on the screen. I tried to make excecutable the py file with no luck. Please tell me how to excecute the and if there is an option whithout using a specific program for that.
My system is : Windows 7 / Ubuntu 12.04
Here, have a look in my blog, It explains how ca you do it in Ubuntu.
http://insidepython.wordpress.com/2012/08/04/hello-world-or-how-or-say-ni/
but basically:
Add this line to the beginning of your script
#!<location of your python interpreter>
To find out where your python interpreter is installed:
$ sudo find / -name "python"
After executing the previous, you should get the location of your python interpreter, then you need to set the environment variable, in my case python executable is located in /usr/bin/python
$ export PATH="$PATH:/usr/bin/python"
Then you need to set the file attributes to executable, you can look more into file attributes in Unix/Linux here http://en.wikipedia.org/wiki/Chmod
$ chmod +x shrubbery.py
And finally to execute your application
$ ./shrubbery.py
For Windows, there's also a python script called GUI2Exe that I have used in the past to create distributable versions of python scripts as apps. It's available at
http://code.google.com/p/gui2exe/ and is, in my opinion, very simple to use. Tutorials and whatnot are easy to find on Google.
It uses py2exe (or any other Python compiler library) to put together the script, but it doesn't require any syntactically annoying setup.py file.

Python 3.2 doesn't work with python 2.7

I already have python 2.7 (installed using activepython). I'm trying to use python 3.2 just to learn more about it so i downloaded it from python.org
python 2.7 still works perfectly, but python 3.2 gives me this error when i try to open the ide.
and then I see the send error window, if i install python 3.2 using activepython i see the same error.
I'm using windows xp pro sp3 32 bit and i had the same error on sp2... How do I fix it?
EDIT #Zuljin
This is the first time that i use dependancy walker so could you give me a hand please
this is what i see
what does that mean? I already have these files...
I saw a lot of answers here. But, I think the file that you are trying to run is not correct
C:\Python32\Pythonw.exe
is not the one you use to open idle.
Open
C:\Python32\Lib\idlelib\idle.pyw
Python 2.x and 3.x can cohabitate perfectly in win xp and win 7, either 32 or 64 bits.
If you first installed the ActiveState Python distribution, be careful when installing python-3.2.2.msi.
When you arrive to the customize python 3.2.2 screen, (un)check 'register extensions' to make the feature unavailable (that is: do not register extensions).
I have installed this way both distributions in different computers without any problem so far.
Note: Check your environment variables. Only python 2.7 should be in the path (if you installed before the two distros you could have them both in the path. Remove python 3.2 path)
Edit:
From Ankit post I realized that in fact you were trying to open IDLE, maybe.
If you follow my installation instructions when you call idle.bat from the py3.2 idlelib folder you actually get idle for python 2.7 as this is what the call find in the windows path. To be able to open idle for py3.2 in the presence of python 2.7 as the registered python, I use a .bat file modified from that in the distribution:
idle_stay.bat
#echo off
rem Working IDLE bat for Windows - Custom bat for py3k as secundary python
C:\Python32\pythonw C:\Python32\Lib\idlelib\idle.pyw %1 %2 %3 %4 %5 %6 %7 %8 %9
The name idle_stay.bat is because I put this file in the folder where the official idle.bat is (C:\Python32\Lib\idlelib\idle_stay.bat). In this way it does not get overwritten each time I unisntall and reinstall a new version of python 3
It seems to me you have associated a *.py or *.pyw file extension type to some version of python.exe and/or pythonw.exe you aren't clear about. This is often, but not exclusively done from within Microsoft's "explore.exe" File Manager (I believe in Options).
I disagree that this is a reliable method:
C:\Python32\Lib\idlelib\idle.pyw
as this method will run whichever program is assigned to the "pyw" extension. That could be:
C:\Python32\pythonw.exe
or
C:\Python26\python.exe
or whatever might have happened to your file extension association as per OS or bad PATH environment variable setting. It is better to do something like this:
C:\Python32\pythonw.exe C:\Python32\Lib\idlelib\idle.pyw
where you are explicidly telling the OS to run the python executable from a specific path and selecting a specific python script in the file/folder hiearchy.
Another thing you can try to gather info is just:
python.exe -V
which will tell you the version number.
Another thing you can do:
Open a DOS command prompt and type:
set PATH=C:\Windows;C:\Windows\system32;C:\Python32
python.exe and note the version and whether is runs.
exit()
exit
Once working debug your PATH. You can use this Python script to gather intel.
If you turned off your WinXP findfile stuff (like I have) you can use this script:
inpath.py can help shed some light.
Run this python script like this:
Drop to a DOS prompt
cd to your folder with a working python.exe version.
and type python.exe inpath.py python*
where inpath is in that same folder for this test/debug.
inpath.py will search your path for all files associated as "runable" in your OS and that is in your "PATH" with the python*.* pattern.

Categories

Resources