Managing Python Versions - python

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.

Related

Python versions and pyenv

I installed python3 using homebrew, but I stumbled upon many posts mentioning that it is better to use pyenv to manage different python versions. However, I have not found a post that answers my question:
Should I uninstall python3, install pyenv, and then reinstall python3 using pyenv? Or should I just install pyenv now, would this work?
Below is output from terminal:
System python
python --version
Python 2.7.18
which python
/usr/bin/python
Python3
python3 --version
Python 3.9.10
which python3
/opt/homebrew/bin/python3
From https://justinmayer.com/posts/homebrew-python-is-not-for-you/
Homebrew’s Python is not for you. It exists to serve Homebrew, or more accurately, Homebrew’s other formulae. The primary purpose of Homebrew’s Python formula is to enable other Python-dependent Homebrew packages to work. If installing Homebrew’s Python allows you to run the occasional Python script or access the REPL, that’s a nice side benefit, but anything beyond that — including developing software with Python — is squarely out-of-scope.
You can run brew uses --installed python to check if any installed packages depend on Homebrew Python. If no packages are listed, then you can safely uninstall via brew uninstall python and use pyenv instead.

How to get rid of multiple versions of python and get a newer one?

I want to update my python to the 3.8 version at least. However, it looks like I have multiple different versions of pythno3 in my Mac (Mac OS Catalina 10.15.7). When I install the newer version, looks like it doesn't overwrite the past versions. Here, the advise was to remove python3.7 from the Application folder and install the newer version from the python website. I did that, but it didn't work and now I have this
python --version
Python 3.7.4
type -a python3
python3 is
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
python3 is /usr/local/bin/python3
python3 is /usr/bin/python3
How do I remove all the 3.7 versions that I have and get the 3.8 or 3.9 one?
Install Homebrew - https://brew.sh/
Then run "brew install python#3.9" in your terminal

Pyenv listed version not found

I have Python 3.9.6 installed on my machine, along with 3.6.5, the two of which I use for different projects. At least that is the idea. However, when I run the command pyenv global 3.9.6, I get the response 3.9.6 not installed,
I do find python#3.9/3.9.6 in my Library folder. What's going on here, what's a good way to switch environments please?
Try to use pyenv versions in order to identify the Versions Installed.
If it is not installed you can use pyenv install.
To ask your question:
-Use pyenv install -l in order to see the curent available version that can be installed. Python 3.9.6 it is not one of them!
You did not install with pyenv Python 3.9.6, it cannot be installed with pyenv. That is the reason you cannot use it.

Install library packages for other python version

I have installed the two python versions v2.7 and v3.7 on the same machine. I want to install the some packages for Python 3.7 when i am trying to install it installing for python2.7. Can any one help here how can i install the packages for python3.7.
Installed /usr/lib/python2.7/site-packages/configlib-2.0.4-py2.7.egg
Processing dependencies for configlib==2.0.4
Searching for everett==0.9
Reading https://pypi.python.org/simple/everett/
[root#nodehost configlib-2.0.4]# /usr/local/lib/python3.7 setup.py install
bash: /usr/local/lib/python3.7: Is a directory
[root#nodehost configlib-2.0.4]#
A more elegant solution is to create a separate environment for different python. Anaconda is the way to go. It is free and it installs many everyday libraries already.
Download and install the python 3.7 version. And then create a python 2 version is simple:
conda create -n py27 python=2.7
Every time you need the 2.7, just
conda activate py27
And then you can install libraries there just for 2.7, without messing up your 3.7. Vice versa for 3.7.
You should be able to choose your Python version with the two following commands:
>> python --version
Python 2.7
>> python3 --version
Python 3.7
If this works, just call pip like so:
>> python3 -m pip install everett

Why 'python3 -m venv myenv' installs older version of pip into myenv than any version of pip I can find anywhere on the system?

This is not causing me any problem that I can't solve by activating the virtual environment and running pip install -U pip, but I always wonder where the older version of pip is coming from.
I'm using OS X 10.7.5. When I create a virtual environment using either pyvenv-3.4 myenv or python3 -m venv myenv, the version of pip that is installed inside the virtual environment is 6.0.8, but I have upgraded my global pip to 6.1.1.
Here is a terminal session demonstrating what I mean:
$ python3 -m venv myenv
$ myenv/bin/pip -V
pip 6.0.8 from /Users/dust/Desktop/myenv/lib/python3.4/site-packages (python 3.4)
Here is what I would like to occur:
$ source myenv/bin/activate
(myenv)$ pip -V
UPDATED SYSTEM VERSION HERE WOULD BE NICE
I can't find a pip 6.0.8 anywhere else, other than what is created inside virtual environments.
Here are the outputs of various commands that I have use to try and figure this out:
$ which pip
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip
$ which pip3
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip3
$ pip -V
pip 6.1.1 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)
$ pip3 -V
pip 6.1.1 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)
I even tried using find:
$ find / -type f -name pip 2>&1 | awk '! /^f.*$/'
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip
/usr/local/bin/pip
$ find / -type f -name pip3 2>&1 | awk '! /^f.*$/'
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip3
I thought maybe that the /usr/local/bin/pip might have been the culprit, but no:
$ /usr/local/bin/pip -V
pip 6.1.1 from /Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg (python 2.7)
Hmm. Perhaps the OS X python has it?
$ /usr/bin/python
>>> import pip
>>> pip.__version__
'6.1.1'
6.1.1 is reported no matter which distribution of python I ask, whether it be OS X's 2.7.1, python.org's 2.7.9, or python.org's 3.4.3.
Is it possible (or advisable) to update the version of pip that gets put into a virtual environment?
I face the same issue, running OSX 10.10.2 and python 3.4.2. Most recently I created a virtual environment in a debian wheezy machine with python 3.4.3 and also ended up with an older version of pip than available. had to upgrade pip.
I've been upgrading pip within the virtual environment to 6.1.1 from 6.0.8 manually, because I'm o.c.d about software library versions that way - and yes, I am upgrading my python 3 version to 3.4.3 right now. Anyway, my system's python3-pip is the latest version 6.1.1, so I've also wondered why pyvenv creates a new virtual environment and loads it with old pip.
I haven't noticed anything bad happen in any of the virtual environments due to upgrading pip, (but on the flip side, I haven't noticed anything good either) Apparently the new pip is faster -- didn't notice, and outputs less junk on successful installs because user's don't care -- also didn't notice, probably because i'm one of those that don't care, and also comes with a state-of-the art coffee machine capable of latte art to boot!!! -- still waiting on sudo pip install latte to finish :(
So, to answer your question, it is definitely possible, and probably advisable to upgrade, because apparently the new pip fixes some bugs and goes faster, but I guess the speed up isn't that major, and the bug fixes don't affect all that many people (I've never faced a bug with my usage of the old pip).
You can link to system site-packages using the flag --system-site-packages when you create a new virtual environment, like this
pyvenv myenv --system-site-packages
This will link to your system wide version of pip, and would remove the annoyance that is manually upgrading pip on every virtual environment, but if you do this, then is your virtual environment all that virtual?
update: following my rant above, I went into the venv package's source to dig. pip is set up by a method called _setup_pip in the file __init__.py, line 248
def _setup_pip(self, context):
"""Installs or upgrades pip in a virtual environment"""
# We run ensurepip in isolated mode to avoid side effects from
# environment vars, the current directory and anything else
# intended for the global Python environment
cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade',
'--default-pip']
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
So, venv seems to be calling ensurepip from the shell using the subprocess module.
One more minute of google-fu gave me this from the documentation for ensurepip.
ensurepip.version()
Returns a string specifying the bundled version of pip that will be installed when bootstrapping an environment.
So, from the command line, the following code:
$ python3 -c 'import ensurepip; print(ensurepip.version())'
6.0.8
displays my current pip that will be bootstrapped with ensurepip.
I guess we're stuck with the old version of pip for every new install until ensurepip gets upgraded, as I can't find a way to upgrade the version of pip that comes with ensurepip
Newer
If you want to "hotpatch" your installed python, just modify the versions listed in ensurepip/__init__.py and replace the two files in ensurepip/_bundled. You can find this location by running find * | grep ensurepip from the directory where python is installed. On macOS with Homebrew, this is the location: /usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ensurepip
You will also want to delete the ensurepip/__pycache__ directory that contains the .pyc files.
My older, build-time fix:
You are able to update the bundled versions of pip and setuptools by patching Python before building it from source. The following patch will update the bundled versions of pip and setuptools to the current available today. You will want to invoke configure with the following option: --with-ensurepip=upgrade
Those whl files are downloaded from PYPI here:
https://pypi.org/project/pip/#files
https://pypi.org/project/setuptools/#files
diff -ru Python-3.7.1/Lib/ensurepip/__init__.py Python-3.7.1.new/Lib/ensurepip/__init__.py
--- Python-3.7.1/Lib/ensurepip/__init__.py 2018-10-20 06:04:19.000000000 +0000
+++ Python-3.7.1.new/Lib/ensurepip/__init__.py 2018-11-27 02:36:19.301655008 +0000
## -8,9 +8,9 ##
__all__ = ["version", "bootstrap"]
-_SETUPTOOLS_VERSION = "39.0.1"
+_SETUPTOOLS_VERSION = "40.6.2"
-_PIP_VERSION = "10.0.1"
+_PIP_VERSION = "18.1"
_PROJECTS = [
("setuptools", _SETUPTOOLS_VERSION),
Only in Python-3.7.1/Lib/ensurepip/_bundled: pip-10.0.1-py2.py3-none-any.whl
Only in Python-3.7.1.new/Lib/ensurepip/_bundled: pip-18.1-py2.py3-none-any.whl
Only in Python-3.7.1/Lib/ensurepip/_bundled: setuptools-39.0.1-py2.py3-none-any.whl
Only in Python-3.7.1.new/Lib/ensurepip/_bundled: setuptools-40.6.2-py2.py3-none-any.whl
If you're lucky, you can bump it up with a Python patch.
Is your Python outdated? This is using macports, but any of brew, apt should do.
port outdated
python36 3.6.9 < 3.6.10
Let's fix that:
sudo port upgrade python36
And now... drumroll, in my newly-recreated venv, it seems that the Python patch has brought along the latest pip.
pip --version shows 20.0.2, it was at 18 previously and that's precisely what it was complaining about in the venv.
This is after spending a ludicrously long amount of time trying to figure out how to update my user-level Python 3.6 version of pip, on a mac where, of course, if you quit the virtualenv you are at Python 2.7 and where Python 3.6, installed through macports, doesn't seem to have a pip command to work with, until you build a venv.
From looking at /opt/local/Library/Frameworks/Python.framework/Versions/3.6, where macports puts things, I suspect Utknonos is on to something, so +1.
There really should be something a bit more obvious, like a pip-system-selfupdate command or a pip36 that comes along with python36. Especially considering that a search of pip upgrade will return tons of hits on anything but this subject.
Note: after my update, even though Python has been updated
(venv)$ pwd
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
(venv)$ grep PIP_VERSION ensurepip/__init__.py
_PIP_VERSION = "18.1"
(venv) $python --version
Python 3.6.10
(venv) $pip --version
pip 20.0.2 from .../venv/lib/python3.6/site-packages/pip (python 3.6)

Categories