Pankaj Singh
Pankaj Singh

Reputation: 1178

How to create virtual environment for python 3.7.0?

I'm able to install it with root user but I wanted to install it in a clean environment. My use case is to test the installation of another application with pip for the customer who is using python3.7.0

sudo apt-get update

sudo apt-get install build-essential libpq-dev libssl-dev openssl libffi-dev zlib1g-dev

sudo apt-get install python3-pip python3-dev

sudo apt-get install python3.7

Thanks.

Upvotes: 27

Views: 132608

Answers (6)

Nishan
Nishan

Reputation: 4441

Answering here because none of the answers give you where to find python executable.

Here's how to create a virtual environment with Python 3.7.9 on Windows

Go to the python version download page (with binary installers) https://www.python.org/downloads/release/python-379/

Python version download page

Go to that page and download Windows x86-64 embeddable zip file Then extract the zip file and place the contents inside the project folder (under pthon379 folder).

Then use,

virtualenv --python .\python379\python.exe env

To create the virtual environment by specifying the exe location.

Now activate it using .\env\Scripts\activate

If you get any error like virtualenv: The term 'virtualenv' is not recognized as a name of a cmdlet, uninstall virtualenv using pip uninstall virtualenv, close the terminal, open terminal as Administrator / root user and install again.

Upvotes: 2

deirdreamuel
deirdreamuel

Reputation: 857

Figure out python3.7 path on your system. For mac with python3.7 in brew you can use the following

virtualenv env -p /usr/local/opt/[email protected]/bin/python3
source ./env/bin/activate

Upvotes: 1

Ankita
Ankita

Reputation: 169

Some added information, if you are trying for some version like python 3.7.10, which might give following error upon executing pip3.7.10 install virtualenv

.pyenv/versions/3.7.10/bin/python: No module named virtualenv

So, in a general sense you can do the following steps: [commands are specific to MacOs, I am currently using with the new M1 chip]

  1. After installing 3.7.10 using pyenv, make it global.
brew update
brew install pyenv

set environment variables

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

look at the pyenv list to see if the version you install is there or not and install and make it global

pyenv install --list
pyenv install 3.7.10
pyenv global 3.7.10
  1. create your virtual environment now with this version
python -m venv MyEnv 
  1. activate it
source MyEnv/bin/activate

Upvotes: 4

Oscar
Oscar

Reputation: 51

Using pip on windows, you can do the following:

1.virtualenv --python "C:\\Python37\\python.exe" venv# use your own path

You will see something like this:

Running virtualenv with interpreter C:\Python37\python.exe Using base prefix 'C:\Python37' New python executable in C:\Users\XXXX\Documents\GitHub\MyProject\venv\Scripts\python.exe Installing setuptools, pip, wheel... done.

2.C:\Users\XXXXX\Documents\GitHub\MyProject>cd venv C:\Users\XXXXX\Documents\GitHub\MyProject\venv>cd Scripts C:\Users\XXXXX\Documents\GitHub\MyProject\venv\Scripts>activate.

At the beginning of the command path, when you see (environment variable name) in this case (venv), this is a sign that your virtual environment is activated. (venv) C:\Users\tuscar2001\Documents\GitHub\MyProject\venv\Scripts>

Please check the following link for more details:http://www.datasciencetopics.com/2020/03/how-to-set-up-virtual-environment-in.html

Upvotes: 3

Mikhail Stepanov
Mikhail Stepanov

Reputation: 3800

(assuming python3.7 is installed)

Install virtualenv package:

pip3.7 install virtualenv

Create new environment:

python3.7 -m virtualenv MyEnv

Activate environment:

source MyEnv/bin/activate

Upvotes: 60

Bill
Bill

Reputation: 419

To help anyone else who runs into the chicken & egg situation trying to use the above chosen answer, here's what solved it for me:

sudo apt install python3.7-venv
python3.7 -m venv env37
source env37/bin/activate
deactivate (when done using the environment)

I had installed python 3.7 using deadsnakes vs source:

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.7

In doing so I could run python3.7 --version but since I had no pip3.7 I could not install virtualenv as directed in the solution above. Luck would have it that deadsnakes has venv! Once I installed venv I could create my environment & be on my merry way

Handy official python page with venv info

So why didn't I use?: python3.7 -m ensurepip

That was giving me:

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python3.7/dist-packages/easy_install.py' Consider using the --user option or check the permissions.

Which left me with 3 choices:

use sudo (which is simple but I keep being told is frowned upon) install with --user option which wasn't ideal in that I may not always be logged in as the same user or install it in an environment which I'm told is the recommended route.

But see chicken egg above.. How do I install pip in environment when I can't create venv or virtualenv? Thus my workaround solution of installing venv from deadsnakes which allowed me to create the virtual environment to then install pip3.7:

(env37) user@ubuntu:~$ python3.7 -m ensurepip
(env37) user@ubuntu:~$ pip3.7 --version
pip 19.2.3 from /home/user/env37/lib/python3.7/site-packages/pip (python 3.7)

Upvotes: 41

Related Questions