Installing Python 2 and 3 separately - python

Is there a way to install python 2 and python 3 on Mac OSX together and be able to switch back and forth? I use Python 2 and 3 for development and I used to be able to do this virtualenv thing where I do source deactivate the development environment. But I wasn't sure how I set that up and it started to get very confusing for the compiler because I apparently installed some modules in one place and it works on python 2 but not 3 (or vice versa) and things are just all over the place. So I am wiping out my whole laptop and this time I want to do this installation business right. Is there any way I can switch back and forth python 2 and 3 with the compiler detecting just one set of library?

Virtualenv is the default answer to that and it's explained well throughout their pages and across the web. An alternative, which does not depend on the shell, is its "reincarnation" as a Python module, pew, the Python Env Wrapper.
Both will allow you to have separate interpreters, even for entirely different version numbers.

Firstly, install homebrew - it's the fundamental tool for managing installation on your Mac.
Now, using homebrew, install separate versions of Python and and Python 3:
$ brew install python
...
$ brew install python3
This should automatically install the relevant versions of pip for each version of Python as well as virtualenv. So you can now create a virtual environment for a Python 2 project:
$ virtualenv --python=python2.7 my_project
and for a Python 3 one:
$ virtualenv --python=python3 my_py3_project
and activate/deactivate them as you need.

Related

What happens if I use update-alternatives to manage python versions instead of virtualenv or pyenv?

I know that there are somethings like virtualenv and pyenv to manage python versions in a single machine. But what happens if I change the default python version using update-alternatives? AFAIK, it will make system-wise change while virtualenv or pyenv can manage version for each project (or directory). Then when I only works for a single project at once, is it OK to simply use update-alternatives instead? Since somethings like pip are run as python -m pip ... so it seems safe for me to manage pip packages in this way also. What should I consider further when I only works for one project at once?
OK. Now I understand what I misunderstood.
By using
update-alternatives, I can change the default python version.
pyenv or virtualenv, I can isolate environments of projects.
Let's assume that I have project A, B for python3.7 and C for python3.8.
And Project A and B require pip package X-1.0 and X-1.3, respectively.
In that case I can use pyenv or virtualenv, but update-alternatives cannot handle the situation though it can change python version to 3.7 (and load corresponding packages for it) for project A and B.

a couple Python versioning questions (touching Homebrew and venv)

I read the Homebrew notes on Python but I'm still a bit confused based on a couple specific situations described below. I'm also starting to use venv and a bit confused how that works. So here are a couple questions regarding how both of those handle Python versioning. Thanks in advance.
brew list shows both python3 and python. This is a bit confusing to me because I have also read that Python 2 is referred to as python#2, and I don't think I have Python 2 installed. So I'm wondering why there are 2 different formulae which both seem to mean "Python 3". (EDIT: I might be able to answer this question myself, I just found another mention that python3 is simply an alias to python: Homebrew - What's the difference between `python#3` and `python3`?)
I used the following command to create a virtual environment in a folder: python3 -m venv venv. Now within ./venv/bin I have the files in the below screenshot.
I am basically wondering why I have 2 files for Python and 3 files for pip in this virtual environment (the Python files are just aliases, but the pip files are actual executables from what I can tell). From running the version commands (python --version, python3 --version, pip -V, pip3 -V) I can see they are the same version, so I am wondering why I got multiple files.
Is this just for convenience? Does it matter which one I use? And why specifically include a file pip3.7 but not do the same thing for Python? Does this have anything to do with how I'm using Homebrew or is this just a venv thing?
I believe it's just for convenience. On some OSes, like MacOS, where you have both python ( Python 2 ) and python3 ( Python 3 ) having both python and python3 in a venv is nice if you've already written scripts that do something like #!/usr/bin/env python or #!/usr/bin/env python3 as they'll "just work" with the new venv environment. The same holds true for the different pip too.

How can I manage mutiple python in Ubuntu16.04?

  In my Ubuntu16.04, there are python 2 and python 3 default. In addition, i have installed anaconda too. I am sucked by the 'python' cmd. Every time i use pip or pip3 install, I don't know where the package install, python2 or python 3? And I use conda install to install anaconda package. I also use anaconda env to manage different virtual env. But I think it mix with my local Python 2 and 3.
  For example, in directory /usr/bin, I found many soft links like this:
   When i try 'python' cmd, it just confuse me!
   Why python3m are local, shouldn't it be anaconda? Why python3 are anaconda, shouldn't it be local? Then I found that if I use ./python2 or ./python3, I found it is correct now!
  So I know it is caused by environment variables. I echo $PATH, Found it like this: /home/kinny/.pyenv/shims:/home/kinny/.pyenv/bin:/home/kinny/anaconda3/bin:/home/kinny/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/ant/bin:/snap/bin:/opt/maven/bin:/usr/lib/jvm/java-8-oracle/bin
   I have used update-alternative --config python to configure default python, but it doesn't work! It sames mixed with each others.
   Now I just want to install tensorflow 0.11 in local python3, because in anaconda it is 0.10 version by default. So how can I change this. I just want to use python python3 and python3m represents python2.7 python3.5 and anaconda python respectively, How can I do that! use pip and pip3 for local python2 and python3 respectively!
I ran into a similiar problem when setting up PyCharm Edu to work with Anaconda. I found that I had several versions of Python installed and it was very hard to keep track of which version the IDE was referencing. My CS professor gave me the advice of simply removing the versions of Python I didn't frequent. I now just have Anaconda installed; and use the Anaconda Prompt as my Python console. I also rely on PyCharm's IPython for the developer console. However, if you still want differing versions of Python installed (say your doing QA testing for older devices); there is the really helpful command: which python. When entered into the python console or Anaconda Prompt: which python will display the directory associated with the currently executing Python Shell. This enables you to better keep track of to what particular python.exe the current window is referring to.
Follow up to the comments mentioning using virtualenv and virtualenvwrapper.
Here are the official docs and a good blog post to follow for getting started using virtualenv's is here:
https://virtualenv.pypa.io/en/stable/installation/
http://virtualenvwrapper.readthedocs.io/en/latest/install.html
http://exponential.io/blog/2015/02/10/install-virtualenv-and-virtualenvwrapper-on-ubuntu/
Also, once you are setup you can create virtualenv's specifying which python installation you want to use.
which python3
returns
/usr/bin/python3
Then create a virtualenv with that python path. Where example_env is the name of the virtualenv.
mkvirtualenv -p /usr/bin/python3 example_env
Then activate the virtualenv using virtualenvwrapper.
workon example_env
Finally, install tensorflow and other dependencies with pip.
pip install tensorflow
the which command is very useful for finding the path to the executable that is first in your path. Zsh also has the where command, which will show you all instances of the given executable that show up in your path. For managing different python versions, you have a lot of options. The easiest for most people tends to be anaconda, using conda environments. The installer will ask you to add some stuff to your .bashrc file, which will then make anaconda's binaries come first in your path. Anything else you run after the .bashrc gets sourced after that, will then use that first, including PyCharm. For graphical desktop apps to pick up the change, you may need to log out and back in again. If you only need one version each of python 2 and python 3, you can just use the ones available via apt. Depending on your Ubuntu version, Python 2 is definitely installed by default as it is used by many system utilities, including apt itself. Some newer versions may also install python 3 by default, but I do not remember for sure. Another option is to install the versions of python you need in an alternate location, such as /opt/python/<version> and then using environment-modules (installed via apt install environment-modules) or Lmod to control which versions are being used, but that may or may not be easy/convenient to use with a desktop application such as PyCharm.
for TensorFlow, 1.11 is available in anaconda, but I don't remember if it's in the default channel or not.

How to get two versions of Python to coexist within a virtualenv?

In order to not alter the default python installation, I have a virtualenv set up that automatically runs on startup. I've also set up my profile so that unless a virtualenv is running, pip will not run. I've been using python2.x up to now, and have now installed python3 via homebrew. When I attempt to install a new package via pip3, I get the following error: Could not find an activated virtualenv (required).. However, when I check for the existence of a virtual env by checking the $VIRTUAL_ENV variable, it tells me that a virtual env is in fact running: -bash: /Users/me/virtualenvs/r: is a directory. Do I need to create separate instances of virtualenvs for each version of python? Why will one of my versions of python recognize the virtualenv, while the other will not?
All your virtualenvs are stored in a directory -- /Users/me/virtualenvs in your case. If you go there, you'll notice that it executes totally separate python interpreters in each virtualenv. For example, this is a freshly created virtualenv:
I'd imagine that running pip3 while on the python2.7 virtualenv wouldn't work for that reason.
It seems that there are two different kinds of virtualenvs, one of which is compatible with python2 and one of which is compatible with python3. I'm not sure whether there is a way to get one that is compatible with both, but I went ahead and created a new virtualenv using python 3 (python3 -m venv myPython3Env), and have been able to successfully use that.

Python Modules Only Installing For Python 2

I'm fairly new to python and want to start doing some more advanced programming in python 3. I installed some modules using pip on the terminal (I'm using a mac) only to find out that the modules only installed for python 2. I think that it's because I only installed it to the python 2 path, which I think is because my system is running python 2 by default.
But I have no idea how to get around this. Any ideas?
You need to use pip3. OS X will default to Python 2 otherwise.
When creating your virtual environment (you are using a virtual environment, right?) use pyvenv <foo> instead of virtualenv <foo>, and that will create a Python 3 virtual environment, free of Python 2. Then you are free to use pip and it will install the modules into that venv.

Categories