Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

How do I use a different python3.x version of python inside virtual environment

I have read this post but I think it is about using python2 or python3 inside virtual environment. My problem is bit different, I want to different version of python 3 itself inside virtual environment.

I am using Ubuntu 18.04. I have three different versions of python 3 in my system and all of them seem to work.

They can be started by mentioning specific python version. eg: python3.6, python3.7, python3.8.

But simply typing python3 will load python 3.7 because it is Anaconda's python version.

sankethbk7777@Lenovo-ideapad:/$ which python3
/home/sankethbk7777/anaconda3/bin/python3

However I want to create a virtual environment with python 3.8 as python version inside it. (I mean inside my virtual env if I type python3 - python3.8 should boot up).

I tried using this command.

sankethbk7777@Lenovo-ideapad:/$ sudo python3.6 -m venv myproject
sankethbk7777@Lenovo-ideapad:/$ source myproject/bin/activate
(myproject) sankethbk7777@Lenovo-ideapad:/$ python3
Python 3.6.9 (default, Oct  8 2020, 12:12:24) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Above we can see that it boots python 3.6 when I type python3.

But when I tried the same for python3.8 this error shows up.

sankethbk7777@Lenovo-ideapad:/$ sudo python3.8 -m venv myproject3
Error: Command '['/myproject3/bin/python3.8', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

However, I have a working python3.8.

sankethbk7777@Lenovo-ideapad:/$ python3.8
Python 3.8.7 (default, Dec 21 2020, 20:10:35) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
sankethbk7777@Lenovo-ideapad:/$ which python3.8
/usr/bin/python3.8

I will provide any further information please help me with this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks like you have Anaconda distribution of Python. I'd simply create a conda virtual environment with the version of Python you need -

conda create -n py38 python=3.8

That should create a conda virtual environment named py38 with Python version 3.8 To activate it,

conda activate py38

And that should give you Python version 3.8


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...