How to install python 2.7.4 on OSX Mountain Lion - python

I tried installing python 2.7.4 by overwriting the native Apple's installation of python 2.7 by following this link (wolfpaulus.com), and as a result both easy_install and pip no longer work.
This link (apple.com) suggests the native Apple bundle should never be deleted.
Is it possible to restore python 2.7 supplied by Apple without reinstalling OSX?
Is there a definitive guide to upgrading python to 2.7.4 on OSX Lion? For instance, before breaking my set up I could install bumpy, django into 2.7.4, but matplotlib refused to install to anything but 2.7, either from binaries or source.
Thanks.

It is absolutely not a good idea to replace Apples default python installation. If you have done, then you could try with the following command
" brew install python --framework "
R

You should never remove anything in /System/Library or /usr (other than /usr/local). Files installed in those locations are managed by Apple as part of OS X. At worst you can fatally damage your OS X system; at best you are at risk of all of your changes being wiped out by the next software update. In particular with Python, you’ve just wiped out Apple-supplied extensions to Python that are not available elsewhere. The right way to update a system component like Python is to install the new version at a different location, like the python.org Python installers do in /Library/Frameworks, and then manage access by modifying your shell PATH environment variable. Unfortunately, Apple does not make it easy to install individual components of OS X that have been mistakenly removed. To properly restore all of the system Python that you've deleted, the easiest approach is to reinstall OS X itself.
When you install another version of Python, you should also use it to install additional versions of pip or easy_install. Generally, each Python version should have its own version of each.

Related

The location of standard Python libraries?

It is stated in Python documentation here that:
By default, the libraries are searched in prefix/lib/pythonversion and exec_prefix/lib/pythonversion, where prefix and exec_prefix are installation-dependent directories, both defaulting to /usr/local.
But when I looked into my /usr/local directory, what I found was a folder named python2.7. I did install python3.5 not python2.7 on my device (MacBook mid 2012).
You could use sys module to find that.
import sys
print sys.path
Ouput like:
['', '/usr/local/lib/python2.7/dist-packages/mccabe-0.3.1-py2.7.egg', ...
Mac comes with a default Python 2.7 installation. Your installation did not remove python2.7. Probably also the reason why the system libraries are not under usr/local.
You never state how you attempted to install Python 3.5 onto your MacBook, depending on the installation options you used while building the Python source (or the bundle you downloaded) the install location could be different from the defaults. If you provide further details on your installation method, it will be easier to answer all aspects of your question.
As far as I can tell, Apple does not use the installation defaults to provide Python with OSX. On my OSX El Capitan machine, I can see the system Python library at /System/Library/Frameworks/Python.framework/Versions/2.7 and the binary at /usr/bin/python (Python 2.7.x comes preinstalled for all recent versions of OSX).
It looks like the easiest way to get Python 3.5 on Mac is to use MacPython which will install alongside the system version of Python (as you should not alter the default system version of Python or risk breaking OSX). It installs some helper applications into your Finder's Applications directory and installs the library files to /System/Library/Frameworks/Python.framework/Versions/3.5. Since you will have more then one version of Python installed, you would also need to put some effort into making sure your scripts are using the proper version.
If you have a /usr/local/python2.7 directory, that sounds like something that was previously installed by a user. If I remember correctly, OSX does not normally use /usr/local for system software.
Hope that helps

How can I install multiple copies of Python 2.7 on the same machine?

I want to install Python 2.7 in two places at once on my Windows machine. For example, one in c:\python27 and another in c:\myproduct\python27. The official installer refuses to let me do this. If there is already an installation when I run the installer, it prompts me and asks if I want to Change, Repair or Remove the existing installation.
The TARGETDIR trick mentioned elsewhere on Stackoverflow doesn't work either - I get the same result if I type the following into a cmd window:
msiexec /i python-2.7.8.msi TARGETDIR=c:\myproduct\python27
So, is there anyway to install Python 2.7 twice on my Windows machine?
Reasons I want to do this are:
My product requires a 32-bit version of Python 2.7 to be installed because it uses ctypes to load a 32-bit DLL. If the user already has a 64-bit version of 2.7 installed, I don't think it is safe to install the 32-bit version over the top.
Relying on the users pre-installed Python 2.7 is bad because they might uninstall it later. That would stop my product from working.
It reduces the complexity of testing my product if I can make its installer always install a known version of Python. I can rely on it being in a known state.
I'd like to add the pyreadline module to the Python install that is part of my product. I suspect most users would rather that I did not mess around with their Python install.
When a user uninstalls my product, the Python it installed should also be removed. If I installed Python in the standard place the user might come to use it for other purposes and be surprised when it goes missing when they uninstall my product.
It appears that you should be able to install the 32 bit version of Python 2.7 in another folder when the 64-bit version is already installed. See How do I install Python 2.7.3 32 bit and 64 bit on Windows side by side for a description. However it may be tricky to automate this, you might have to get users to install it themselves.
I would suggest you ask your users to install Python 2.7 32-bit, and give them instructions on how to do it if they already have a conflicting 64-bit version. Then in your installation you:
Ensure that virtualenv is installed (and install it if it is not).
Create a virtualenv in your application folder.
That addresses everything except the second item on your list and I think you simply have to accept that no matter what you do, if someone removes a component that you need your application will fail. If they do that they can use the 'repair' option on your installer from which you can either reinstall the missing Python or tell them that's what they need to do.
To install virtualenv I would first install pip (see https://pip.pypa.io/en/latest/installing.html for instructions, you need to download get_pip.py and run it with the Python 2.7 interpreter), then just run pip install virtualenv.

Python Versions on Mac

I'm working on Mac Os 10.7 (Lion) and I have some questions:
What is the pre-installed version of python on Lion?
I've been working on this computer for some time now, and i've installed lots of software in order to do college work many times I didn't know what I was really doing. The thing is: now I hava on the /Library/Frameworks/Python.framework/Versions/ a folder called "7.0" I'm pretty sure there no python version 7. Is this folder native or a third-part program installation. Can I delete it? (it's using 1 Gb on disk).
Where is located the original python that comes with mac os?
I've choose Homebrew as my package manager, is there a easy way to manage python versions with it?
Lion uses Python 2.7 by default; 2.5 and 2.6 are also available.
/Library/Frameworks/Python.framework does not exist on a stock install of Lion. My guess is that you've ended up with this by installing some application.
The default Python install is primarily installed in /System/Library/Frameworks/Python.framework, although some components are located elsewhere.
Yes - you can brew install python#2 to get a Python 2.7 separate from the system version, or brew install python to get Python 3.7. Both will install to /usr/local, like any other Homebrew recipe.
I think that is Python 2.7 but you can check typing python on a command terminal. It will tell you the version.
I couldn't tell that you can deleting it, because I don't know what it has... can you give more info about it?
Just type "which python" and it will tell you
You should use virtualenv and pip: http://jontourage.com/2011/02/09/virtualenv-pip-basics/

Django - Mac development, environment hell

I was trying to setup Django dev environment on Mac and arrived into a hell. It all started when trying to install PIL, which failed after trying 15 or so different recipes I found on blogs. So I wanted to install the Python, this time 2.7, and reinstall setuptools, easy_install, pip from scratch.
After just installing Python 2.7, and easy_install with setuptools for 2.7, this all in turn created such a mess that is unbelievable. Different version of Python are installed everywhere, easy_install is installed everywhere and points randomly to different python hashbangs (sometimes to #!/usr/bin, #!/usr/local/, #!/Library/...)
Now I can't even do easy_install pip, which I always could. So I'm already in a hell and I haven't even attempted to install MySQL yet.
My question finally is did anyone bump into such problems, it would help enough to know that I'm not alone.
Second, would it be easier to set up the entire environment on Ubuntu than it is on a Mac?
Thirdly, is there any guide that can really clearly explain how to set up but also tear down the stack for Python development on a Mac?
It wouldn't hurt to run a VM with vagrant. This post should tell you more:
http://stevelosh.com/blog/2011/06/django-advice/
Of course using virtualenv should also help alleviate some of these issues.
I've gone through the same hell 2 weeks ago :)
I needed to make working python 2.7 and virtualenv on OSX 10.6.8.
You haven't mentioned virtualenv in your question but I strongly recommend it. That way you minimize amount of globally installed packages. Everything is... cleaner.
My idea is to only have following things globally:
python (from brew)
pip (via easy_install)
virtualenv (via pip)
virtualenvwrapper (via pip)
other through either virtualenv or buildout
I've just checked and pip PIL installs fine within my virtualenv.
Here are notes from this battle (gist.github.com):
#NOTE: .pydistutils.cfg seems to be not compatible with brew install python
#areas I needed to clean before installation
#clean up ~/Library/Python
#clean up .local
brew install python
easy_install pip
pip install virtualenv
pip install virtualenvwrapper
mkdir $HOME/.virtualenvs
Example .bash_profile:
#homebrew
export PATH=/usr/local/bin:/usr/local/sbin:${PATH}
# homebrew python 2.7
export PATH="/usr/local/share/python:${PATH}"
#virtualenv wrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/share/python/virtualenvwrapper.sh
Good luck!
Second, would it be easier to set up
the entire environment on Ubuntu than
it is on a Mac?
To answer this question (though I never used Mac though): I never had problems setting up a python environment for Django development on Ubuntu. Though in any case you should go with the built-in Python version if possible. Attempting to install any other Python versions usually ends up messy. Luckily with Ubuntu 11.04 the standard version is already 2.7.
I'm using django development environment on a MAC OS X 10.8 with python 2.7. I don't use virtualenv ore some other things.
With all the respect can say that there is NO ANY PROBLEMS to develop on a mac. Mac is a UNIX like system and you've probably seen that all tools for developers have MAC ports.
As for the setup mess. It's a good idea to use virtualenv. As for PIL installation. I needed to compile it with TrueType. As I'm in common with UNIX like environments it was not heavy task for me to compile PIL from sources using GCC (it's already installed on a MAC)... There are some mess with Django to setup virtualenv... There are certainly lots of articles to setup it on Google.
I use Eclipse and write all my PYTHONPATH variables there. You can forget installing everything like in Linux and try not to make anymore mess with installed tools. Try to read THIS article if you feel like you're ok to use Eclipse for your development on a MAC. It also has a recipe to avoid mess with installation of many copies of Python and other dev utils.
Yes I have had problems with MacOS. I think rather than trying to figure it out I just switched to Ubuntu. I use a mac with Ubuntu installed in VMware Fusion. I have developed on both and prefer the Ubuntu because I'm just more comfortable with installing packages and the file structure.
I love using the VM because I'm never scared of having to start over. I can get a whole new OS installed and get the packages with what I use in just a few hours. Not to mention with 6month rollouts I can do complete installs of new versions instead of updates.
Depending on your production environment, it may be beneficial to use an OS that is similar, if you can install a package on ubuntu desktop, you already know how to do it on ubuntu server.

What is the best way to install python 2 on OS X?

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.

Categories

Resources