virtualenv on macOS uses nonexistent python interpreter - python

When I run virtualenv, I get this:
$ virtualenv
-bash: /usr/local/bin/virtualenv: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory
virtualenv only started behaving this way today. It worked yesterday. It breaks because virtualenv is a Python script using a nonexistent Python interpreter:
$ head -1 $(which virtualenv)
#!/usr/local/opt/python/bin/python2.7
On my machine, /usr/local/opt/python is a symlink to a Python 3.6 directory:
$ ls -l /usr/local/opt/python
lrwxr-xr-x 1 jim admin 24 2 Mar 13:45 /usr/local/opt/python -> ../Cellar/python/3.6.4_3
As expected, the Python 3.6 directory does not contain a bin/python2.7:
$ ls /usr/local/Cellar/python/3.6.4_3/bin/
2to3 idle pip3 pydoc3.6 python3-config python3.6m-config wheel3
2to3-3.6 idle3 pip3.6 python python3.6 pyvenv
easy_install idle3.6 pydoc python-config python3.6-config pyvenv-3.6
easy_install-3.6 pip pydoc3 python3 python3.6m wheel
virtualenv clearly expects /usr/local/opt/python to contain Python 2 material, but it only contains Python 3 material.
My /usr/local/opt/python is managed by Homebrew. I don't know the provenance of my virtualenv. How do I find out where my /usr/local/bin/virtualenv came from?
Which is to blame? My virtualenv or Homebrew?

The blame for this lies with pip, not Homebrew. My /usr/local/bin/virtualenv came from pip install virtualenv, which embeds an absolute link to the Python interpreter at installation time! I have opened an issue about this unidiomatic behavior.

Same problem on my Mac. Maybe it got broken when I updated to Mojave? Who knows.
Resolved with a brew install of Python 2:
brew install python2
This now takes over from my factory-installed Python 2.7 and gives me a new virtualenv that works:
$ which virtualenv
/usr/local/bin/virtualenv

Firstly, sorry for adding a separate comment here -- I lack the reputation to add a comment to #jameshfisher's answer.
I used homebrew to update python2 on macos to the latest version:
~ ❯❯❯ python2 --version
Python 2.7.15
Which creates/updates the python2 symlink in /usr/local/bin to link to that particular brew installed update:
~ ❯❯❯ ls -ahl =python2
lrwxr-xr-x 1 michael admin 39B 3 Jul 17:11 /usr/local/bin/python2 -> ../Cellar/python#2/2.7.15_1/bin/python2
The shebang in my /usr/local/bin/virtualenv was:
~ ❯❯❯ head -1 $(which virtualenv)
#!/usr/local/opt/python/bin/python2.7
Which did not exist:
~ ❯❯❯ ls -l /usr/local/opt/python/bin/python2.7
ls: /usr/local/opt/python/bin/python2.7: No such file or directory
So modifying the shebang to #!/usr/local/bin/python2 to use the brew installed updated python2 version was the most appropriate way to go:
~ ❯❯❯ virtualenv --version
15.1.0
✨🐟✨
A bit of a red herring for me was that I had mistakenly assumed typing which python would give me the path to my version of python2:
~ ❯❯❯ which python
/Users/michael/.pyenv/shims/python
~ ❯❯❯ which python2
/usr/local/bin/python2
~ ❯❯❯ which python3
/Users/michael/.pyenv/shims/python3
~ ❯❯❯ /Users/michael/.pyenv/shims/python --version
Python 3.7.0
I had forgotten that I had set pyenv global to python 3.7.0. Please don't fall into that trap as I did! ✨😄✨

Related

How to set Python's default version to 3.0.0 on Mac

The Python's default version is 2.7.0 on my MacOS,
python --version
Python 2.7.16
python3 --version
Python 3.9.9
and Python3 has been installed via homebrew, how to make it as default? I want to make it point to the correct directory instead of using alias.
type -a python
python is /usr/bin/python
type -a python3
python3 is /usr/local/bin/python3
python3 is /usr/local/bin/python3
python3 is /usr/local/bin/python3
python3 is /usr/bin/python3
Also, it would be very grateful if you can help with pip also! So far only pip3 has been installed.
type -a pip
pip not found
type -a pip3
pip3 is /usr/local/bin/pip3
pip3 is /usr/local/bin/pip3
pip3 is /usr/local/bin/pip3
pip3 is /usr/bin/pip3
I don't know why my python2 exists in /usr/bin, where ln is not permitted.
Generally doing this can impact legacy programs reliant on Python 2.x, but the below was taken from a website to demonstrate how to change the default.
How to set Python3 as a default python version on MacOS?
This has been answered on this website, and the below are snippets from the website
https://dev.to/malwarebo/how-to-set-python3-as-a-default-python-version-on-mac-4jjf#:~:text=Change%20Python%20Version%20MacHow%20to%20set%20Python3%20as,how%20it%20should%20be%20done%3A%20More%20items...%20
Look where python is installed & view the output.
ls -l /usr/local/bin/python*
Change the default python symlink to the version you want to use from above.
Note that, we only need to choose the one that end with python3.*. Please avoid using the ones' that end with config or python3**.***m or python3.*m-config. Below command shows how it should be done:
ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
Close the current terminal session or keep it that way and instead open a new terminal window
python --version

I want to make command python3 to run python3.7

I am currently using Mac OS Mojave version 10.14.2
I just installed python3.7 and uninstalled python3.6.
Then, the following result occurs
$ python3
-bash: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3: No such file or directory
$ which python3
-bash: type: python3: not found
$ which python3.7
python3.7 is /usr/local/bin/python3.7
I want to make the python3 command runs python3.7 but I am very confused of the setting of python.
I searched on Google but I couldn't solve it.
Could you please help me?
Thank you
That's a bad soft link since you removed python3.
Check out where python3 is by
which python3
Then use the return of that output to see what it's currently pointing to
ls -la $(which python3)
Replace the bad python3 softlink with the correct python3.7
ln -sf $(which python3.7) /usr/local/bin/python3
Test out the version
python3 --version
In the future, check out pyenv because it will make installing new versions of python and switching between versions much easier.

pip or pip3 to install packages for Python 3?

I have a Macbook with OS X El Captain. I think that Python 2.7 comes preinstalled on it. However, I installed Python 3.5 too. When I started using Python 3, I read that if I want to install a package, I should type:
pip3 install some_package
Anyway, now when I use
pip install some_package
I get some_package installed for Python 3. I mean I can import it and use it without problems. Moreover, when I type just pip3 in the Terminal. I got this message about the usage:
Usage:
pip <command> [options]
which is the same message I get when I type just pip.
Does it mean that in previous versions, things were different, and now pip and pip3 can be used interchangeably? If so, and for the sake of argument, how can I install packages for Python 2 instead of Python 3?
Your pip is a soft link to the same executable file path with pip3.
you can use the commands below to check where your pip and pip3 real paths are:
$ ls -l `which pip`
$ ls -l `which pip3`
You may also use the commands below to know more details:
$ pip show pip
$ pip3 show pip
When we install different versions of python, we may create such soft links to
set default pip to some version.
make different links for different versions.
It is the same situation with python, python2, python3
More information below if you're interested in how it happens in different cases:
MacOS/Homebrew
Fedora/CentOS
Debian/Ubuntu
If you had python 2.x and then installed python3, your pip will be pointing to pip3.
you can verify that by typing pip --version which would be the same as pip3 --version.
On your system you have now pip, pip2 and pip3.
If you want you can change pip to point to pip2 instead of pip3.
When you install python3, pip3 gets installed. And if you don't have another python installation(like python2.7) then a link is created which points pip to pip3.
So pip is a link to to pip3 if there is no other version of python installed(other than python3).
pip generally points to the first installation.
By illustration:
pip --version
pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)
pip3 --version
pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)
python --version
Python 3.7.3
which python
/usr/bin/python
ls -l '/usr/bin/python'
lrwxrwxrwx 1 root root 7 Mar 26 14:43 /usr/bin/python -> python3
which python3
/usr/bin/python3
ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 9 Mar 26 14:43 /usr/bin/python3 -> python3.7
ls -l /usr/bin/python3.7
-rwxr-xr-x 2 root root 14120 Mar 26 14:43 /usr/bin/python3.7
Thus, my in my default system python (Python 3.7.3), pip is pip3.
I think pip, pip2 and pip3 are not soft links to the same executable file path. Note these commands and results in my Linux terminal:
mrz#mrz-pc ~ $ ls -l `which pip`
-rwxr-xr-x 1 root root 292 Nov 10 2016 /usr/bin/pip
mrz#mrz-pc ~ $ ls -l `which pip2`
-rwxr-xr-x 1 root root 283 Nov 10 2016 /usr/bin/pip2
mrz#mrz-pc ~ $ ls -l `which pip3`
-rwxr-xr-x 1 root root 293 Nov 10 2016 /usr/bin/pip3
mrz#mrz-pc ~ $ pip -V
pip 9.0.1 from /home/mrz/.local/lib/python2.7/site-packages (python 2.7)
mrz#mrz-pc ~ $ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
mrz#mrz-pc ~ $ pip3 -V
pip 9.0.1 from /home/mrz/.local/lib/python3.5/site-packages (python 3.5)
As you see they exist in different paths.
pip3 always operates on the Python3 environment only, as pip2 does with Python2. pip operates in whichever environment is appropriate to the context. For example, if you are in a Python3 venv, pip will operate on the Python3 environment.
This is a tricky subject. In the end, if you invoke pip it will invoke either pip2 or pip3, depending on how you set your system up.
If you installed Python 2.7, I think you could use pip2 and pip2.7 to install packages specifically for Python 2, like
pip2 install some_pacakge
or
pip2.7 install some_package
And you may use pip3 or pip3.5 to install pacakges specifically for Python 3.
On my Windows instance - and I do not fully understand my environment - using pip3 to install the kaggle-cli package worked - whereas pip did not. I was working in a conda environment and the environments appear to be different.
(fastai) C:\Users\redact\Downloads\fast.ai\deeplearning1\nbs>pip
--version
pip 9.0.1 from C:\ProgramData\Anaconda3\envs\fastai\lib\site-packages (python 3.6)
(fastai) C:\Users\redact\Downloads\fast.ai\deeplearning1\nbs>pip3
--version
pip 9.0.1 from c:\users\redact\appdata\local\programs\python\python36\lib\site-packages
(python 3.6)
Given an activated Python 3.6 virtualenv in somepath/venv, the following aliases resolved the various issues on a macOS Sierra where pip insisted on pointing to Apple's 2.7 Python.
alias pip='python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'
This didn't work so well when I had to do sudo pip as the root user doesn't know anything about my alias or the virtualenv, so I had to add an extra alias to handle this as well. It's a hack, but it works, and I know what it does:
alias sudopip='sudo somepath/venv/bin/python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'
background:
pip3 did not exist to start (command not found) with and which pip would return /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/pip, the Apple Python.
Python 3.6 was installed via macports.
After activation of the 3.6 virtualenv I wanted to work with, which python would return somepath/venv/bin/python
Somehow pip install would do the right thing and hit my virtualenv, but pip list would rattle off Python 2.7 packages.
For Python, this is batting way beneath my expectations in terms of beginner-friendliness.
In my system, I use the update alternatives.
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip2 2
If I want to switch between them I use the following command.
sudo update-alternatives --config pip
Note: The 1st line is enough if you have only pip3 installed and not pip2.

mysql connector installed in python 2.6, but not on 2.7

RedHat is my OS. By default it runs python2.6. However, I needed to install python2.7. I installed it following this post. Now, when I run:
$ python -V
Python 2.7.5
however
$ sudo python -V
Python 2.6.6
So, I incurr problems when installing libraries.
I tried to install mysql-connector by:
pip install mysql-connector-python
following the installation guide for source distribution
following the installation guide for binary distribution
However, mysql-connector module is not installed on the python 2.7 version. If I run:
$ python
>>> import mysql.connector
ImportError: No module named mysql.connector
How can I install mysql module also on python version 2.7?
EDIT
Additional information:
$ which pip
/usr/bin/pip
$ sudo which pip
/usr/bin/pip
$ which easy_install
/opt/rh/python27/root/usr/bin/easy_install
$ sudo which easy_install
/usr/bin/easy_install
/usr/bin/ $ ls -al pip
-rwxr-xr-x. 1 root root 281 Feb 7 11:07 pip
/opt/rh/python27/root/usr/bin $ ls -al easy_install
-rwxr-xr-x. 1 root root 344 Aug 16 11:40 easy_install
This is exactly why you should use virtualenv.
Please check where pip and easy_installed are installed? my guess that which pip would show pip is in the python 2.6 folder. you need to to reinstall pip and easy_install to the python 2.7 folder. you can do it with :
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
and then check which easy_install points to the right direction (you might need to alias/symlink easy_install to the new install. to override the system easy_install) then you can just easy_install pip
Edit (after OP input):
try to easy_install pip (since the basic easy_install is python 2.7)
Update:
Since for some reason python2.7 easy install is installed as root instead of your user you can change ownership of the easy_instlal (or maybe you'll need the entire python2.7 lib. try to change as little as possible) - you can do it with the chown command:
chown username:usergroup(usually your username also) path/to/change/ownership
You should install pip in python2.7.5 environment. After that pip install will work as expected

Use different Python version with virtualenv

How do I create a virtual environment for a specified version of Python?
NOTE: For Python 3.3+, see The Aelfinn's answer below.
Use the --python (or short -p) option when creating a virtualenv instance to specify the Python executable you want to use, e.g.:
virtualenv --python="/usr/bin/python2.6" "/path/to/new/virtualenv/"
Since Python 3, the documentation suggests creating the virtual environment using:
python3 -m venv "my_env_name"
Also, if we want a particular version of python, lets say 3.6, then we can install as
python3.6 -m venv "my_env_name"
Make sure to install the referenced version of python along with your existing system python. For example, if the installed version in your system is python 3.8 only, you will encounter an error stating that "Command 'python3.6' not found".
Obsolete information
The pyvenv script can be used to create a virtual environment:
pyvenv "/path/to/new/virtual/environment"
Deprecated since Python 3.6.
These are the steps you can follow when you are on a shared hosting environment and need to install & compile Python from source and then create venv from your Python version. For Python 2.7.9. you would do something along these lines:
mkdir ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install
virtual env
cd ~/src
wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
tar -zxvf virtualenv-15.0.2.tar.gz
cd virtualenv-15.0.2/
~/.localpython/bin/python setup.py install
virtualenv ve -p $HOME/.localpython/bin/python2.7
source ve/bin/activate
Naturally, this can be applicable to any situation where you want to replicate the exact environment you work and deploy on.
There is an easier way,
virtualenv venv --python=python2.7
Thanks to a comment, this only works if you have python2.7 installed at the system level (e.g. /usr/bin/python2.7).
Otherwise, if you are using homebrew you can use the path to give you what you want.
virtualenv venv --python=/usr/local/bin/python
You can find the path to your python installation with which python (Linux) or py -0p (Windows)
This will also work with python 3.
which python3
>> /usr/local/bin/python3
virtualenv venv --python=/usr/local/bin/python3
Ultimately condensing to:
virtualenv venv -p `which python`
virtualenv venv -p `which python3`
virtualenv --python=/usr/bin/python2.6 <path/to/myvirtualenv>
Under Windows for me this works:
virtualenv --python=c:\Python25\python.exe envname
without the python.exe I got WindowsError: [Error 5] Access is denied
I have Python2.7.1 installed with virtualenv 1.6.1, and I wanted python 2.5.2.
Mac OSX 10.6.8 (Snow Leopard):
1) When you do pip install virtualenv, the pip command is associated with one of your python versions, and virtualenv gets installed into that version of python. You can do
$ which pip
to see what version of python that is. If you see something like:
$ which pip
/usr/local/bin/pip
then do:
$ ls -al /usr/local/bin/pip
lrwxrwxr-x 1 root admin 65 Apr 10 2015 /usr/local/bin/pip ->
../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip
You can see the python version in the output.
By default, that will be the version of python that is used for any new environment you create. However, you can specify any version of python installed on your computer to use inside a new environment with the -p flag:
$ virtualenv -p python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3.2
New python executable in my_env/bin/python
Installing setuptools, pip...done.
virtualenv my_env will create a folder in the current directory which
will contain the Python executable files, and a copy of the pip
[command] which you can use to install other packages.
http://docs.python-guide.org/en/latest/dev/virtualenvs/
virtualenv just copies python from a location on your computer into the newly created my_env/bin/ directory.
2) The system python is in /usr/bin, while the various python versions I installed were, by default, installed into:
/usr/local/bin
3) The various pythons I installed have names like python2.7 or python3.2, and I can use those names rather than full paths.
========VIRTUALENVWRAPPER=========
1) I had some problems getting virtualenvwrapper to work. This is what I ended up putting in ~/.bash_profile:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/django_projects #Not very important -- mkproject command uses this
#Added the following based on:
#http://stackoverflow.com/questions/19665327/virtualenvwrapper-installation-snow-leopard-python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7
#source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh
2) The -p option works differently with virtualenvwrapper: I have to specify the full path to the python interpreter to be used in the new environment(when I do not want to use the default python version):
$ mkvirtualenv -p /usr/local/bin/python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3
New python executable in my_env/bin/python
Installing setuptools, pip...done.
Usage: source deactivate
removes the 'bin' directory of the environment activated with 'source
activate' from PATH.
Unlike virtualenv, virtualenvwrapper will create the environment at the location specified by the $WORKON_HOME environment variable. That keeps all your environments in one place.
[November 2019] I needed to install a Python 3.7 environment (env) on my Python 3.8-based Arch Linux system. Python 3.7 was no longer on the system, so I could not downgrade Python, to install a package that I needed.
Furthermore, I wanted to use that package / Python 3.7 inside a virtual environment (venv). This is how I did it.
Download Python version source files:
I downloaded the Python 3.7.4 source files from
https://www.python.org/downloads/source/
to
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4.tgz
I then extracted that archive (source files) to
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
Installation:
[Note: in my system env, not a venv.]
cd /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
time ./configure ## 17 sec
time make ## 1 min 51 sec
time sudo make install ## 18 sec
time make clean ## 0.3 sec
Examine installed Python versions:
$ which python
/usr/bin/python
$ python --version
Python 3.8.0
$ which python3.7
/usr/local/bin/python3.7
$ python ## Python 3.8 [system / env]
Python 3.8.0 (default, Oct 23 2019, 18:51:26)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python3.7 ## newly-installed Python 3.7 package
Python 3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0]
>>>
$ python3.7 --version
Python 3.7.4
How to create a venv for a specific Python version:
https://docs.python.org/3/tutorial/venv.html
12.2. CREATING VIRTUAL ENVIRONMENTS
The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.
To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:
python3 -m venv tutorial-env
This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.
...
Create Python 3.7 venv [on a Python 3.8 operating env / system]:
python3.7 -m venv ~/venv/py3.7 ## create Python 3.7-based venv
source ~/venv/py3.7/bin/activate ## activate that venv
deactivate ## deactivate that venv (when done, there)
Added to ~/.bashrc:
alias p37='echo " [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]" && source ~/venv/py3.7/bin/activate'
Test Python 3.7 venv:
$ p37
[Python 3.7 venv (source ~/venv/py3.7/bin/activate)]
(py3.7)$ python --version
Python 3.7.4
(py3.7)$ python
Python 3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0]
>>>
Suppose you currently have python 2.7 installed in your virtualenv. But want to make use of python3.2, You would have to update this with:
$ virtualenv --python=/usr/bin/python3.2 name_of_your_virtualenv
Then activate your virtualenv by:
$ source activate name_of_your_virtualenv
and then do: python --version in shell to check whether your version is now updated.
You should have that Python version installed. If you have it then basically,
With virtualenv,
virtualenv --python=python3.8 env/place/you/want/to/save/to
with venv
python3.8 -m venv env/place/you/want/to/save/to
The above examples are for python3.8, you can change it to have different versions of virtual environments given that they are installed in your computer.
These two commands should work fine.
virtualenv -p python2 myenv (For python2)
virtualenv -p python3 myenv (For python3)
You can call virtualenv with python version you want. For example:
python3 -m virtualenv venv
Or alternatively directly point to your virtualenv path. e.g. for windows:
c:\Python34\Scripts\virtualenv.exe venv
And by running:
venv/bin/python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
you can see the python version installed in virtual environment
The -p approach works well, but you do have to remember to use it every time. If your goal is to switch to a newer version of Python generally, that's a pain and can also lead to mistakes.
Your other option is to set an environment variable that does the same thing as -p. Set this via your ~/.bashrc file or wherever you manage environment variables for your login sessions:
export VIRTUALENV_PYTHON=/path/to/desired/version
Then virtualenv will use that any time you don't specify -p on the command line.
On the mac I use pyenv and virtualenvwrapper. I had to create a new virtualenv. You need homebrew which I'll assume you've installed if you're on a mac, but just for fun:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
export PATH=/Users/{USERNAME}/.pyenv/versions/2.7.10/bin:$PATH
mkvirtualenv -p ~/.pyenv/versions/2.7.10/bin/python {virtual_env_name}
I also froze my requirements first so i could simply reinstall in the new virtualenv with:
pip install -r requirements.txt
Even easier, by using command substitution to find python2 for you:
virtualenv -p $(which python2) <path/to/new/virtualenv/>
Or when using virtualenvwrapper :
mkvirtualenv -p $(which python2) <env_name>
As already mentioned in multiple answers, using virtualenv is a clean solution. However a small pitfall that everyone should be aware of is that if an alias for python is set in bash_aliases like:
python=python3.6
this alias will also be used inside the virtual environment. So in this scenario running python -V inside the virtual env will always output 3.6 regardless of what interpreter is used to create the environment:
virtualenv venv --python=pythonX.X
For Mac(High Sierra), install the virtualenv on python3 and create a virtualenv for python2:
$ python3 -m pip install virtualenv
$ python3 -m virtualenv --python=python2 vp27
$ source vp27/bin/activate
(vp27)$ python --version
Python 2.7.14
These seem a little overcomplicated for Windows. If you're on Windows running python 3.3 or later, you can use the python launcher py to do this much more easily. Simply install the different python version, then run:
py -[my version] -m venv env
This will create a virtual environment called env in your current directory, using python [my version]. As an example:
py -3.7 -m venv env
./env/Scripts/activate
This creates a virtual environment called env using python3.7 and activates it. No paths or other complex stuff required.
I utilized this answer for Windows
https://stackoverflow.com/a/22793687/15435022
py -3.4 -m venv c:\path\to\wherever\you\want\it
On windows:
py -3.4x32 -m venv venv34
or
py -2.6.2 -m venv venv26
This uses the py launcher which will find the right python executable for you (assuming you have it installed).
In windows subsystem for linux:
Create environment for python3:
virtualenv --python=/usr/bin/python3 env
Activate it:
source env/bin/activate
I use pyenv to manage my python version.
pyenv install 3.7.3
pyenv local 3.7.3
Check your python version:
$ python --version
Python 3.7.3
Create the virtual environment with venv:
python -m venv .
Then activate the Virtual Environment:
source bin/activate
Check your python version:
$ python --version
Python 3.7.3
You may need to remove the previous virtual environment
rm -rf bin
End of 2020:
The most seamless experience for using virtualenv (added benefit: with any possible python version) would be to use pyenv and its (bundled) pyenv-virtualenv plugin (cf https://realpython.com/intro-to-pyenv/#virtual-environments-and-pyenv)
Usage: pyenv virtualenv <python_version> <environment_name>
Installation:
first check that you've got all prerequisites (depending on your OS): https://github.com/pyenv/pyenv/wiki/Common-build-problems#prerequisites
curl https://pyenv.run | bash
exec $SHELL
cf https://github.com/pyenv/pyenv-installer
That being said, nowadays the best possible alternative instead of using virtualenv (and pip) would be Poetry (along with pyenv indicated above, to handle different python versions).
Another option, because it's supported directly by the PyPA (the org behind pip and the PyPI) and has restarted releasing since the end of May (didn't release since late 2018 prior to that...) would be Pipenv
This worked for my usage in Windows 10, where I have Python 3.7 and want to downgrade for a project in Python 3.6.6:
I used "venv" to create a new environment called "venv", I downloaded from https://www.python.org/downloads/windows/ ; install "Download Windows x86-64 executable installer-" ; then I used the following command line in the directory where I want to create my environment
>C:\Users\...\Python\Python36\python.exe -m venv venv
Finally, I activated the environnent using the command line:
>venv\Scripts\activate.bat
And check the python version by calling:
>python --version
Python 3.6.6
On Linux Ubuntu 21.04 (currently Python 3.9.5) I needed to get a virtualenv of Python 3.7.8. Full steps to get working:
Find the Python version source you want, for example 3.7.8 is here: https://www.python.org/downloads/release/python-378/
Download the Gzipped source tarball
Unzip it with tar zxvf Python-3.7.8.tgz (amend as required with your version number if different from 3.7.8)
Copy the unzipped folder to /usr/bin with: sudo cp -r Python-3.7.8 /usr/bin
cd /usr/bin/Python-3.7.8/
Check the contents if you wanted to see what you have so far: ls
sudo time ./configure
sudo time make
time sudo make install
time make clean
Check how your python is set up and reporting:
which python
python --version
Should be all relating to your primary install (Python 3.9.5 for me)
To check your new install:
which python 3.7
python3.7 --version
Should be all relating to your 3.7.8 install
If you want to run it to check, do:
python3.7
exit()
Install venv:
sudo apt install venv
To create a venv (maybe in your repo, if so, add .venv to .gitignore):
python3.7 -m venv .venv
To activate your venv:
source .venv/bin/activate
Check your version:
python --version
Answer to this question shouldn't be that complicated...
TL,DR:
install as many versions of python you prefer on your system and use:
/c/path/to/any/version/of/python -m venv my_venv
============================================
I use venv to install virtual environments with
python -m venv <where/to/and/name_of_venv>
if you try which python you will see which python you are referring to, when saying "python". for example, for me it is:
which python
result:
/c/Program Files/Python36/python
So, now you have the answer!
you can install any version of python on your system and have multiple of them at the same time. So, for example I installed Python3.7 in this directory: "C:\Program Files\Python37".
So, instead of using 'python' now I specify which python by /c/Program\ Files/Python37/python:
/c/Program\ Files/Python37/python -m venv my_venv
(don't forget to escape the space in the path)
That's it!
Yes, the above answers are correct and works fine on Unix based systems like Linux & MAC OS X.
I tried to create virtualenv for Python2 & Python3 with the following commands.
Here I have used venv2 & venv3 as their names for Python2 & Python3 respectively.
Python2 »
MacBook-Pro-2:~ admin$ virtualenv venv2 --python=`which python2`
Running virtualenv with interpreter /usr/local/bin/python2
New python executable in /Users/admin/venv2/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ ls venv2/bin/
activate easy_install pip2.7 python2.7
activate.csh easy_install-2.7 python wheel
activate.fish pip python-config
activate_this.py pip2 python2
MacBook-Pro-2:~ admin$
Python3 »
MacBook-Pro-2:~ admin$ virtualenv venv3 --python=`which python3`
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/admin/venv3/bin/python3
Also creating executable in /Users/admin/venv3/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ ls venv3/bin/
activate easy_install pip3.6 python3.6
activate.csh easy_install-3.6 python wheel
activate.fish pip python-config
activate_this.py pip3 python3
MacBook-Pro-2:~ admin$
Checking Python installation locations
MacBook-Pro-2:~ admin$ which python2
/usr/local/bin/python2
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ which python3
/usr/local/bin/python3
MacBook-Pro-2:~ admin$
I use Windows so I should use .exe on the pthon path
virtualenv -p=C:\Python27\python2.exe <envname>
It worked for me
sudo apt-get install python3-minimal
virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
virtualenv -p python3 myenv
Link to Creating virtualenv

Categories

Resources