how to create a venv with a different python version - python-3.x

I have different venvs in my machine in which I have python 3.10.
Now for a specific project, I realised that python 3.10 is not suitable as some libraries are still not compatible. Therefore when creating a new venv for a new project, I would like to downgrade python, say to 3.8, only for this specific venv.
How can I do that?
What should I type onto the terminal to do this?
PS: I use VS and its terminal to create venv

The recommended way by python.org
The recommended way of managing virtual environments since Python 3.5 is with the venv module within the Python Standard Library itself.
Source: https://docs.python.org/3/library/venv.html#creating-virtual-environments
That is not the same as virtualenv, which is a third party package, outside the Python Standard Library.
Source: https://pypi.org/project/virtualenv/
Dangerous to downgrade (and to upgrade)
Depending on if your system itself uses Python, it could be dangerous for system stability to change the system Python version. Your system might need exactly that version of Python. That is true with Ubuntu.
Install another version of Python
Safer than downgrading or upgrading is installing other versions of Python on the same system.
For example, in Ubuntu 20.04, to install Python 3.9:
# Update package lists
me#mydevice:~$ sudo apt update
# Add the deadsnakes repository
me#mydevice:~$ sudo add-apt-repository ppa:deadsnakes/ppa
# Install Python 3.9
me#mydevice:~$ sudo apt install python3.9
Install the venv package and create a venv virtual environment
# Install the venv package for Python 3.9
me#mydevice:~$ sudo apt install python3.9-venv
# Make a folder for venv virtual environments
me#mydevice:~$ mkdir ~/.venvs
# Create a new venv virtual environment with Python 3.9 in it
me#mydevice:~$ python3.9 -m venv ~/.venvs/my-venv-name
# Activate the new venv
me#mydevice:~$ source ~/.venvs/my-venv-name/bin/activate
(my-venv-name) me#mydevice:~$
Check versions within the venv virtual environment
# Check the Python version inside the venv:
(my-venv-name) me#mydevice:~$ python -V
Python 3.9.9
# Check the Pip version inside the venv:
(my-venv-name) me#mydevice:~$ pip3 --version
pip 21.2.4 from /home/me/.venvs/my-venv-name/lib/python3.9/site-packages/pip (python 3.9)
Deactivate the venv virtual environment
(my-venv-name) me#mydevice:~$ deactivate
me#mydevice:~$
Check versions outside any virtual environments
# Check Python version:
me#mydevice:~$ python -V
Python 3.8.10
# Check the Pip version:
me#mydevice:~$ pip3 --version
pip 20.0.2 from /home/me/.venvs/my-venv-name/lib/python3.8/site-packages/pip (python 3.8)
Install more Python versions
To install more Python versions, just change the version number from 3.9 to which ever version you choose, that is available from the deadsnakes repository.

I believe the best way to work with different python versions in isolation is pyenv, managing virtual environments can be done with pyenv-virtualenv.
I think this article from Real Python does a good job at explaining how to manage different python versions as well as different virtual environments.
For posterity, with the tools mentioned above you can do the following (once the proper python versions are installed)
pyenv virtualenv <python_version> <environment_name>
# Then activate it
pyenv local <environment_name>
Now that you've created a virtual environment in the folder, it should be picked up any time you enter the folder. VSCode should also pick it up, as per its documentation.
P.S: The reason I think it's a good approach it's because it allows you to manage python versions as well as environments with a single tool. Each version is installed only once, in one place, which should help because it reduces complexity.

Simple and recent
Supposed that you have a different version of Python installed in your system. To check use the following command to check:
> py --list
-3.10-64 *
-3.7-64
And you want to create a new virtual environment for python 3.7 on a 'test_env' directory. Run the following command:
> py -3.7 -m venv test_env
Then activate the test_env by running the following command on Windows PowerShell:
> .\test_env\Scripts\Activate.ps1
Or Linux:
$ source test_env/bin/activate
Check:
python --version
Python 3.7.0

You can have multiple python versions installed at the same time and you can create virtual environments with the needed version. Make sure you have installed the python version you need and then specify its location when you create the virtual environment:
virtualenv -p <path-to-new-python-installation> <new-venv-name>
Example:
virtualenv -p C:\Users\ssharma\AppData\Local\Programs\Python\Python38\python.exe venv38
This will create a virtual environment called venv38 with Python 3.8.

you can do it by using "virtualenv" library. It can be installed with command pip install virtualenv
then followed by command
virtualenv "name_of_your_environment" #no quotes
and then use the following code to activate your venv
"name_of_your_environment"\Scripts\activate #NOTE YOU MUST BE at your directory where you created your env.
its for VS CODE but I prefer installing conda and then creating env on conda prompt using conda which later you can access to vs code to and its easy to activate that env from anywhere just type conda activate 'name_of_your_env' on vs terminal

You can use anaconda:
conda create -n mypython3.8 python=3.8
For merely the sake of enough porridge in your stew.

Related

How to create python 2.7 virtual environment using python 3.7

I have Python 3.7 && I would like to create a python 2.7 virtual environment to run some code that only works on python 2.7
How do I create this python 2.7 virtual environment?
python3 -m venv ?
When creating virtual environment, a pyvenv.cfg is created that has home key which tells where the python executable is, which was used to create the virtual environment. If your global python installation is version 3.8.6, and you run
python3 -m venv something
you will create virtual environment in folder something, that has pyvenv.cfg that points to the python executable of the Python 3.8.6 installation. There is no easy way* to make it point to the Python 2.7 executable.
What can you do?
virtualenv as venv replacement
The venv module was introduced in Python 3.3, so you cannot use it to create virtual environments with python 2.7. You could use the virtualenv package which is a superset of venv. First, install it with python 2.7**:
python -m pip install virtualenv
If Python 2.7 is not on your PATH as python, use full path to the python executable in place of python. Then, you can create virtual environments that have Python 2.7 with
virtualenv something
or
virtualenv --python=python2.7 something
* It is not supported by the venv module out of the box, at least.
** You can actually install it with any Python version, but then you will have to specify --python=/opt/python-2.7/bin/python or --python=python2.7 when running virtualenv. By default, it uses the python executable that was used to install it.
venv doesn’t allow creating virtual environments with other versions of Python than the one currently installed. You would have to use the traditional virtualenv package which allows creating virtual environments for different versions of python by providing the path to the binary like this:
virtualenv --python=/usr/bin/python2.7 /path/to/virtualenv/
where the path /usr/bin/python2.7 refers to the path of the binary for Python 2.7 on your system.
Install python2.7
Add universe repo
sudo apt-add-repository universe
sudo apt update
Install python2.7
sudo apt install python2-minimal
Create virtualenv with python2.7
mkvirtualenv -p $(which python2) something

Should i remove Python 3.6 while installing Python 3.8 or newer?

I tried to install a certain python module that required python 3.6 minimum to work properly so I checked my version using python --version which gave me the output of Python 2.7.17 and then used python3 --version giving me Python 3.6.9. Now, I know for a fact that i have Python 3.8 installed because I ran apt install python3.8 just before checking the version.
If someone wants to know what my system is running; I am currently running Elementary OS 5.1.7 Hera.
EDIT:
(IDK what term to use, I want to say I am done going through answers, and I liked none.)
After a while of whacking my brain, I decided not to uninstall the 3.6 version as It may have version specific modules which if removed may cause other installed programs to break.
Since I just use Linux for my college-work, It wont matter if more than one versions are installed anyway.
Sorry for any mistakes I may have made, I was never good at this kind of things.
Use conda to install and manage different versions of Python (or lots of other software, for that matter). Install different Python versions in separate conda environments. For example:
Find what python versions are available:
conda search python
Output (as of the time of writing, in the future I expect that the latest Python versions will be higher):
Loading channels: done
# Name Version Build Channel
python 1.0.1 0 conda-forge
python 1.2 0 conda-forge
...
python 3.9.0 h88f2d9e_1 pkgs/main
python 3.9.0 ha017127_4_cpython conda-forge
Install the Python versions you need in separate environments. Here, install Python 3.9.0 in environment named python39:
conda create --name python39 python=3.9.0
Switching environments is easy:
conda activate python39
# python is now 3.9.0
conda deactivate
# back to the default python
or (deprecated now):
source activate python39
source deactivate
SEE ALSO:
conda docs: Managing environments
This question is more appropriate for Unix & Linux.
Python installations (more generally, versioned installations of software) co-exist on linux using version numbers. You can likely run Python 3.8 using the python3.8 command (or else, locate where you installed it and run from there / add the location to the PATH environment variable to run directly).
Likewise, for each python version you can install its own package manager (e.g. install pip3.8 by python3.8 -m pip install pip) and use it to install packages for that python version. (As different projects require different sets of packages, the general practice is to create a "virtual environment", i.e. a separate copy of the needed version of python, pip and their package installation folders for each project, and install the required packages there - for more information see e.g. this excellent answer).
Regarding the command python3 (usually /usr/bin/python3) is just a symbolic link, you can replace it with the version you like (as long as it remains compatible with what the system expects - i.e. python3 of version no less than your built-in python3/python3-minimal, otherwise you will probably break something), e.g. assuming which python3 gives you /usr/bin/python3, you can
sudo rm /usr/bin/python3 #remove existing link
sudo ln /usr/bin/python3.8 /usr/bin/python3 # create a new link to the version of your choice
(although a better alternative could be instead aliasing it alias python3='/usr/bin/python3.8' and adding this to ~/.bashrc).
Whatever you do, do not uninstall python3-minimal as it is - at least, on Ubuntu - required by the system and uninstalling or changing it will likely break the system.

Installing python3.6 on virtualenv

I have python3.7 installed on my windows 10 laptop
But i need python3.6 for a specific project
Can i install it in virtualenv which will override python3.7 in that environment?
I don't know whether this may be an appropriate solution for you. But this is what I generally follow. Just install Anaconda in your system and create an environment according to your needs. For your case create an environment for Python 3.6.
conda create --name py36 python=3.6
//This lines will create an environment named py36
You then install libraries according to your needs in that environment. You work in that environment without interfering with the libraries of the other environment. To use anaconda kindly follow Anaconda cheatsheet. You will get everything that you need.

Setting Up a Python Virtual Environment with Hydrogen in Atom

I'm in the middle of switching from VS Code to Atom and I'm trying to set up a virtual environment for my python project.
It was pretty easy to do in VS Code, I'd run the following script and it would automagically start using the new env (with all the required packages) when I'd run the script:
python3 -m venv my_env
source my_env/bin/activate
pip3 install -r requirements.txt
Now I'm trying to set up Hydrogen to work the same way. When I run lines of code inline with Hydrogen, I want them to be run in a virtual environment that has the imported modules I need from a requirements.txt file.
I was able to install the python3 kernel with the following commands:
python3 -m venv my_environment_name # create a virtual environment
source my_environment_name/bin/activate # activate the virtual environment
python -m pip install ipykernel # install the python kernel (ipykernel) into the virtual environment
python -m ipykernel install
And Atom is able to see it:
Screenshot
However, I'm still puzzled as how to install my dependencies into the kernel. And if I do install my dependencies there, I don't want my next python projects to have all those modules in there. I'd love to have the fresh-slate that virtual environments promise.
Any help here would be appreciated. Has anyone had experience setting up a virtual environment that can be used by the Hydrogen package?
Ok, after some more experimentation, I was able to connect to a kernel that I had installed my requirements.txt into.
Here are the steps I took:
python3 -m venv env
source env/bin/activate
# make sure requirements.txt has ipykernel in it
pip3 install -r requirements.txt
python3 -m ipykernel install --user --name=env
Then in Atom, press cmd-shift-p and find Hydrogen: Update Kernels.
Or manually Packages->Hydrogen->Update Kernels
After, I was able to use the kernel by doing cmd-shift-p again and selecting Hydrogen: Start Local Kernel and selecting env.
When I would run import statements via Hydrogen (selecting them and pressing cmd-enter), they would now know what to import! Horray!
Windows Version.
This is the windows version of the excellent answer by Tim Estes. Without the use of requirements.txt in case some folks such as myself would like to install ipykernel separately. (Thanks so much Tim, it saved me a ton of time)
python -m venv env
env/bin/activate
pip install ipykernel
python -m ipykernel install --user --name=env
Similar to Tim's answer, go ahead and update kernels at Atom.
Packages->Hydrogen->Update Kernels

conda_env package is not available upon downgrading Python version to 3.6

I'm trying to create a Docker image with Conda and Python 3.6 installed. I have installed Anaconda 3 with the default Python 3.7. Then I used the following command to downgrade to Python 3.6:
conda install -y python=3.6
This results in Python 3.6 successfully installing, but when I subsequently execute conda --version, it gives me the error
'conda' module not found
I found that under python3.6/site-packages folder there are no Conda-related packages and instead all these packages are found under python3.7/site-packages.
It used to work before. Is there a way to get conda packaged under python3.6 folder?
Srikanth,
You should always try creating a new environment when you are working with some old version of any library.
conda create -n myenv python=3.4
If you want to install specific packages in this environment, you can use
conda install -n myenv tensorflow
Refer to this documentation for more information:
docs.conda.io

Resources