Where is pyvenv script in Python 3 on Windows installed? - python

After reading the following statement from PEP 405
A pyvenv installed script is also provided to make this more
convenient:
pyvenv /path/to/new/virtual/environment
I tried to create a new virtual environment and failed miserably;
C:\>python --version
Python 3.3.1
C:\>pyvenv myvenv
'pyvenv' is not recognized as an internal or external command,
operable program or batch file.
Apparently pyvenv script is not installed into Scripts folder which is being usually added to the PATH environment variable making it possible to easily run such scripts on the command line.
Is PEP 405 wrong, was it not properly implemented in Python 3.3 or am I missing something?

It looks like pyvenv script is placed in Tools\Scripts subfolder inside Python installation folder (sys.prefix). It seems like copying it to Scripts subfolder is a good idea as it allows to simply type pyvenv from the command line (assuming Scripts folder is already on the PATH). As there's no exe wrapper for this script one has to make sure
.py extension is added to PATHEXT environment variable so that
Windows finds Python script placed on the PATH when typing script's
name at the command prompt.
.py extension is associated either with Python executable or with Python launcher (py.exe) which is available starting from Python 3.3
Alternatively one can just type python -m venv instead of pyvenv and save himself all of the hassle...
Related Python bug 17480 - pyvenv should be installed someplace more obvious on Windows

Use python -m venv someenvname instead.
Moreover, there is no strong reason to add python folder to PATH if you use system-wide python.exe only for creating virtual environments.

Related

No module recognised after creating new virtualenv

So, i have python 3.11 installed, i wanted to run a code with flask module, and it was running using original python, but not in vscode, so i followed a suggestion from stack exchange and used command python -m venv env.
This created a new folder called env, and after that i got a prompt that new virtual environment has been created, do you want to use it for the workspace folder, i said yes, and still nothing worked. And now even other modules are not being recognized either by vscode or the original python.exe.
I am a novice in programming and have no idea what to do, please help.
Thanks
Once a venv is created, you must activate it like so: C:\User\venv_folder\Scripts\ activate
If this doesn't work try using C:\User\venv_folder\Scripts\ activate.ps1 or activate.bat
You'll know if it works once you have the (venv_name) before your command line in the terminal
python -m venv env
According to the docs. Running this command creates the target directory (creating any parent directories that don’t exist already) and places a pyvenv.cfg file in it with a home key pointing to the Python installation from which the command was run (a common name for the target directory is .venv). It also creates a bin (or Scripts on Windows) subdirectory containing a copy/symlink of the Python binary/binaries (as appropriate for the platform or arguments used at environment creation time). It also creates an (initially empty) lib/pythonX.Y/site-packages subdirectory (on Windows, this is Lib\site-packages). If an existing directory is specified, it will be re-used.
I think what you need more is to create a conda environment. Use the following command to create it (take python 3.10.4 as an example), and then you can manually select it in python interpreter.
conda create -n env-01 python=3.10.4
You can also read vscode docs for more details.

python curses module for windows can't install

i try to install curses module on windows and it ends with this error
picture:https://i.stack.imgur.com/fbKCJ.png
You can't install windows curses with 3.10 yet. It is supported on 3.9. I don't know when it will be supported for 3.10, so your best option for now is just to install 3.9.
You can make a virtual environment with python 3.9 for any projects that need to use curses. A virtual environment makes a copy of your python interpreter and installs it into a directory of your choice. You use this by "activating" the virtual environment, and as long as you're inside that environment, anything you install will be contained by this copied installation.
This allows you to run different versions of python, and it also allows you to install packages that you don't want cluttering up your main installation. It's a good idea to use this for all projects that are going to need packages outside of the standard library (anything that you pip install).
To make a virtual environment with your default interpreter, type:
python -m venv <envname> where <envname> is whatever you want the environments directory to be called. This is usually env.
So python -m venv env would install a fresh copy of your python 3.10 to a folder called env inside your current directory.
You activate this by typing .\<envname>\scripts\activate
You'll then get a prompt that has (<envname>) in front of it, which will let you know you're in that environment.
You can leave that environment by typing deactivate.
In order to use a different version, you have to run venv with the interpreter you want to use on the project. So if you wanted to use python 3.9, it would be something like
"C:\Program Files\Python39\python.exe" -m venv env depending on where you installed python 3.9. The directory I used is usually the default directory when installing for all users.
To more easily work with other versions on windows, I make batch files for each one and put them in a utils folder that's on my system path. (I'll explain how to add a folder to the system path at the bottom if you don't know.)
So make a file called python39.bat, and into that, put "C:\Program Files\Python39\python.exe" %*. (Or wherever the installation is. %* just expands other arguments so you can use it exactly like you would the other executable.
That way you can create a new python 3.9 virtual environment with python39 -m venv env (along with any other arguments you want) instead of typing out the full path.
You can also use --prompt to change the name displayed by your virtual environment instead of changing the name of the folder. This is useful for making it shorter or just keeping things straight when you're using a bunch of environments for different projects. (Using the same folder name allows you have something that doesn't change into your standard ignore files.)
So anyway, here's an example of the full process after you install python 3.9.
Go to your project directory or wherever you'd like to install the environment.
type "C:\Program Files\Python39\python.exe" -m venv env (optional) --prompt somealternatenametodisplay (or python39 -m venv env if you made a .bat file).
type .\env\scripts\activate
You should now have (env) or the alternate name at the beginning of your prompt
type pip install windows-curses
And everything should work now. Just remember to activate your environment whenever you want to use this.
(To put a folder on the path)
Let's make a new folder called myutils as an example at C:\myutils
and put python39.bat in that folder.
Right click My Computer
Select properties
On the right side under Related settings click on Advanced system settings.
At the bottom of the Advanced tab, click on Environment Variables
(You can also get to Environment Variables much faster by opening the start menu and starting to type environment, which should give you Edit the system environment variables).
Under System variables, select Path, and then click Edit...
Type C:\myutils, hit Enter, and press OK.
Now, open a new terminal, and you'll be able to access any programs you put in that folder.
In your path variable in Environment Variables, you can also change the default python interpreter. The default will be whichever one is at the top (which will probably be 3.9 now that you've just installed it).
To change it back to 3.10, select C:\Program Files\Python310\Scripts\ and click Move Up until it's above the Python39 entries, then do the same with C:\Program Files\Python310\.
GO to this URL and find your python version and download wheel file from it
As from my image it is python 3.7 with 64 bit so i will download this file
windows_curses-2.2.0-cp37-cp37m-win_amd64.whl and give whole path where it is downloaded
and give full path for installation like
pip install filepath\windows_curses-2.2.0-cp37-cp37m-win_amd64.whl in cmd or powershell

How exactly works Python venv folder? My project use the dependencies into this venv folder executing the project using a different Python version?

I am an absolute beginner with Python (I came from Java) and I have the following doubt about the venv folder in my project.
So basically I have this project structure:
As you can see it contains the venv folder. Reading on the Python official documentation:
https://docs.python.org/3/library/venv.html
I can read:
The venv module provides support for creating lightweight “virtual
environments” with their own site directories, optionally isolated
from system site directories. Each virtual environment has its own
Python binary (which matches the version of the binary that was used
to create this environment) and can have its own independent set of
installed Python packages in its site directories.
So from what I can understand it means that, using venv, every project have its own venv folder containing:
The bin folder: it contains the Python interpreter binary (in this specific case Python 3.7).
The lib folder: containing the dependencies that I have installed using pip3 (infact I installed scapy and scapy_http via pip3)
This because my PyCharm IDE is set to use Python 3.7
If this reasoning is correct (I am asbolutly not sure) it means that when I run something like:
python3 packet_sniffer.py
the packet_sniffer.py script (contained in the previous project) will be run using the Python 3 bynary and the dependencies that are into my venv folder. Is it correct?
If my reasoning is correct I have the following doubt:
What happen when I run it using Python 2 by
python packet_sniffer.py
I suppose that it is not using anymore the Python 3 version embedded into the venv folder of my project. And what dependencies of scapy am I using? where does it come from?
As you can see running with python instead python3 I am using Python 2 version:
root#kali:~# python --version
Python 2.7.17
root#kali:~# python3 --version
Python 3.7.5
If you create a virtual environment, and you then activate it, the python interpreter installed in this virtual environment will be run if you call any of these: python foo.py / python3 foo.py. There will be NO differences between calling python foo.py or python3 foo.py (if you installed python3 of course).
The lib folder will contain the associated dependencies that you install through pip in this virtual environment and when you use python interpreter through this virtual environment you will use the aforementioned dependencies
So from what I can understand it means that, using venv, every project have its own venv folder containing:
The bin folder: it contains the Python interpreter binary (in this specific case Python 3.7).
The lib folder: containing the dependencies that I have installed using pip3 (infact I installed scapy and scapy_http via pip3)
Using venv every project has its own virtual environment folder that you can name anything you want, containing yes the bin folder and the lib folder.
python virtual environments
The venv folder is a suggested standard for installing python virtual environments.
To use the virtual environment you must first activate it.
Note: Activate your python virtual environment prior to installing any packages required by your application.
from your command line execute:
source venv/bin/activate
python packet_sniffer.py
Note: You do not need to specify the specific version of python as it defaults to the version used to create the virtual environment.
PyCharm can be configured to utilize python virtual environments
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
If you have the virtual environment activated then everything you say about executing python3 is correct. However, all other commands are still available and will run from wherever they are located assuming they can be found by your shell. So your python command is probably running the system-level installed version of python.
If you want to know exactly where that python interpreter is running from:
python
>>> import sys
>>> print(sys.executable)
C:\Python27\python.exe

python27 and python3 are not not recognized as an internal or external command, operable program or batch file [duplicate]

I am using Python 3.5.2 version on Windows 7 and tried using python3 app.py. I am getting this error message:
'python3' is not recognized as an internal or external command,
operable program or batch file.
Is there any specific cause about why the python3 command is not working?
I also verified that the PATH is added to environment variables.
There is no python3.exe file, that is why it fails.
Try:
py
instead.
py is just a launcher for python.exe. If you have more than one python versions installed on your machine (2.x, 3.x) you can specify what version of python to launch by
py -2 or
py -3
You can also try this:
Go to the path where Python is installed in your system. For me it was something like C:\Users\\Local Settings\Application Data\Programs\Python\Python37
In this folder, you'll find a python executable. Just create a duplicate and rename it to python3. Works every time.
Python3.exe is not defined in windows
Specify the path for required version of python when you need to used it by creating virtual environment for your project
Python 3
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
Python2
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
then activate the environment using
.\environment\Scripts\activate.ps1
Yes, I think for Windows users you need to change all the python3 calls to python to solve your original error. This change will run the Python version set in your current environment. If you need to keep this call as it is (aka python3) because you are working in cross-platform or for any other reason, then a work around is to create a soft link. To create it, go to the folder that contains the Python executable and create the link. For example, this worked in my case in Windows 10 using mklink:
cd C:\Python3
mklink python3.exe python.exe
Use a (soft) symbolic link in Linux:
cd /usr/bin/python3
ln -s python.exe python3.exe
In my case I have a git hook on commit, specified by admin. So it was not very convenient for me to change the script (with python3 calls).
And the simplest workaround was just to copy python.exe to python3.exe.
Now I could launch both python and python3.
If python2 is not installed on your computer, you can try with just python instead of python3
For Python 27
virtualenv -p C:\Python27\python.exe django_concurrent_env
For Pyton36
virtualenv -p C:\Python36\python.exe django_concurrent_env
Enter the command to start up the server in that directory:
py -3.7 -m http.server
I had a related issue after installing windows 11, where python3 in cmd would open the windows store. I was able to sort it out between this post and this other one. In short, I reinstalled python and made sure to add it to PATH. Then, in settings, Apps > Apps & Features > App Execution aliases. Here, all I had to do was make sure that every single python .exe (including idle and pip) were turned off EXCEPT FOR the python3.exe alias. Now it works like a charm.
FWIW:
The root of this issue is not with you or with python. Apparently, Microsoft wanted to make installing python easier for young kiddos getting interested in coding, so they automatically add an executable to PATH. For those of us that already have this executable, it can cause these issues.
Found out instead press the play button the top right and it should work in visual studios:
Do not disable according to first answer
Saying python3 in the command will not work by default.
After figuring out the problem with the modules (Solution): https://youtu.be/paRXeLurjE4
Summary:
To import python modules in case of problem to import modules:
Hover over python in search:
Click open in folder
Hover over and right click
click properties
copy everything in path before \python.exe
close those windows
For cmd (administrator):
cd --path that was copied--
then python -m pip install --upgrade pip
cd Scripts
pip install "Name of Package" such as pip install --module (package) --
Im on win10 and have 3.7, 3.8 and 3.10 installed.
For me "python" launches version 3.10 and does not accept commands (like -3.7), "py" launches newest version but does accept commands, and "python3" does nothing.
Uninstalled 3.10 and "python" now does nothing, and "py" launches 3.8.
I am unable to add a comment, but the mlink option presented in this answer above https://stackoverflow.com/a/55229666/8441472 by #Stanislav preserves cross-platform shebangs at the top of scripts (#!/usr/bin/env python3) and launches the right python.
(Even if you install python from python.org, Windows will direct you to the app marketplace nowadays if you type python3 on the command line. If you type python on the same cli it will launch the python.org version repl. It leads to scripts that generate no output, but more likely silently failed completely. I don't know ho common this is but have experienced it on a couple of different devices)
If you have this at the top of your script to ensure you launch python3 and don't feel like editing everything you own, it is not a bad approach at all... lol.

Virtualenv won't create environment; runtime error

I'm trying to create a simple virtualenv environment; python and virtualenv are both installed. When I go in to my directory in which I am trying to make this environment, I keep getting a Windows Script Host popup box:
Script: *My path to virtualenv.js*
Line: 1
Char: 1
Error: Object expected
Code: 800A138F
Source: Microsoft JScript runtime error
I went in to my file to see where the problem may have lied and assumed it was just a module I hadn't installed yet, though all of them are in the node_modules in my package. I'm not sure where else this issue is coming from?
It looks to me as if you have a virtualenv.js JavaScript file on your path, and that JS files are associated with Windows Scripting Host on your machine. (Perhaps you have the node-virtualenv project installed, which puts a JavaScript file with the name virtualenv.js in your path?)
To create a typical Python virtual environment, you want to invoke the (Python) virtualenv.py script. Assuming you've installed virtual environment with:
pip install virtualenv
Then you can use:
python -m virtualenv ENV
to invoke the Python interpreter directly, and have it load the appropriate virtualenv.py script file on your path.

Categories