I have just downloaded the Python 2.7.1 DMG from Python.org. I have seen suggestions to get ActivePython, but I would prefer the one from Python.org.
- If I just run the
mpkg
installer inside the DMG, accepting all defaults, will it live peacefully with the Apple-supplied Python? - If I type
python
in Terminal, which one will I get? - In Terminal, how do I specify to run the Apple-supplied Python? What about the Python I installed myself?
- What are these talk about setting the
PATH
when installing a different Python version? I understand that the Python installer will just set it up automatically. But I still want to peek under the hood. I know how to do this in Windows (Environment Variables). For Mac OS X, how do I tinker with thePATH
?
I might as well try these out myself first, but I'm new to the Mac. Python is quite a complicated installation, writing 开发者_运维技巧files to different folders and configuring OS settings like PATH
. TrashMe or AppCleaner might not be very effective with uninstalling Python if ever I want to go back to a clean slate. Therefore I want to gain clear insights to my questions above.
If you did not change the default set of packages when using the python.org installer, typing python
from a command line should run the newly-installed Python 2.7. (You will need to start a new terminal session after running the installer to see this.) The current python.org installers for OS X create a folder in your Applications directory named Python m.n
depending on the Python version. If you look in /Applications/Python 2.7
, you'll see a file called Update Shell Profile.command
. It's a shell script; you can inspect it in an editor or with Quicklook. Its purpose is to modify the startup files for the most common shell programs on OS X (bash
, sh
, csh
) to ensure that the directory where the new Python's executable commands are located gets added to the front of the list of directories in the PATH environment variable, so that the python commands in it will be found before the Apple-suppled python commands are found. By default, the installer runs the Update Shell Profile.command
for you automatically. This should result in something like this:
$ cat ~/.bash_profile
# .bash_profile
# ... other stuff
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
If you take a look in that directory, you should see something like this:
$ ls -l /Library/Frameworks/Python.framework/Versions/2.7/bin
total 272
lrwxr-xr-x 1 root admin 8 Nov 30 00:49 2to3@ -> 2to3-2.7
-rwxrwxr-x 1 root admin 140 Nov 30 00:30 2to3-2.7*
lrwxr-xr-x 1 root admin 7 Nov 30 00:49 idle@ -> idle2.7
-rwxrwxr-x 1 root admin 138 Nov 30 00:30 idle2.7*
lrwxr-xr-x 1 root admin 8 Nov 30 00:49 pydoc@ -> pydoc2.7
-rwxrwxr-x 1 root admin 123 Nov 30 00:30 pydoc2.7*
lrwxr-xr-x 1 root admin 9 Nov 30 00:49 python@ -> python2.7
lrwxr-xr-x 1 root admin 16 Nov 30 00:49 python-config@ -> python2.7-config
-rwxrwxr-x 1 root admin 33764 Nov 30 00:31 python2.7*
-rwxrwxr-x 1 root admin 1663 Nov 30 00:31 python2.7-config*
lrwxr-xr-x 1 root admin 10 Nov 30 00:49 pythonw@ -> pythonw2.7
-rwxrwxr-x 1 root admin 33764 Nov 30 00:31 pythonw2.7*
lrwxr-xr-x 1 root admin 11 Nov 30 00:49 smtpd.py@ -> smtpd2.7.py
-rwxrwxr-x 1 root admin 18586 Nov 30 00:30 smtpd2.7.py*
The new python is available as the command python2.7
but there is also a symbolic link to it as python
. Because the PATH environment has been changed:
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
when you type python
as a command in the shell, that symlink will be found first before the Apple-supplied python
in /usr/bin
one of the directories where system-supplied user commands are installed (as on OS X 10.6):
$ ls /usr/bin/py*
/usr/bin/pydoc* /usr/bin/python-config* /usr/bin/python2.6-config@
/usr/bin/pydoc2.5@ /usr/bin/python2.5@ /usr/bin/pythonw*
/usr/bin/pydoc2.6@ /usr/bin/python2.5-config@ /usr/bin/pythonw2.5@
/usr/bin/python* /usr/bin/python2.6@ /usr/bin/pythonw2.6@
(Note, in general, you should not attempt to modify or delete files in /usr/bin
since they are part of OS X and managed by Apple.)
There are many ways to manage multiple Python installations on OS X; check the archives or the web. One thing to keep in mind is that you can always use an absolute path to the desired python command to check. So with the modified path as above you should see the following behaviors:
$ /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -c 'import sys;print(sys.version)'
2.7.1 (r271:86882M, Nov 30 2010, 09:39:13)
[GCC 4.0.1 (Apple Inc. build 5494)]
$ python2.7 -c 'import sys;print(sys.version)'
2.7.1 (r271:86882M, Nov 30 2010, 09:39:13)
[GCC 4.0.1 (Apple Inc. build 5494)]
$ python -c 'import sys;print(sys.version)'
2.7.1 (r271:86882M, Nov 30 2010, 09:39:13)
[GCC 4.0.1 (Apple Inc. build 5494)]
$ /usr/bin/python -c 'import sys;print(sys.version)'
2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)]
$ /usr/bin/python2.6 -c 'import sys;print(sys.version)'
2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)]
- Yes, it will work fine.
- You will have to change the appropriate symlink. You can always check using
which python
. - You can create extra symlinks for the different versions (e.g.
python_mac
andpython_standard
). - Read this.
Different python installations can be made to run without hinderance specially, if you use virtualenv
You can follow these article about
- bootstrapping virtual environments
- setting up python ecosystems
These will instruct you on installing different versions of python in different places each with their own set of site-packages and have them individually work on different projects. Highly recommend you look into it as it also explains how to change the PATH
variable in an easy way so that different python installations can be found by your shell commands.
Here is some quick info about where different python installations go to..
Typing which python
at the terminal will tell you its location. You can also use python -V
to see the version used. Remember, python
and python2.x
can be different things. You can assign appropriate names to different python installations in order to call them at the terminal or in your scripts by using alias
command. For instance,
alias /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 sys-py27
Apple supplied system python is installed at
/System/Library/Frameworks/Python.framework
and/usr/bin/python
- Indeed, you shouldn't mess with these by trying to delete them manually. The system python could be in use by other software in your computer.
To check what is currently the system python you can use
python -c 'import sys;print(sys.version)'
Packaged installers that you download and run typically install to
/Library/Frameworks/Python.framework/Versions/2.7/bin/
- You can manually remove the Python.framework folder. You might also see Python in the Applications folder along with other things like IDLE, AppBuilder and PythonLauncher. You can just go ahead and delete those if you need to.
MacPorts will install python or something like python26 and python27 to
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/
You can uninstall them by using usual
sudo port uninstall packageName
and if you want to remove dependencies use something like this codesudo port uninstall python27 --follow-dependents installed
All additional things like numpy, scipy, PIL, opencv, etc. that you try to install using
port
will go to/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
精彩评论