简体   繁体   中英

Proper permissions for Python and packages on OS X Lion

I am running OS X Lion (started on Leopard, and have gone through two upgrades). OS X Lion comes with Python 2.7. At some point, I thought Python and its packages were working together (possibly before my upgrade with Lion).

I can run Python as a non-superuser. However, when I import packages or try running easy_install , for example, I get the following error.

system:distutils $ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zope
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/zope/__init__.py", line 1, in <module>
    __import__('pkg_resources').declare_namespace(__name__)
  File "build/bdist.linux-i686/egg/pkg_resources.py", line 698, in <module>
    class Environment(object):
  File "build/bdist.linux-i686/egg/pkg_resources.py", line 701, in Environment
    def __init__(self, search_path=None, platform=get_supported_platform(), python=PY_MAJOR):
  File "build/bdist.linux-i686/egg/pkg_resources.py", line 96, in get_supported_platform
    plat = get_build_platform(); m = macosVersionString.match(plat)
  File "build/bdist.linux-i686/egg/pkg_resources.py", line 222, in get_build_platform
    plat = get_platform()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/util.py", line 165, in get_platform
    "Don't know machine value for archs=%r"%(archs,))
ValueError: Don't know machine value for archs=()
>>> ^D
system:distutils $

When I run it as root or as sudo , everything works fine. All of the directories and files have 0755 or 0644 permissions, respectively.

What should the permissions be for Python and its packages on OS X Lion so you can use it as a normal user? Is the way my permissions are currently configured the default, or did I mess up the permissions along the way.

I realize I can go through the /System/Library/... and the /Library/Python/... directories and change the ownership and permission to me. But that does not seem like that is the correct solution.

For anyone coming here via googling for the error, it's also possible this is not a permissions problem. Rather, it can occur because the environment variables used by Python to determine the (Apple) platform are set incorrectly.

Relevant flags include at least:

'CFLAGS', 'LDFLAGS', 'CPPFLAGS', 'BASECFLAGS', 'BLDSHARED', 'LDSHARED', 'CC', 'CXX', 'PY_CFLAGS', 'PY_LDFLAGS', 'PY_CPPFLAGS', 'PY_CORE_CFLAGS'

Check out the the _osx_support.py file of Python stdlib, and the get_platform_osx function in particular; it's all there.

After struggling for a while, and getting repeatedly frustrated, I figured out two reasons easy_install could fail with this error.

  1. I need to have XCode installed (which I already did)
  2. I simply went through the code in util.py to find the line that produced the error, and changed the result.

This worked perfectly. I now never receive that error. I have put the diff below for the solution. Please note, I chose 'intel' for the machine type, because with OS X Lion, all PPC support is removed.

I hope this helps someone else.

wintermute:distutils $ pwd
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils
wintermute:distutils $ diff -U 5  util.py.bad util.py
--- util.py.bad 2012-03-03 15:30:39.000000000 -0500
+++ util.py 2012-03-03 15:32:21.000000000 -0500
@@ -159,12 +159,13 @@
                 elif archs == ('ppc64', 'x86_64'):
                     machine = 'fat64'
                 elif archs == ('i386', 'ppc', 'ppc64', 'x86_64'):
                     machine = 'universal'
                 else:
-                    raise ValueError(
-                       "Don't know machine value for archs=%r"%(archs,))
+                    machine = 'intel'
+                    #raise ValueError(
+                    #   "Don't know machine value for archs=%r"%(archs,))

             elif machine == 'i386':
                 # On OSX the machine type returned by uname is always the
                 # 32-bit variant, even if the executable architecture is
                 # the 64-bit variant
wintermute:distutils $ 

There's something unusual about your Python setup. On my Lion (10.7.3) systems, the Apple-supplied Python 2.7 has this banner and there is no problem importing Zope:

$ /usr/bin/python2.7
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zope
>>>

Perhaps you have a pre-release version of parts of Lion installed?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM