0

I would like to know if it is possible to specify the python version in a virtual environment (created with virtualenv and virtualenvwrapper) after the environment is created.

If I create a virtualenv with the following command:

mkvirtualenv -p python3.8 venvname

I would like to know:

I can be sure that I am using python3.8 by just typing python when the virtual environment is activated and it will display something like the following:

Python 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 

The only way to specify the python version in the mkvirtualenv command is (as far as I know) to have python3.8 installed beforehand

Indeed, if for example I do

mkvirtualenv -p python3.6 venvname

I get the following error

RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.6'

since I don't have python3.6 installed.

My question is:

Can I change the python version installed in my virtualenv after having created it?

Thanks in advance to anyone who will try to help me.

2
  • 1
    Please ask one question per question only!
    – Klaus D.
    CommentedOct 5, 2021 at 16:45
  • You are right, I am sorry about that. I edited the question to ask only one question. Now it should be more clear.
    – desmond13
    CommentedOct 5, 2021 at 16:47

1 Answer 1

1

How can I be sure that I am using python3.6?

If you mean at runtime, python --version inside the venv should do it.

Is the only way to specify the python path?

With mkvirtualenv I believe so. With python -m venv the particular python executable you call sets up the venv. (docs)

Can I change the python version installed in my virtualenv after I create it?

AFAIK there is no built in way to do this. It is possible just to replace the executable, update the libraries, and go, but this sounds like a recipe for disaster, quite aside from dimension changes. Since you venv is presumably being used to develop a package, and since you keep your package dependencies somewhere---either a requirements.txt or a setup.py or something like that, make a new virtualenv with a new python and starting afresh should be straightforward.

Incidentally if you haven't already you might want to consider managing all this with poetry, which makes re-creating the venv even easier---just delete the venv dir and do poetry install again.

I'm willing to be proven wrong about updating python within a venv, though.

2
  • Thanks a lot! after posting my question I figured out the first two questions out and then I was thinking that the third one did not make sense since, as you said, might be a recipe for disaster! I think that the only way I have to use python3.6 is to have it installed on my local machine and then do mkvirtualenv -p python3.6 venvname
    – desmond13
    CommentedOct 5, 2021 at 16:27
  • I think that this answer is what I was looking for in the end. I will accept it in few days if no one will prove the author wrong.
    – desmond13
    CommentedOct 5, 2021 at 16:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.