python 3.2 import issue

2020-02-16 02:33发布

问题:

I've been troubleshooting for the past few days trying to install distribute so I can start importing 3rd party modules. I haven't used python for a couple years so I'm re-remembering everything which has been pretty difficult.

I followed the errors to the directories given but don't know what to do next.

I'm running a mac version 10.8.2 with python 3.2 running in Eclipse.

here's the code:

Traceback (most recent call last):
  File "/Volumes/James Hard Drive/Python/Python_Lessons/3rd_Party_Stuff/easy_install.py", line 5, in <module>
    from setuptools.command.easy_install import main
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/distribute-0.6.28-py3.2.egg/setuptools/__init__.py", line 2, in <module>
    from setuptools.extension import Extension, Library
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/distribute-0.6.28-py3.2.egg/setuptools/extension.py", line 5, in <module>
    from setuptools.dist import _get_unpatched
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/distribute-0.6.28-py3.2.egg/setuptools/dist.py", line 6, in <module>
    from setuptools.command.install import install
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/distribute-0.6.28-py3.2.egg/setuptools/command/__init__.py", line 8, in <module>
    from setuptools.command import install_scripts
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/distribute-0.6.28-py3.2.egg/setuptools/command/install_scripts.py", line 3, in <module>
    from pkg_resources import Distribution, PathMetadata, ensure_directory
  File "/Volumes/James Hard Drive/Python/Python_Lessons/3rd_Party_Stuff/pkg_resources.py", line 45
    def _bypass_ensure_directory(name, mode=0777):
                                               ^
SyntaxError: invalid token

回答1:

To install distribute (and pip, virtualenv):

#!/bin/bash
# download latest virtualenv.py
wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py

# create a bootstrap virtual environment in ./venv directory
python3 virtualenv.py venv

To activate virtualenv:

$ . ./venv/bin/activate

Now you could use pip, easy_install to install other packages or use virtualenv-3.x to create new virtualenvs.

If you use many virtualenvs (different projects, different python versions); you could pip installvirtualenvwrapper to manage them easily e.g., workon/mkvirtualenv/rmvirtualenv commands.



回答2:

The issue is that Python 3 interprets octal numbers differently than Python 2.x. In 2.x you could just prefix a number with a 0 to indicate that it is octal. In Python 3 you prefix it with 0o, so your number needs to be 0o777.

def _bypass_ensure_directory(name, mode=0o777):

See http://docs.python.org/release/3.0.1/whatsnew/3.0.html#integers for more details.



回答3:

It is possible, that the file dist, is not to a recent version. Watch to this : https://pypi.python.org/pypi/distribute#uninstallation-instructions; and replace your folder normally in Python(version xxx)\scripts, by the highter version. It as raise this problem in my case, without all parse error on files describe before. Erase the previously version installed to finish.