a couple Python versioning questions (touching Homebrew and venv) - python

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.

Related

Installed Python 3.7 and tried changing that to the default however, changing $PATH and running python --version still shows 2.7

Sorry if the question is simple or I'm missing something obvious but I'm fairly new with console commands and Python.
As the title suggests, I installed Homebrew and subsequently the latest version of Python. I want to make this the default when running python --version in bash.
I tried changing the path in my bash_profile with the following line at the bottom:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
It seems that when I install new modules however, that they are for 2.7 and not 3. Checking pip --version going straight to the default:
pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
I also tried running the following commands:
echo "export Path=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
this does edit my bash_profile again, but the line I wanted (and added manually) isn't the one that appears; instead the following is inserted (I have no idea why Applications/VMware is inserted at the end, it has nothing to do with Python or what I'm trying to do):
export PATH=/usr/local/bin:/usr/local/sbin:$PATHexport Path=/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware
Can anyone explain to me how I can make python3 the default. I don't want to get rid of 2.7 obviously because a lot of scripts for mac still depend on it. Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
You really don't want to change this.
First:
Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
Yes! Install pipenv, and set up an environment where 3.7 is the default, but leave the system default at 2.7.
More generally:
According to PEP 394, which isn't due to be reevaluated until 2020, if you don't have a virtual environment activated:
python3 runs Python 3.x
python2 runs Python 2.x
python usually shouldn't be used—but if you do use it, it should run Python 2.x.
Usually, you'll also have, e.g., python3.7 and python2.7 to run specific 3.x and 2.x versions, although this isn't covered by any standard.
Scripts like pip, and those installed by pip, should be installed with similar suffixes—but ideally, if you have multiple versions, you shouldn't run those scripts. Most of them are designed to be run as modules, so you can run python3 -m pip to run the pip for your 3.x.
For a few things, it may be handy to set up shortcuts. (For example, instead of running python3 -m ipython from the command line, I usually use an iTerm profile that uses that instead of bash as my start command. If you don't use either IPython or iTerm, you can make a Terminal.app profile that runs python3.)
Scripts installed as part of your platform's package manager are a whole more complicated story, but macOS doesn't have an official package manager, and Homebrew generally expects you to install Python libraries with pip, not brew, so you don't have to worry about that.
Programs should have an installer—whether they install via pip or something like py2app—that creates a proper shbang line, so they run with whichever Python version they were installed with, so you don't have to worry about them. But programs that come with your OS may use something like /usr/bin/env python, and they will expect that to run the version of Python that came with your OS.
For development purposes, you usually want to use virtualenv or pipenv or conda so that python runs the Python interpreter for whichever environment is currently activated.
So, don't try to change the fact that python runs Apple's Python 2.7. That's what it's supposed to do, and if you make it do otherwise, you could break things. Use python3, python3 -m pip, etc.; use #!/usr/bin/env python3 on your scripts (or, better, use setuptools to create entry-point scripts automatically); use virtual environments; just never run python, and everything will work properly.
You just need to run command
$which python
And then goto the path rename python to python2
Then run command
$which python3
To get the path of python3
Just link with command
sudo ln -s $python3path /usr/bin
/python
You need to instead $python3path with real path
Then you can run python3 with python command

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.

setting/changing PYTHONPATH in OS Mavericks

Ok, I hate to start another of seemingly hundreds of other threads about this, but how do I set or tweak the PYTHONPATH? I'm very new to Mac and the only other time I've successfully done this was on Windows using Rapid Enviroment Editor. This looks like a whole different beast. I tried installing Django and it failed to do it for 2.7, while I need 3.4, so the current path must be for 2.7. Running which python in Terminal shows the /usr/bin/python directory, which I then can't cd into, let alone find by browsing. I see that my Python 3.4 directory has the Update Shell Profile file, but it has a lot of limitations. I also see other threads mention PYTHONPATH commands in IDLE and creating one of the bash profile type files for the Terminal. How can I set this and not worry about it anymore until I need to run a different version of Python? I'm on Mac 10.9.2.
"Explain like I'm five".
If you want to install packages for python 3.4 use: pip3 install django
When installing for python 2.7 just use: pip install django
To use python 3.4 type python3 in your shell.
To see where all installations of python are use: which -a python
Depending on how you installed the new versions of python you will see output like:
/usr/local/bin/python
/usr/bin/python
If you wanted to use the python in /usr/local/bin/python you can edit your .bashrc file and add export path="/usr/local/bin:$path".
Save, then type source .bashrc in your shell and when you type which python it will show something like /usr/local/bin/python
Don't screw around too much with different versions of python, you will end up causing yourself a lot of problems.
You should not have to change your PYTHONPATH, just specify which python or pip version you want to use and that will most likely be all you need to do.
To update PYTHONPATH you can run from the terminal:
export PYTHONPATH=$PYTHONPATH:/desired/path/to/add
Then to check the updated PYTHONPATH you can run:
echo $PYTHONPATH
I'm not sure if this completely answers your question, but this is one way to make sure modules are visible to python when you import them.

Difficulty with using virtualenv and specific Python version?

I'm trying to understand how to create an isolated python environment using an alternative version of Python other than the default (in my case this is Python 2.7). virtualenv works on my system with Python 2.7, but I can't seem to create a virtual environment with a version of Python 3.
I tried to specify the Python version as outlined in this this post, like this:
$ virtualenv -p /usr/bin/python3.2
Can anyone explain how I can create a virtualenv with a specific version of Python? Thanks very much.
I believe you need to install python 3.2 to the system first. You're telling the virtualenvironment to use a version of python that doesn't exist anywhere so it cannot find it. This is the "does not exist"
Edit after more info was given in comment:
virtualenv -p /usr/local/bin/python3.2

Python - Virtualenv , python 3?

Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even work with python 3.0? I downloaded it, and it doesn't seem to.. And I don't really understand how it works, Can you 'turn on' on env at a time or something? What I want is to leave the system python 2.5 (obviously), and to have python 3.1.1 with subversion pygame to write my own stuff, and python 2.6 with normal stable pygame to use to run other things, like pygame games downloaded from pygame.org. Any help on how to accomplish that? Thanks.
OK I realized virtualenv is not what I'm looking for.
It's an old question by now, but I found it myself on top of google search for the answer, and I don't think the answers provided are what people are looking for.
As I understand it you want to create different virtual environments with different Python versions?
This is very easy, and you only need virtualenv itself.
For, say, a Python 3:
$ virtualenv -p python3 p34env
(...)
New python executable in p34env/bin/python3.4
Also creating executable in p34env/bin/python
Installing setuptools, pip...done.
$ source p34env/bin/activate
(p34env)$ python -V
Python 3.4.2
(p34env)$ deactivate
$
You use the source command to activate the venv, and deactivate to - you guessed it - deactivate it. Notice the prompt changes to indicate the env.
For your system's standard version of Python you just skip the -p python3 argument, and you can use the argument to point to any version you want given a path.
The last argument is the name (p34env) and you can make as many as you like, just give them different names.
Your use case doesn't actually need virtualenv. You just need to install several different Python versions.
virtualenv is designed to create isolated environments of a Python environment. The trick to using it with multiple Python instances is to either install virtualenv into each of the Python versions you want to use it with, for example:
/usr/bin/easy_install-2.6 virtualenv
/usr/local/bin/easy_install virtualenv
sudo port install py26-virtualenv
or to invoke it with the intended Python version, for example:
/usr/bin/python2.6 virtualenv.py ENV
/usr/local/bin/python2.6 virtualenv.py ENV
/opt/local/bin/python2.5 virtualenv.py ENV
So, as such, it doesn't directly solve the problem (particularly acute on OS X) of which Python you want to work with. There are various ways to deal with that issue: use absolute paths to the intended Python (as in the above examples), define shell aliases, carefully manage the $PATH search order, among others.
At the moment, AFAIK, virtualenv is not supported with Python 3 because, among other things, setuptools (the magic behind easy_install) is not yet supported on Python 3, although there is work in progress towards a solution for that.
BTW, many people use Doug Hellman's virtualenvwrapper to simplify use of virtualenv.
For me virtualenv3 works very well. I also have pypi.python.org/pypi/distribute installed. That also works with the mentioned www.doughellmann.com/docs/virtualenvwrapper/ . I've only tested that on Linux though.
Not sure if I understood you correctly, but here goes :)
I don't know about OS X, but in Linux you can install both 2.6 and 3. Then you can either specify to use python25 or python3, or change the /usr/bin/python symlink to the version you want to use by default.

Categories