Windows / Multiple python versions / How to get Pip or Venv command? - python

I am trying to get multiple versions of Python in my Computer (current version is 3.7)
I have downloaded a 3.6 embed version : python-3.6.8rc1-embed-amd64
When I run python from this new directory, it works and I can run sys module inside Python Prompt.
But in the case of Venv or Pip (outside Python Prompt)
ModuleNotFoundError: No module named 'venv'
What would be the correct way ?
thanks

Actually no need to use embed version. Installing differents python versions is ok
E.G I have three differents versions in this folder C:\Users\FB088ABL\AppData\Local\Programs\Python
When I need to source a specific version for a project, I am using :
[absolute path to the version I want]python -m venv .env
Then when you type python in this environment, it supersedes your path environment
NB: with the Python Launcher for Windows there is no need to specify the full path.

Related

CMD has two versions of python

So I just installed the latest version of python, and I also changed the old 3.5 python version to 3.10 in my environment variables. There is no python 3.5 in the PATH inside environment variables, but when I run py --version in my CMD then it says version 3.5, but when I type python --version then it says version 3.10.2, is there a way to manually choose which version should be called on py or python?
You would need a python virtual environment to control it. (venv)
Just one thing, you can create a venv only with the version or python which is installed.
Also you can use conda to manage different version of python.
Here is a tutorial to create a venv or use conda on windows :
https://medium.com/co-learning-lounge/create-virtual-environment-python-windows-2021-d947c3a3ca78#a904
#The Mungax I am not sure but I can show you something that might help you
what is the Difference between “python” vs “py”
PYTHON
The command python refers to the Python executable of the default
Python installation. Technically, the path of this version is stored
inside the PATH environment variable where your OS will search for the
executable when processing any command.
PY
The command py refers to the Python launcher, a utility that’s
automatically installed into C:\Windows\ for any Python installation
on Windows. All files in the Windows folder are accessible without
needing to modify the PATH environment variable. Thus, the Python
launcher automatically delegates the work to the latest Python version
installed in your environment. However, you can also specify the used
installation by means of a flag argument such as in py -3.6 to specify
Python version 3.6.

How to use blender through VSCode with python for integrating 2D widgets with 3D models

I am new to Blender community and has created some add-ons to model things through code..
However, I am failing to execute the same code through my IDE, IDLE and VS Code with all my paths set for Python 3.7.0 installed inside python folder of Blender 2.80 directory in C drive (cant use other version for VS Code as Blender extension in VS Code only supports python 3.7.0 and the same Python version is compatible with only Blender 2.80)
However I have installed UPBGE in another drive with latest versions of Python(3.9.1) and Blender(2.93 Alpha)..
There, I manually installed latest version of Python like the one I had installed
So, I have my python interpreters at two following locations
C:\Program Files\Blender Foundation\Blender\2.80\python\python.exe
D:\Softwares\UPBGE\2.93\python\python.exe
Problems are as follows:
When I check python version through command "py --version" it
displays 3.9.1 even when interpreter selected as 3.7.0 in VS Code
selected interpreter ambiguity
Whenever I install any pypi module through cmd, it installs the
module to older version, i.e., 3.7.0 directory in C drive even when python 3.9.1 interpreter selected
Blender stores its built in python interpreter inside bin directory inside python folder
Whenever I try to run any script importing bpy stuff, it says
bpy module is not present
cannot find the module named bpy-types
Installing python libraries into blender's python environment.
<[Blender Python.exe path]> -m pip install <[Package Name]>
For example, in your case for UPBGE:
D:\Softwares\UPBGE\2.93\python\python.exe -m pip install <[Package Name]>
If the command fails you might need to add "pip" to you python env available here:
https://bootstrap.pypa.io/get-pip.py
For running a script that uses bpy you also need to use Blenders python.
<[Blender Python.exe path]> <[Script Path]>

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

How can I run python version 3.6.2 instead of 3.6.1

this is my first time here, and I'm fairly new to python, so please let me know if you need more information. Thanks in advance.
I am running python 3 on Windows 7
I discovered my problem after I used pip install numpy. This works just fine. Then, when I try to use import numpy in the python shell I receive the ModuleNotFoundError: No module named 'numpy'. That's when I noticed that my default version of python was 3.6.1, despite having updated to 3.6.2 at some point. I still have both .exe setup files and when I run them it shows that I only have 3.6.2 installed. However, when I type python --version in the command line i get Python 3.6.1, even though Python36-32 is what i have in my path.
I think my question is how can I make sure I'm running the newer version of python as my default, or if need be, how can I get rid of the older version?
When you type python in cmd, it searches python command inside the directories in the environment variable named Path. Actually Path includes both python directory and python scripts directory. For example, in my computer, Path includes:
C:\Users\user\AppData\Local\Programs\Python\Python36
C:\Users\user\AppData\Local\Programs\Python\Python36\Scripts
pip is in the python scripts directory. Your Path can be wrong. You should check it. This link can help you. You should have one python directory and one python script directory in Path, just the version you need.
Also, you can call pip as a module:
python -m pip install numpy
This will install the package to the version which is in the Path, Python 3.6.2 in your situation.
If none of these works, I recommend you to uninstall(delete) Python 3.6.1, and try to use pip again. If pip doesn't work(or disappeares), you can read this or use get-pip.py to install pip to your computer again. Maybe, you can delete all python versions, and install the version you need, and of course, you should be careful about Path again.
EDIT:
I am not sure about your problem. Some informations are needed for a certain solution.
You can find the source of an executable(python or py in your situtation) with where command. Here is an example from my local:
where python
Output:
C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe
That means C:\Users\user\AppData\Local\Programs\Python\Python36-32\ directory is in my Path and when type python, cmd runs python.exe.
So, you can find out what are py and python exactly.

How install and use another version python(python 2.7) on linux with the default python version is python 2.6

There is a default python version, namely python 2.6, on the GPU server with Linux OS. Now I want to install a new python version on the server from its source, namely python 2.7. I should not change the default python version since I am not the administrator and some reason. So what should I do?
You can install your new version of Python. It should be accessible with the python27 command (which may be a symbolic link).
Then you will just have to check that the python symbolic link still points to python26.
Doing this, python will keep on execute Python 2.6 while python27 will execute Python 2.7
You can use virtualenv, to execute your programm in an environment with python 2.7.
Install virtualenv and virtualenvwrapper (for comfotable use.)
mkvirtualenv -p <your-python-version> would then start a virtual environment where the desired python version is the default.
To build on Tryph's answer, you can install that new version to your home directory, then in a directory specified within your PATH (like in .bash_profile), you can point to that directory and within there create a sym-link that points to the new python.
For instance, if you have a bin folder in your home directory that is specified in the path
ln -s /bin/python ~/bin/python

Categories