This question already has answers here:
Dealing with multiple Python versions and PIP?
(28 answers)
Closed 1 year ago.
I have python 3.7.8 and python 3.10.0 installed. I have a question regarding the dependencies of these two. Is it okay to have this two installed at the same time but in different versions?
Does it make my site-packages conflict with one another?
On my command prompt, when I check my python version it says I am using the recent version, 3.10.0.
Having multiple versions on the same machine is perfectly "okay", as long as you understand how to select/use the correct python version, which is the more pertinent and useful question you should be asking yourself.
They would normally be installed in separate locations and would have separate site-packages (from How do I find the location of my Python site-packages directory?):
~$ python3.7 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python#3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']
~$ python3.8 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python#3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']
~$ python3.9 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python#3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages']
The recommended way however is to not install to these site-packages folder directly and use a virtual environment instead. There are many variations of virtual env packages/tools, but you can start with the Python docs on Virtual Environments if you are not familiar or using one yet.
With virtual environments, you can usually indicate which version of python to use when creating the virtual env, so that whenever you activate that same env, it would use the same version you used to create it.
tmp$ python3.7 -m venv app1
tmp$ source ./app1/bin/activate
(app1) tmp$ python -V
Python 3.7.12
(app1) tmp$
(app1) tmp$ deactivate
tmp$ python3.8 -m venv app2
tmp$ source ./app2/bin/activate
(app2) tmp$ python -V
Python 3.8.12
(app2) tmp$
(app2) tmp$ deactivate
tmp$ python3.9 -m venv app3
tmp$ source ./app3/bin/activate
(app3) tmp$ python -V
Python 3.9.9
(app3) tmp$
(app3) tmp$ deactivate
In the above example, I created 3 virtual environments for 3 hypothetical apps, each using a different Python version. As long as I activate the correct virtual environment, I don't have to think about which version python refers to, as it will use the same version used to create the env. See also How do I check what version of Python is running my script?.
As for installing packages, again, once you have setup the correct virtual environments, doing pip install would ensure it installs only on the site-packages on that environment, and the app running on that env would be able to import that package.
If not using a virtual environment, as I noted in my comment, the answers at Dealing with multiple Python versions and PIP? provide good suggestions on how to ensure you are installing packages for the correct Python version, namely with
$ </path/or/alias/to/specific/python/installation> -m pip install <packages>
$ python3.7 -m pip install "flake8<=3.6"
$ python3.7 -m pip list | grep flake8
flake8 3.6.0
$ ls -H /usr/local/Cellar/python#3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages | grep flake8
flake8
flake8-3.6.0.dist-info
flake8_quotes
flake8_quotes-3.2.0.dist-info
$ python3.9 -m pip install flake8
$ python3.9 -m pip list | grep flake8
flake8 4.0.1
$ ls -H /usr/local/Cellar/python#3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages | grep flake8
flake8
flake8-4.0.1.dist-info
The above example shows 2 different versions of flake8 installed for 2 different versions of Python, each on their own site-packages folder.
But when I run my command prompt and check my python version it says I am using the recent version. The 3.10.0.
This means the system default for python or python3 on your machine points to the 3.10 installation. Mine is set to point to Python 3.9:
tmp$ python3 -V
Python 3.9.9
tmp$ which python3
/usr/local/bin/python3
tmp$ /usr/local/bin/python3 -V
Python 3.9.9
You can:
Manually change the default version
Setup aliases to the different versions:
tmp$ type python3.7
python3.7 is aliased to `/usr/local/opt/python#3.7/bin/python3'
tmp$ type python3.8
python3.8 is aliased to `/usr/local/opt/python#3.8/bin/python3'
tmp$ type python3.9
python3.9 is aliased to `/usr/local/opt/python#3.9/bin/python3'
Use a version management tool for switching versions, such as pyenv
To add onto Gino Mempin's answer it's also worth noting that in Windows you can quickly switch between versions of Python by editing the PATH environment variable in System Variables under your Environment Variables.
From my experience most people who switch between Python versions are on Linux but for all those people who might be using Windows you can just go to This PC -> Right-click -> Properties -> Advanced System Settings then from System Properties go to the Advanced tab and select Environment Variables to edit PATH. Note you don't have to delete older versions of Python here you can just add other ones and then select to move them above the others to give them priority.
You'll also need to restart your Command Prompt or Powershell after doing this if you had it open and was using Python before editing the environment variables.
After doing this you'll be able to install and manage packages for the current version of Python that has priority.
There is no activate script in my virtual environment's bin folder.
Only files are: python, python3, python3.9.
How to fix my environment without deleting it? I'm using venv not virtualenv. I'm using Linux.
My problem is similar to this one -
There is no activate when I am trying to run my virtual env
One of answers says I should run.
python3.7 -m venv venv
But I don't understand this syntax. Should I literally write python3.9 -m venv venv into my terminal? Will it fix my enviroment?
Also I want to say that newly created environments work as expected and all the others too.
I had this problem with python 3.10, I had to install venv for my version otherwise I was missing the activate script. (I am using the deadsnake ppa on ubuntu) after this the activate script appears when creating new venv.
sudo apt-get install python3.9-venv
I documented the full python 3.10 on Ubuntu installation here
i think this is helpfull for you
but for basic use it like this:
python3.7 -m venv myenvname
source myenvname/bin/activate
and if activate isnt there try to find that in sub directories
I installed Python 3.7.1 using home-brew, and on the command line typing python returns the default python 2.7.10 and python3 gives the home-brew installed version. Then using
python3 -m pip install --user numpy scipy matplotlib
I installed the required packages.
Now what I understand is that to use the home-brew version python I have to always use python3and the required packages are installed inside python 3's lib folder.
I want to install virtual env correctly. I want to know what command should I use if I want it to be usable with python3?
You don't need to install virtual env because venv module is included in Python 3 standard library.
To create a virtualenv under .venv subdirectory of the current working directory:
python3 -m venv .venv
From my understanding you want to create python3 environments. First install virtualenv using pip.
pip install virtualenv
Then you can create a python3 environment like this:
virtualenv -p python3 env_name
I'm working in Amazon's Cloud9.
ec2-user:~/environment/flask_init $ python -V
Python 2.7.14
ec2-user:~/environment/flask_init $ virtualenv -p python3 venv
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/ec2-user/environment/flask_init/venv/bin/python3
Also creating executable in /home/ec2-user/environment/flask_init/venv/bin/python
Installing setuptools, pip, wheel...done.
ec2-user:~/environment/flask_init $ source venv/bin/activate
(venv) ec2-user:~/environment/flask_init $ python -V
Python 2.7.14
Why is the virtual environment not using Python 3?
Please note that this question is not a duplicate of this one. The issue was specifically to do with the way the Cloud 9 environment sets up Python alias.
I tried your flow on my machine and everything works as expected.
dluzak#Karol-PC:/tmp$ python -V
Python 2.7.12
dluzak#Karol-PC:/tmp$ virtualenv -p python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /tmp/venv/bin/python3
Also creating executable in /tmp/venv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
dluzak#Karol-PC:/tmp$ source venv/bin/activate
(venv) dluzak#Karol-PC:/tmp$ python -V
Python 3.5.2
(venv) dluzak#Karol-PC:/tmp$
Nonetheless I personally use virtualenv as module when creating venv with python 3: python3 -m virtualenv venv. Maybe this would work.
You provided very little details. Have you installed virtualenv for both Python 2 and 3? Are you sure Python 3 interpreter works fine?
Edit:
After investigation in comments we found out that the problem was in bash settings configured by Amazon. It seams that Amazon configures bash (probably in ~/.bashrc) to replace python calls with an alias. To fix this a call unalias python before enabling venv is needed. It is described in Amazon docs
When I was using virtualenv earlier today, I had the same problem that my env was not using the right version of python.
Instead of activating my environment like this:
source activate
I found that activating it like this actually worked:
source ./activate
Hope this is helpful!
Here is how i create virtualenv on Cloud9
Python 3.4
$ sudo pip install virtualenv
$ virtualenv -p /usr/bin/python3.4 venv
$ source venv/bin/activate
Python 3.6
$ sudo apt update
$ sudo apt install python3.6-venv
$ python3.6 -mvenv venv
$ source venv/bin/activate
I have encountered a similar issue.
In my case did not work because I moved the virtual env folder (but the same thing happens when you rename it).
You can understand which version of python (and thus which module will import) is using by typing
$ which python
If it write something like:
/usr/bin/python
Then it means your virtual env is not being activated.
To solve this issue, instead of creating a new virtual environment, you can simply edit the script activation file in your env:
$ nano venv/bin/activate
And edit the following line with your absolute path of your virtual environment:
VIRTUAL_ENV="/YOUR_ABSOLUT/PATH_TO/venv"
Hope it helps
:)
Using virtualenv, I run my projects with the default version of Python (2.7). On one project, I need to use Python 3.4.
I used brew install python3 to install it on my Mac. Now, how do I create a virtualenv that uses the new version?
e.g. sudo virtualenv envPython3
If I try:
virtualenv -p python3 test
I get:
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.4.0_1/Frameworks/Python.framework/Versions/3.4'
New python executable in test/bin/python3.4
Also creating executable in test/bin/python
Failed to import the site module
Traceback (most recent call last):
File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/site.py", line 67, in <module>
import os
File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/os.py", line 634, in <module>
from _collections_abc import MutableMapping
ImportError: No module named '_collections_abc'
ERROR: The executable test/bin/python3.4 is not functioning
ERROR: It thinks sys.prefix is '/Users/user/Documents/workspace/test' (should be '/Users/user/Documents/workspace/test/test')
ERROR: virtualenv is not compatible with this system or executable
simply run
virtualenv -p python3 envname
Update after OP's edit:
There was a bug in the OP's version of virtualenv, as described here. The problem was fixed by running:
pip install --upgrade virtualenv
Python 3 has a built-in support for virtual environments - venv. It might be better to use that instead. Referring to the docs:
Creation of virtual environments is done by executing the pyvenv
script:
pyvenv /path/to/new/virtual/environment
Update for Python 3.6 and newer:
As pawciobiel correctly comments, pyvenv is deprecated as of Python 3.6 and the new way is:
python3 -m venv /path/to/new/virtual/environment
I'v tried pyenv and it's very handy for switching python versions (global, local in folder or in the virtualenv):
brew install pyenv
then install Python version you want:
pyenv install 3.5.0
and simply create virtualenv with path to needed interpreter version:
virtualenv -p /Users/johnny/.pyenv/versions/3.5.0/bin/python3.5 myenv
That's it, check the version:
. ./myenv/bin/activate && python -V
There are also plugin for pyenv pyenv-virtualenv but it didn't work for me somehow.
Install prerequisites.
sudo apt-get install python3 python3-pip virtualenvwrapper
Create a Python3 based virtual environment. Optionally enable --system-site-packages flag.
mkvirtualenv -p /usr/bin/python3 <venv-name>
Set into the virtual environment.
workon <venv-name>
Install other requirements using pip package manager.
pip install -r requirements.txt
pip install <package_name>
When working on multiple python projects simultaneously it is usually recommended to install common packages like pdbpp globally and then reuse them in virtualenvs.
Using this technique saves a lot of time spent on fetching packages and installing them, apart from consuming minimal disk space and network bandwidth.
sudo -H pip3 -v install pdbpp
mkvirtualenv -p $(which python3) --system-site-packages <venv-name>
Django specific instructions
If there are a lot of system wide python packages then it is recommended to not use --system-site-packages flag especially during development since I have noticed that it slows down Django startup a lot. I presume Django environment initialisation is manually scanning and appending all site packages from the system path which might be the reason. Even python manage.py shell becomes very slow.
Having said that experiment which option works better. Might be safe to just skip --system-site-packages flag for Django projects.
virtualenv --python=/usr/bin/python3 <name of env>
worked for me.
This is all you need, in order to run a virtual environment in python / python3
First if virtualenv not installed, run
pip3 install virtualenv
Now Run:
virtualenv -p python3 <env name> # you can specify full path instead <env_name> to install the files in a different location other than the current location
Sometime the cmd virtualenv fails, if so use this:
python3 -m virtualenv <env_name> # you can specify full path instead <env_name> to install the files in a different location other than the current location
Now activate the virtual env:
source <env_name>/bin/activate
Or:
source `pwd`/<env_name>/bin/activate
Now run
which python
You should see the full path to your dir and <env_name>/bin/python suffix
To exit the virtualenv, run:
deactivate
To troubleshoot Python location got to here
You can specify specific Version of Python while creating environment.
It's mentioned in virtualenv.py
virtualenv --python=python3.5 envname
In some cases this has to be the full path to the executable:
virtualenv --python=/Users/username/.pyenv/versions/3.6.0/bin/python3.6 envname
How -p works
parser.add_option(
'-p', '--python',
dest='python',
metavar='PYTHON_EXE',
help='The Python interpreter to use, e.g., --python=python3.5 will use the python3.5 '
'interpreter to create the new environment. The default is the interpreter that '
'virtualenv was installed with (%s)' % sys.executable)
I had the same ERROR message. tbrisker's solution did not work in my case. Instead this solved the issue:
$ python3 -m venv .env
In addition to the other answers, I recommend checking what instance of virtualenv you are executing:
which virtualenv
If this turns up something in /usr/local/bin, then it is possible - even likely - that you installed virtualenv (possibly using an instance of easy_tools or pip) without using your system's package manager (brew in OP's case). This was my problem.
Years ago - when I was even more ignorant - I had installed virtualenv and it was masking my system's package-provided virtualenv.
After removing this old, broken virtualenv, my problems went away.
The below simple commands can create a virtual env with version 3.5
apt-get install python3-venv
python3.5 -m venv <your env name>
if you want virtual env version as 3.6
python3.6 -m venv <your env name>
Python now comes with its own implementation of virtual environment, by the name of "venv". I would suggest using that, instead of virtualenv.
Quoting from venv - docs,
Deprecated since version 3.6: pyvenv was the recommended tool for
creating virtual environments for Python 3.3 and 3.4, and is
deprecated in Python 3.6.
Changed in version 3.5: The use of venv is now recommended for
creating virtual environments.
For windows, to initiate venv on some project, open cmd:
python -m venv "c:\path\to\myenv"
(Would suggest using double quote around directory path if it contains any spaces. Ex: "C:/My Dox/Spaced Directory/Something")
Once venv is set up, you will see some new folders inside your project directory. One of them would be "Scripts".
To activate or invoke venv you need:
C:\> <venv>\Scripts\activate.bat
You can deactivate a virtual environment by typing “deactivate” in your shell. With this, you are now ready to install your project specific libraries, which will reside under the folder "Lib".
================================ Edit 1 ====================================
The scenario which will be discussed below is not what originally asked, just adding this in case someone use vscode with python extension
In case, you use vs code with its python extension, you might face an issue with its pylint which points to the global installation. In this case, pylint won't be able to see the modules that are installed in your virtual environment and hence will show errors while importing.
Here is a simple method to get past this.
cd Workspace\Scripts
.\Activate.ps1
code .
We are basically activating the environment first and then invoking vs-code so that pylint starts within the environment and can see all local packages.
In python3.6 I tried
python3 -m venv myenv,
as per the documentation, but it was taking so long. So the very simple and quick command is
python -m venv yourenv
It worked for me on python3.6.
On Mac I had to do the following to get it to work.
mkvirtualenv --python=/usr/bin/python3 YourEnvNameHere
If you install python3 (brew install python3) along with virtualenv burrito, you can then do mkvirtualenv -p $(which python3) env_name
Of course, I know virtualenv burrito is just a wrapper, but it has served me well over the years, reducing some learning curves.
virtualenv --python=/usr/local/bin/python3 <VIRTUAL ENV NAME>
this will add python3
path for your virtual enviroment.
It worked for me
virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
For those having troubles while working with Anaconda3 (Python 3).
You could use
conda create -n name_of_your_virtualenv python=python_version
To activate the environment ( Linux, MacOS)
source activate name_of_your_virtualenv
For Windows
activate name_of_your_virtualenv
I tried all the above stuff, it still didn't work. So as a brute force, I just re-installed the anaconda, re-installed the virtualenv... and it worked.
Amans-MacBook-Pro:~ amanmadan$ pip install virtualenv
You are using pip version 6.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting virtualenv
Downloading virtualenv-15.0.3-py2.py3-none-any.whl (3.5MB)
100% |████████████████████████████████| 3.5MB 114kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.3
Amans-MacBook-Pro:python amanmadan$ virtualenv my_env
New python executable in /Users/amanmadan/Documents/HadoopStuff/python/my_env/bin/python
Installing setuptools, pip, wheel...done.
Amans-MacBook-Pro:python amanmadan$
I wanted to keep python 2.7.5 as default version on Centos 7 but have python 3.6.1 in a virtual environment running alongside other virtual environments in python 2.x
I found the below link the best solution for the newest python version ( python 3.6.1)
https://www.digitalocean.com/community/tutorial_series/how-to-install-and-set-up-a-local-programming-environment-for-python-3.
It shows the steps for different platforms but the basic steps are
Install python3.x (if not present) for your platform
Install python3.x-devel for your platform
Create virtual environment in python 3.x
(for example $ python3.6 -m venv virenv_test_p3/ )
Activate the testenvironment for python 3.x
(for example source virenv_test_p3/bin/activate)
Install the packages which you want to use in your new python 3 virtual environment and which are supported ( for example pip install Django==1.11.2)
On Windows command line, the following worked for me. First find out where your python executables are located:
where python
This will output the paths to the different python.exe on your system. Here were mine:
C:\Users\carandangc\Anaconda3\python.exe
C:\Python27\python.exe
So for Python3, this was located in the first path for me, so I cd to the root folder of the application where I want to create a virtual environment folder. Then I run the following which includes the path to my Python3 executable, naming my virtual environment 'venv':
virtualenv --python=/Users/carandangc/Anaconda3/python.exe venv
Next, activate the virtual environment:
call venv\Scripts\activate.bat
Finally, install the dependencies for this virtual environment:
pip install -r requirements.txt
This requirements.txt could be populated manually if you know the libraries/modules needed for your application in the virtual environment. If you had the application running in another environment, then you can automatically produce the dependencies by running the following (cd to the application folder in the environment where it is working):
pip freeze > requirements.txt
Then once you have the requirements.txt that you have 'frozen', then you can install the requirements on another machine or clean environment with the following (after cd to the application folder):
pip install -r requirements.txt
To see your python version in the virtual environment, run:
python --version
Then voila...you have your Python3 running in your virtual environment. Output for me:
Python 3.7.2
For those of you who are using pipenv and want to install specific version:
pipenv install --python 3.6
I got the same error due to it being a conflict with miniconda3 install so when you type "which virtualenv" and if you've installed miniconda and it's pointing to that install you can either remove it (if your like me and haven't moved to it yet) or change your environment variable to point to the install you want.