Home > front end >  how to create a venv with a different python version
how to create a venv with a different python version

Time:12-20

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

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related