[ACCEPTED]-What is the most compatible way to install python modules on a Mac?-macports

Accepted answer
Score: 145

The most popular way to manage python packages 23 (if you're not using your system package 22 manager) is to use setuptools and easy_install. It 21 is probably already installed on your system. Use 20 it like this:

easy_install django

easy_install uses the Python Package Index which 19 is an amazing resource for python developers. Have 18 a look around to see what packages are available.

A 17 better option is pip, which is gaining traction, as 16 it attempts to fix a lot of the problems associated with easy_install. Pip 15 uses the same package repository as easy_install, it 14 just works better. Really the only time 13 use need to use easy_install is for this 12 command:

easy_install pip

After that, use:

pip install django

At some point you 11 will probably want to learn a bit about 10 virtualenv. If you do a lot of python development 9 on projects with conflicting package requirements, virtualenv 8 is a godsend. It will allow you to have 7 completely different versions of various 6 packages, and switch between them easily 5 depending your needs.

Regarding which python 4 to use, sticking with Apple's python will 3 give you the least headaches, but If you 2 need a newer version (Leopard is 2.5.1 I 1 believe), I would go with the macports python 2.6.

Score: 42

Your question is already three years old 65 and there are some details not covered in 64 other answers:

Most people I know use HomeBrew or 63 MacPorts, I prefer MacPorts because of its clean 62 cut of what is a default Mac OS X environment 61 and my development setup. Just move out 60 your /opt folder and test your packages with 59 a normal user Python environment

MacPorts 58 is only portable within Mac, but with easy_install 57 or pip you will learn how to setup your 56 environment in any platform (Win/Mac/Linux/Bsd...). Furthermore 55 it will always be more up to date and with 54 more packages

I personally let MacPorts handle 53 my Python modules to keep everything updated. Like 52 any other high level package manager (ie: apt-get) it 51 is much better for the heavy lifting of 50 modules with lots of binary dependencies. There 49 is no way I would build my Qt bindings (PySide) with 48 easy_install or pip. Qt is huge and takes 47 a lot to compile. As soon as you want a 46 Python package that needs a library used 45 by non Python programs, try to avoid easy_install 44 or pip

At some point you will find that 43 there are some packages missing within MacPorts. I 42 do not believe that MacPorts will ever give 41 you the whole CheeseShop. For example, recently I 40 needed the Elixir module, but MacPorts only offers 39 py25-elixir and py26-elixir, no py27 version. In 38 cases like these you have:

pip-2.7 install 37 --user elixir

( make sure you always type 36 pip-(version) )

That will build an extra 35 Python library in your home dir. Yes, Python 34 will work with more than one library location: one 33 controlled by MacPorts and a user local 32 one for everything missing within MacPorts.

Now 31 notice that I favor pip over easy_install. There 30 is a good reason you should avoid setuptools 29 and easy_install. Here is a good explanation and I try to 28 keep away from them. One very useful feature 27 of pip is giving you a list of all the modules 26 (along their versions) that you installed 25 with MacPorts, easy_install and pip itself:

pip-2.7 24 freeze

If you already started using easy_install, don't 23 worry, pip can recognize everything done 22 already by easy_install and even upgrade 21 the packages installed with it.

If you are 20 a developer keep an eye on virtualenv for controlling 19 different setups and combinations of module 18 versions. Other answers mention it already, what 17 is not mentioned so far is the Tox module, a 16 tool for testing that your package installs 15 correctly with different Python versions.

Although 14 I usually do not have version conflicts, I 13 like to have virtualenv to set up a clean 12 environment and get a clear view of my packages 11 dependencies. That way I never forget any 10 dependencies in my setup.py

If you go for 9 MacPorts be aware that multiple versions 8 of the same package are not selected anymore 7 like the old Debian style with an extra 6 python_select package (it is still there 5 for compatibility). Now you have the select 4 command to choose which Python version will 3 be used (you can even select the Apple installed 2 ones):

$  port select python
Available versions for python:
    none
    python25-apple
    python26-apple
    python27 (active)
    python27-apple
    python32

$ port select python python32

Add tox on top of it and your programs 1 should be really portable

Score: 30

Please see Python OS X development environment. The best way is to use MacPorts. Download 12 and install MacPorts, then install Python 11 via MacPorts by typing the following commands 10 in the Terminal:

sudo port install python26 python_select
sudo port select --set python python26

OR

sudo port install python30 python_select
sudo port select --set python python30

Use the first set of commands 9 to install Python 2.6 and the second set 8 to install Python 3.0. Then use:

sudo port install py26-packagename

OR

sudo port install py30-packagename

In the 7 above commands, replace packagename with the name of 6 the package, for example:

sudo port install py26-setuptools

These commands 5 will automatically install the package (and 4 its dependencies) for the given Python version.

For 3 a full list of available packages for Python, type:

port list | grep py26-

OR

port list | grep py30-

Which 2 command you use depends on which version 1 of Python you chose to install.

Score: 7

I use MacPorts to install Python and any 23 third-party modules tracked by MacPorts 22 into /opt/local, and I install any manually installed 21 modules (those not in the MacPorts repository) into 20 /usr/local, and this has never caused any problems. I 19 think you may be confused as to the use 18 of certain MacPorts scripts and environment 17 variables.

MacPorts python_select is used to select the 16 "current" version of Python, but it has 15 nothing to do with modules. This allows 14 you to, e.g., install both Python 2.5 and 13 Python 2.6 using MacPorts, and switch between 12 installs.

The $PATH environment variables does 11 not affect what Python modules are loaded. $PYTHONPATH is 10 what you are looking for. $PYTHONPATH should point 9 to directories containing Python modules 8 you want to load. In my case, my $PYTHONPATH variable 7 contains /usr/local/lib/python26/site-packages. If you use MacPorts' Python, it 6 sets up the other proper directories for 5 you, so you only need to add additional paths to $PYTHONPATH. But 4 again, $PATH isn't used at all when Python searches 3 for modules you have installed.

$PATH is used to 2 find executables, so if you install MacPorts' Python, make 1 sure /opt/local/bin is in your $PATH.

Score: 6

There's nothing wrong with using a MacPorts 16 Python installation. If you are installing 15 python modules from MacPorts but then not 14 seeing them, that likely means you are not 13 invoking the MacPorts python you installed 12 to. In a terminal shell, you can use absolute 11 paths to invoke the various Pythons that 10 may be installed. For example:

$ /usr/bin/python2.5         # Apple-supplied 2.5 (Leopard)
$ /opt/local/bin/python2.5   # MacPorts 2.5
$ /opt/local/bin/python2.6   # MacPorts 2.6
$ /usr/local/bin/python2.6   # python.org (MacPython) 2.6
$ /usr/local/bin/python3.1   # python.org (MacPython) 3.1

To get the 9 right python by default requires ensuring 8 your shell $PATH is set properly to ensure 7 that the right executable is found first. Another 6 solution is to define shell aliases to the 5 various pythons.

A python.org (MacPython) installation 4 is fine, too, as others have suggested. easy_install can 3 help but, again, because each Python instance 2 may have its own easy_install command, make sure you 1 are invoking the right easy_install.

Score: 6

If you use Python from MacPorts, it has 5 it's own easy_install located at: /opt/local/bin/easy_install-2.6 (for 4 py26, that is). It's not the same one as 3 simply calling easy_install directly, even 2 if you used python_select to change your 1 default python command.

Score: 4

Have you looked into easy_install at all? It won't 6 synchronize your macports or anything like 5 that, but it will automatically download 4 the latest package and all necessary dependencies, i.e.

easy_install nose

for 3 the nose unit testing package, or

easy_install trac

for the 2 trac bug tracker.

There's a bit more information 1 on their EasyInstall page too.

Score: 3

For MacPython installations, I found an 5 effective solution to fixing the problem 4 with setuptools (easy_install) in this blog 3 post:

http://droidism.com/getting-running-with-django-and-macpython-26-on-leopard

One handy tip includes finding out 2 which version of python is active in the 1 terminal:

which python
Score: 2

When you install modules with MacPorts, it 7 does not go into Apple's version of Python. Instead 6 those modules are installed onto the MacPorts 5 version of Python selected.

You can change 4 which version of Python is used by default 3 using a mac port called python_select. instructions here.

Also, there's 2 easy_install. Which will use python to install python 1 modules.

Score: 2

You may already have pip3 pre-installed, so 1 just try it!

Score: 1

Regarding which python version to use, Mac 5 OS usually ships an old version of python. It's 4 a good idea to upgrade to a newer version. You 3 can download a .dmg from http://www.python.org/download/ . If you do that, remember 2 to update the path. You can find the exact 1 commands here http://farmdev.com/thoughts/66/python-3-0-on-mac-os-x-alongside-2-6-2-5-etc-/

More Related questions