I am starting to learn Python on my Mac and use VS Code. At the bottom left corner I can select the Python version. Apparently I have four versions installed.
I know that macOS already has one installed (I am assuming it is the one in /System/Library/..). I must have (accidentally) installed the other versions.
What is the safe/recommended way to set up Python3 on Mac for development (I only want/need one version 3.xx)?
Where should I install pip3?
I don't want to mess around with the system's python version, but delete everything unnecessary.
Thank you for advice!
Do not delete python2.7 as it may be used by system. Other than that, whenever you want to create any project in python is recommended to create a virtual environment, this way you can always separate what version will every project use and what libraries will you install for a specific library. More infor https://docs.python.org/3/tutorial/venv.html and how to uninstall python from mac https://osxuninstaller.com/uninstall-guides/properly-uninstall-python-mac/
I have a variety of different Python versions installed on my Windows system- a 2.7 version, a 3.5 version, and a 3.6 version (there are a bunch of different packages that only work with one version, or are too buggy in the 3.6 version, etc.).
Long story short, I'm trying to keep my all my pips and python.exes in order. I've added my C:/Python35 and C:/Python36 and their Scripts folders to my path, but I also want to make sure that I am using the right pip from my command line (for example, I don't want to pip install pyinstaller to the 3.6 version, since Python 3.6 doesn't play well with pyinstaller as of right now.
I see that inside my Python3x/Scripts/ folder, there are three different pips available: pip, pip3.5, and pip3.
So whenever I want to install a module for 3.5, I plan to issue the following command pip3.5 install package_name. Whenever I want to install something for 3.6, I'd use pip or pip3. Seems like a decent enough plan to me.
However, can anyone confirm if the three pips are all the same executable? If so, I'd like to delete pip and pip3 so that I don't accidentally confuse it with my Python 3.6 pip- is this acceptable practice or am I missing something? This SO post provides some insights but doesn't explain why there's multiple pips in the same folder. Also, why are three separate pips provided? Is it simply for convenience from the command line?
Within the same python installation all the different pip files you find should be the same executable, there is the multiple versions simply to help keep everything in order if there are multiple installs of python on a single computer.
I personally only have the main version of python I use for development set to my PATH variable on my windows laptop and then if I need to do anything to a different python I instead link directly to the necessary file with something like C:\Python36\Scripts\pip3 install natsort but that is simply personal preference and my way of organizing.
If you do have them all on path you can then simply call out pip3.6 install <package name> or whatever python version you are using
The difference between them is that each one install the package in its own folder, for example if i type pip install Django, it will be placed for python 2 version, but is a little bit complex when you have multiple version of python3 like you showed, the solution: Don't delete the files and makevirtualenv when you're working, that avoid problems.
This prevents dependency issues with different versions of Python. You also check out virtualenvwrapper which is a convenient way to manage your virtual environments
If you want to manage the version with virtualenv
virtualenv python2_project -p usr/bin/python2.7
virtualenv p35_project -p usr/bin/python3.5
virtualenv p3x_project -p usr/bin/python3.x
I'm new to python. I installed python3.4 on OsX some time ago and now I installed python3.5 using the installer you can download from the site.
I noticed that in /Library/Frameworks/Python.framework/Versions/ I have both 3.4 and 3.5. I wasn't expecting that - I was expecting an upgrade where 3.5 replaced 3.4
So, if I run python3.5 and I try to import the packages I installed when using 3.4, they are not found. Furthermore if I use pip install to reinstall them, it says the packages are already installed, therefore I can see that it's pointing to the 3.4 version.
What I'm doing wrong? I supposed that installing the new python should upgrade my existing installation (bringing installed packages with it) rather than add a completely new install.
I'm not sure what to do now:
Should I keep every old version?
Should I manually change which pip
is used every time?
(is there a more streamlined update procedure
for next time?)
A lot of Python packages are 3rd party. The community is always moving forward and this may take some getting used to!
That said, my recommendation is to start using venv. It gives you (mostly) isolated Python virtual environments in which you can install whatever packages you like (via pip) without polluting the global installation. This also allows you to configure various virtual environments with varying packages and versions. It's really handy!
Link:
https://docs.python.org/3.4/library/venv.html
When I use pip install matplotlib --upgrade it runs and installs matplotlib and all dependicies, and at the end reads uninstalled version '1.3.1'. Running python and matplotlib.__version__ outputs '1.3.1' again, even though it was just declared uninstalled. How do I switch which python I'm running from, which would hopefully be where pip is installing and updating packages?
Edit: Running Mac OS 10.10.1, using python from bash.
which pip returns /usr/local/bin/pip and which -a python returns
/opt/local/bin/python
/opt/local/bin/python
/Library/Frameworks/Python.framework/Versions/Current/bin/python
/Library/Frameworks/Python.framework/Versions/Current/bin/python
/Library/Frameworks/Python.framework/Versions/Current/bin/python
/Library/Frameworks/Python.framework/Versions/2.6/bin/python
/usr/bin/python
which I'm assuming is the problem. I've deleted 2.6 before, but my Mac uses it and I had to restore my OS. How do I switch amongst them.
Your problem appears to be that you have two or more Python 2.7 installations, and the first python on the PATH comes from one installation (the MacPorts version, at /opt/local/bin/python), while the first pip on the PATH comes from a different one (you haven't said which, but I'd guess it's /usr/local/bin/pip, and it's attached to either the pre-installed Apple Python or yet another Python).
If you want to have lots of Pythons installed all over the filesystem and switch among them without using virtualenv, you just need to write some scripts with modify your PATH. (And your PYTHONPATH, if you use one… but hopefully you don't.)
But I wouldn't recommend that. If you want to use the MacPorts Python:
Make sure you have the MacPorts pip installed (it should be in /opt/local/bin/pip). I don't know if MacPorts has a separate python-pip package, or expects you to install it manually, or otherwise.
If you don't know how to rehash your shell cache, just open a new Terminal window after getting the MacPorts pip installed. Or, to be safe, just explicitly use /opt/local/bin/pip until the next time you logout and back in.
When you write scripts, you may want to #!/opt/local/bin/python instead of #!/usr/bin/env python.
Uninstall every other Python except the MacPorts one and the pre-installed Apple ones.
Any packages you accidentally installed for other Python installations, just reinstall them for MacPorts. Also, you should usually check for ports first, like port install python-ipython or port install python27-ipython, and only fall back to pip if there is no portfile.
As a side note, many people who have this problem also have MacPorts colliding with Homebrew (or sometimes Fink, gentoo-alt, or other package managers, but none of them are very common nowadays). If you're using two such package managers, you really, really shouldn't be unless you know what you're doing. I'd disable one entirely (at least take it off your PATH, if not uninstall it). You will likely have to rebuild a bunch of stuff after you do so, but it's better than having a bunch of stuff that kind of sometimes works…
A colleague of mine wants to use my python 2 code on his OS X (10.6) machine. My code imports several built-in python packages, including Tkinter and shelve, and also uses third-party packages, including numpy, scipy, matplotlib, and ipython.
I've encountered a few problems with OS X's built-in python. (IDLE doesn't work, for example*). I suspect I should install a more recent version of python, and a different version of Tk.
My questions:
Will having two different versions of python/Tk on the same machine cause problems?
I would like to associate the terminal commands 'python', 'ipython', and 'easy_install' with the more recent version of python. How should I do this?
When I install third-party packages like numpy using a .dmg file, how do I control which version of python numpy installs into?
Is there a better way to do this?
If this process goes well, I'd consider adding OS X instructions to my code's documentation, so I'd like to boil down this process to the simplest, most general approach.
*EDIT: Also, this
EDIT: Thank you everyone for the useful answers. My colleague tried MacPorts, which seems to work well, but has a few speedbumps. First we had to install Xcode from the system install disk. This is not a fast or lightweight install (several GB). Luckily we still had the disk! Once Xcode was installed, MacPorts was easy to install. Python and the python subpackages we needed were also easy to install, but he told me this installation took several hours. Presumably this delay is due to compilation? He had an easy time setting the MacPorts python as default. However, I think we have to change the 'Python Launcher' application by hand, this seems to still default to the system python.
Even though he has a working system now, I'm tempted to ask him to try one of the other solutions. I'm not sure all of my code's potential users will tolerate a multi-hour, multi-gigabyte installation.
I use brew to install all my libraries/compilers/interpreters.
To install python try this:
brew install python
Then add Python's binaries directory to your $PATH in your ~/.profile:
export PATH=`brew --prefix python`/bin:$PATH
I'd recommend you to install pip, virtualenv and virtualenvwrapper to have better control over your environment too.
Have you tried ActivePython?
It includes a package manager (PyPM) that, by default, installs into your home directory (eg: ~/Library/Python/2.7). Main scripts get symlinked in /usr/local/bin; use the included pythonselect to set the active Python version.
You don't have to bother installing .dmg packages, as PyPM is a binary package manager ... therefore you can install non-pure Python packages like NumPy without having to compile things yourself.
ActivePython can use Apple's Tcl/Tk or, if installed, ActiveTcl.
A "simplest, most general approach" in your documentation could be:
Install ActivePython 2.7
Open Terminal and type pypm-2.7 install matplotlib ipython
Using MacPorts, you can install python 2.6, 2.7, 3.1 and 3.2 at the same time, with their own packages, without ever touching the built-in python.
numpy, scipy, matplotlib, and ipython are also available as ports for most of those python versions.
Moreover, if you install the python_select port, you'll be able:
to choose which one of those (plus the built-in python) is the "default" python;
to install python packages through easy_install/pip for the "selected" python, if they're not available as ports.
Add virtualenv to the mix, and you'll have a very, very flexible Python development environment.
As for your questions:
Q1: with MacPorts, no. while not a frequent user, I've installed and used matplotlib in 2.6 and 2.7, switching between the two using python_select.
Q2: easy_install, pip, ipython will be "linked" to the python they were installed by. (but see tip 1)
Q3: it's easier to install one of the py{26,27,xx}-numpy ports, or pip install numpy under your python_select'ed python.
Q4: well, MacPorts is the best thing I know after APT on Debian/Ubuntu... :-)
Now, two tips if you try MacPorts:
MacPorts cleanly installs ports separately from the OS X installation, in an /opt/local directory, and each python version is installed in a /opt/local/Library/Frameworks/Python.framework/Versions/{2.5,2.6,2.7,...} directory. Using python_select cleanly switch the "python" command using links. BUT... the Versions/{2.5,2.6,2.7,...}/bin directory, where python scripts are installed, is not added to the PATH. Just adding: export PATH=/opt/local/Library/Frameworks/Python.framework/Versions/Current/bin:$PATH to your ~/.profile will always give you direct access to the scripts installed for the selected python.
to avoid bad surprises, I've added a echo Selected python is \"$(python_select -s)\" line to my ~/.profile, so I always know which is my currently selected python when opening a session... :-)
Regards,
Georges
In almost all cases, the best python to use is the one from http://python.org/. It sets up the paths correctly and doesn't overwrite anything. DMG package installs usually work automatically, as does python setup.py install, and it's not too hard to get setuptools to work. If you want per-user installs, it is easy to set up .pydistutils.cfg and python automatically recognizes the path install_lib = ~/Library/Python/$py_version_short/site-packages
An addendum regarding the usage of brew:
Since some time, brew install python will install python3.
If you intend to install python2, you want to use
brew install python#2
It is perfectly fine to install both python and python3 using brew!
Here is an old post that answers your questions too.
In general it is not a problem at all to have more than one python installation on your machine. You just have to watch out which one you are calling on the command line.
>> which python
... helps to identify where your python binary is located. The original Mac OS X python is usually at "/usr/bin/python"
I personally use the MacPorts python installation. It also supports you with the installation of modules. (see link above)
I have 4 versions of python on my MacBook Pro. 2 from the original install of OS X 10.6 and a subsequent update, then self installed copies of python 2.7 and 3.2. You can update the python command to point at any of the versions. They all install in separate directories and cause no problems with each other.
I'm not sure what will happen when you install from a .dmg file. I believe it will simply use whatever version python points to.
This post on superuser.com answers your questions on changing default paths.