简体   繁体   中英

virtual environment in python

I want to use python version 3.6 or 3.7 because it supports tensorflow1.15. Am trying to create a virtual environment in my linux pc

Method 1:

mkdir project,
cd project,
python3.7 -m venv myvenv,
Error "bash: python3.7: command not found",

Method 2:

virtualenv myvenv --python=python3.7;
Error "RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.7'",

Help me how to install a python 3.7 in virtual environment. Later, I want to install tensorflow 1.15 to continue my project.

First you need to install Python 3.7 on your system and set it as default and I'm assuming you use Linux based system

sudo apt install python3.7
sudo update-alternatives --config python
sudo update-alternatives  --set python /usr/bin/python3.7

Then you could create your venv using python3.7

If you don't, in general a better solution for data science and machine learning is to use Andaconda . This way you could manage your virtual environment easily. Check this cheatsheet for more info.

An easier way would be to use conda in order to create a new environment:

conda create -n myenv python=3.7

This works easily and I personally prefer it as it reduces the effort of going to the source file to activate the new environment

Install new Python version, then use it in venv

First download and install the Python version you want, then create a virtual environment that uses this newly installed Python version.

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. The method 1 in your case.

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/

Install another version of Python

For example, in Ubuntu 20.04, to install Python 3.7:

# Update package lists
me@mydevice:~$ sudo apt update

# Add the deadsnakes repository
me@mydevice:~$ sudo add-apt-repository ppa:deadsnakes/ppa

# Install Python 3.7
me@mydevice:~$ sudo apt install python3.7

Install the venv package and create a venv virtual environment

# Install the venv package for Python 3.7
me@mydevice:~$ sudo apt install python3.7-venv

# Make a folder for venv virtual environments
me@mydevice:~$ mkdir ~/.venvs

# Create a new venv virtual environment with Python 3.7 in it
me@mydevice:~$ python3.7 -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 Python version within the venv virtual environment

# Check the Python version inside the venv:
(my-venv-name) me@mydevice:~$ python -V
Python 3.7.12

Deactivate the virtual environment

(my-venv-name) me@mydevice:~$ deactivate
me@mydevice:~$

Check Python version outside any virtual environments

# Check Python version:
me@mydevice:~$ python -V
Python 3.8.10

Do not set the new Python as system default

The Linux system needs its original Python version to be default for its own functionality. The system Python, in the case of Ubuntu 20.04 - Python 3.8, should be left as default, otherwise the system might become instable.

More info here: https://unix.stackexchange.com/questions/652299/changing-pythons-default-version-breaks-ubuntu-20-04

Install more Python versions

To install more Python versions, just change the version number from 3.7 to which ever version you choose, that is available from the deadsnakes repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM