How to create virtual environment for python 3.7.0?
(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
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)
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]
- 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
- create your virtual environment now with this version
python -m venv MyEnv
- activate it
source MyEnv/bin/activate
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