So, for some reason, I have way too many versions of python on my Mac.
Running python in the terminal gets me Python 2.7.10.
python2 gets me Python 2.7.14.
python2.7 gets me 2.7.13.
python2.6 gets me 2.6.9.
python3 gets me 3.6.3.
python3.6 gets me 3.6.3 again. Not sure if this is a separate install or not.
python3.4 gets me 3.4.7.
So I have 6, possibly 7 versions of Python. And I have no idea what to do.
As for pip, I have the following installed: pip, pip2, pip2.6, pip2.7, pip3, and pip3.6.
Some of these versions of Python were shipped with the OS, and others were installed with Homebrew.
So it's a terrifying, yet sort of hilarious mess I've gotten myself into. How can I better manage my versions? Most of the versions installed I do not use, but can't remove them because they are part of the system. So how can I make my Python versions less of a pain?
I recommend looking into pyenv, it makes managing multiple versions of Python much easier. With the virtualenv plugin life gets even better. With both installed you can do something like this:
pyenv install 2.7.13
pyenv install 3.5.1
pyenv install 3.6.3
pyenv virtualenv 2.7.13 proj1
pyenv virutalenv 3.6.3 proj2
pyenv activate proj2
python -V # 3.6.3
pyenv activate proj1
python -V # 2.7.13
And of course each project now has its own clean virtual environment you can install packages in with pip.
My Python virtual environments use python3.6 when I create them using virtualenv
~ $ virtualenv my_env
but I need to use python3.5 as 3.6 is not currently supported by Opencv3.
I've tried using the --python=<py_version> flag when creating a virtual environment but this doesn't work.
How do I specify the python (3.x) version to install using virtualenv for Mac and/or Linux?
Assuming that you have installed python3 or any desired version of Python (2.6, 2.7, 3.5, 3.6), Now while creating the virtual environment directly pass the python executable path. Hence here are few valid example
$ virtualenv new_p2_env # Creates a new default python environment (usually python 2)
$ virtualenv -p python3 new_p3_env # Creates a new default python3 (python3 must be a valid command i.e found in the PATH)
And last
# Directly point to any version of python binary, this can be even another virtualenv's bin/python.
$ virtualenv -p /path/to/any/bin/python new_env
Alternatively, I think you could use the specific version of Python itself to create the virtual environment. That way, you'll know for sure it's the correct version:
$ python3.5 -m venv test35
$ ./test35/bin/python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build ) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Reference at https://docs.python.org/3.5/library/venv.html
As of version 3.3, python includes a package named venv. However that package doesn't provide the same functionalities as the traditional virtualenv package.
venv allows creating virtual environments only for the version of python it's installed for.
virtualenv allows creating virtual environments for different versions of python by providing the path to the binary.
Creating virtual envs for different versions of python:
So assuming one has python 2.7 and python 3.6 installed in /path/to/ and wants to create the virtual env named respectively env-py36 with python 3.6 and env-py27 with python 2.7
# create a virtual env with python3's venv :
/path/to/python36/bin/python3 -m venv /my/python-venvs/env-py36
. /my/python-venvs/env-py36/bin/activate
# we're now running python 3's "env-py36" virtual env, we want to install the "virtualenv" package
pip install virtualenv
deactivate
# now use virtualenv to create a virtual environment for python 2.7
/my/python-venvs/env-py36/bin/virtualenv --python=/path/to/python27/bin/python /my/python-venvs/env-py27
Using python 3.3+ venv
Python 3.3+ :
/path/to/python3/bin/python3 -m venv ENV_DIR
Python 3.3 to 3.5 (deprecated in 3.6+) :
/path/to/python3/bin/pyvenv ENV_DIR
Sources:
Creating Virtual Environments
Python 3.3 venv
Python virtualenv package
I working on all ubuntu and MacOS
Ubuntu : virtualenv -p python3.6 environment_file
Mac OS : virtualenv -p python3.6 environment_file
I think it be same
I had this issue (and came here) but under Windows. Python 3.9 was installed on one system but it had issues with code developed under 3.7. I wanted to use a virtual environment to downgrade to 3.7 to help debug the issue. Using Python Launcher for Windows:
py -3.7 -m venv my_env
in the python project folder did the trick for me.
Simple and direct solution:
Just see this video (https://www.youtube.com/watch?v=hC9FBQnOv6o) and follow the python setup download instructions of a particular python version and then use virtualenv <folder_name> -p /python.exe
This command is also shown in the video too.
In Linux:
Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is (not to downgrade) to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)
Steps:
Install python 3.7 and it’s virtual environment packages.
sudo apt-get install python3.7-dev python3.7-venv
Find out where your python 3.7 is located by this command:
which python3.7 (Should be something like /usr/bin/python3.7)
Create Virtual Environment in the Home directory.
cd
mkdir virtual_env
/usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7
source ~/virtual_env/venv_with_python3.7/bin/activate
python --version (Should be python 3.7 now)
Done. Python 3.7 can be used in this virtual environment. Type which python, you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.
Run deactivate when you need to deactivate.
Using anaconda we can create a virtual environment called "py35_env" with Python 3.5 version by running:
conda create --name py35_env python=3.5
I'm trying to set up a Django environment to work on a website project for my Python group. We're using 2.7 for the project, but when I followed this guide https://www.howtoforge.com/tutorial/django-install-ubuntu-14.04/ it only set it up for 3.4. How can I differentiate which set of setup tools it installs?
Here is my pip version when I type --version
~ $ pip --version
pip 1.5.6 from /usr/local/lib/python3.4/dist-packages/pip-1.5.6-py3.4.egg (python 3.4)
you can point to python executable via -p:
virtualenv -p /usr/bin/python2.7 <path/to/new/virtualenv/>
while creating your new virtualenv.
My Mac comes installed with python 2.7.2 in /usr/bin. Yet for our project I need to use python 2.7.6. So I did a brew install python.
Now python 2.7.6 is installed in /usr/local/bin/python. Yet when I create a new virtualenv it still creates one with the 2.7.2 python version installed in /usr/bin.
How can I tell virtualenv to create a virtual environment with the 2.7.6 version?
Any Ideas?
Use this flag when creating the env:
$ virtualenv --help
Usage: virtualenv [OPTIONS] DEST_DIR
Options:
...
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
(/usr/bin/python)
I have installed manually python (2.7.3). Whoc do I update the rpm version
usr/bin/python -V:
Python 2.7.3
rpm -qf /usr/bin/python:
python-2.6.5-3.el6.x86_64
any suggestions?
linux version: RH6.3
You installed it incorrectly. Instead of make install you should run make altinstall. This will install the new version of Python parallel to existing versions, and create a new executable in $PREFIX/bin with the name of python followed by the minor version of Python installed, e.g. python2.7.
Create a symlink in /usr/bin/ called python2.7, point to to where you have installed the new Python and use that.
Do not attempt to upgrade or force the default python on a redhat box, because a lot of other tools will stop working.