Python can't locate distutils_path on Mac OSX

前端 未结 5 874
慢半拍i
慢半拍i 2021-02-01 18:54

I\'ve been using virtualenv + pip for python development. I\'m not sure what happened, but suddenly whenever I try to run a command-line tool or import libraries, I get this er

相关标签:
5条回答
  • 2021-02-01 19:36

    I have used a similar approach of Nat Goodspeed.

    But I've copied all *.py files.

    Download the same version of your system python, 2.7.2 in my case:

    $ python --version
    

    Download it and unpack it. http://python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2

    # -n copy only missing files, -r recursively
    $ sudo cp -rn ~/Downloads/Python-2.7.2/Lib/* /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
    
    0 讨论(0)
  • 2021-02-01 19:44

    Turns out the problem was that Migration Assistant, for whatever reason, didn't copy over tools like gcc -- I reinstalled Xcode and things work properly again.

    0 讨论(0)
  • 2021-02-01 19:58

    I encountered this distutils/__init__.py problem when transitioning to OS X 10.7 Lion (from OS X 10.5 Leopard) and using Migration Assistant. I've already installed Xcode 3.2.6 -- thus resolving the missing install_name_tool problem.

    Migration Assistant brought over my previous virtualenvs, but since they were based on Leopard's Python 2.5, I figure I need to recreate each of them with the current system Python 2.7.

    easy_install was already in the PATH -- probably because it was bundled with Lion's Python 2.7; it seems unlikely to be the result of Migration Assistant. I used easy_install to install virtualenv.

    This problem, it seems to me, doesn't have anything to do with Xcode or lack thereof. It's a peculiar line in a file placed in the new virtual env by the virtualenv command:

    
      File "/path/to/my/virtualenv/lib/python2.7/distutils/__init__.py", line 16, in 
        exec(open(os.path.join(distutils_path, '__init__.py')).read())
    IOError: [Errno 2] No such file or directory: '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/__init__.py'
    

    The issue is that, in the Python 2.7 install bundled with Lion, the library doesn't come with .py source files. That directory contains .pyc and .pyo files, but no .py files. virtualenv doesn't seem to expect that.

    My workaround is to download Python 2.7 source:
    http://python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2

    and unpack distutils/__init__.py into the expected place:
    sudo tar xvjf ~/Downloads/Python-2.7.2.tar.bz2 --strip-components=2 -C /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 Python-2.7.2/Lib/distutils/__init__.py

    That permits virtualenv to complete successfully, and the resulting Python interpreter seems to run.

    Given that the Python 2.7 library bundled with Lion is installed without source, it might seem useful to change virtualenv to try for either distutils/__init__.py or distutils/__init__.pyc ?

    0 讨论(0)
  • 2021-02-01 19:59

    While Migration Assistant doesn't handle things like XCode so well, it is nevertheless designed to transfer some types of applications. It works best with those that exist entirely in the /Applications/ folder or those applications that launch from the /Applications/ folder and do checks for associated files located elsewhere (e.g., /usr/bin/), installing them when they aren't detected on startup.

    See http://support.apple.com/kb/HT4413.

    0 讨论(0)
  • 2021-02-01 20:01
    > cd /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/
    > sudo touch __init__.py
    

    Out-of-the-box python on Lion comes without the python source - just the compiled pyc/pyo files. However virtualenv goes looking for the distutils source file just to confirm where it is. Turns out all we need to do it touch the file it's looking for into existence.

    Credits belong to "npdoty" and "Nat Goodspeed".

    0 讨论(0)
提交回复
热议问题