代码之家  ›  专栏  ›  技术社区  ›  Jono

更新OSX和python后,无法在python中导入已安装的软件包

  •  1
  • Jono  · 技术社区  · 10 年前

    我最近更新了我的系统(OXS10.9.1->10.9.2),并使用brew(2.7.3->2.7.6)更新了python。

    现在我的行为很奇怪。一个例子是尝试运行一个创建烧瓶套接字的测试应用程序:

    jono@air:~/Workspace/sandbox $ python app.py
    Traceback (most recent call last):
      File "app.py", line 9, in <module>
        import flask, flask_sockets, time, threading, simplejson
      File "/usr/local/lib/python2.7/site-packages/flask/__init__.py", line 21, in <module>
        from .app import Flask, Request, Response
      File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 26, in <module>
        from . import json
      File "/usr/local/lib/python2.7/site-packages/flask/json.py", line 25, in <module>
        from itsdangerous import json as _json
    ImportError: No module named itsdangerous
    

    首先想到的是更新它的危险性:

    jono@air:~/Workspace/sandbox $ pip install itsdangerous --upgrade
    Requirement already up-to-date: itsdangerous in /usr/local/lib/python2.7/site-packages
    Cleaning up...
    

    检查其安装是否正确:

    jono@air:~/Workspace/sandbox $ locate itsdangerous
    /usr/local/lib/python2.7/site-packages/itsdangerous-0.23-py2.7.egg-info
    /usr/local/lib/python2.7/site-packages/itsdangerous-0.23-py2.7.egg-info/PKG-INFO
    /usr/local/lib/python2.7/site-packages/itsdangerous-0.23-py2.7.egg-info/SOURCES.txt
    /usr/local/lib/python2.7/site-packages/itsdangerous-0.23-py2.7.egg-info/dependency_links.txt
    /usr/local/lib/python2.7/site-packages/itsdangerous-0.23-py2.7.egg-info/installed-files.txt
    /usr/local/lib/python2.7/site-packages/itsdangerous-0.23-py2.7.egg-info/not-zip-safe
    /usr/local/lib/python2.7/site-packages/itsdangerous-0.23-py2.7.egg-info/top_level.txt
    /usr/local/lib/python2.7/site-packages/itsdangerous.py
    /usr/local/lib/python2.7/site-packages/itsdangerous.pyc
    

    检查我的巨蟒:

    jono@air:~/Workspace/sandbox $ echo $PYTHONPATH
    /usr/local/lib/python2.7/site-packages/
    

    检查python是否正在查找正确的PYTHONPATH:

    jono@air:~/Workspace/sandbox $ python
    Python 2.7.6 (default, Mar  6 2014, 10:46:26) 
    [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import itsdangerous
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named itsdangerous
    >>> import os
    >>> os.getenv("PYTHONPATH")
    '/usr/local/lib/python2.7/site-packages/'
    >>> 
    

    我对virtualenv等其他基本软件包也有类似的问题:

    jono@air:~/Workspace/sandbox $ virtualenv venv
    Traceback (most recent call last):
      File "/usr/local/bin/virtualenv", line 5, in <module>
        from pkg_resources import load_entry_point
      File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 2720, in <module>
    
      File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 588, in resolve
        The `plugin_env` should be an ``Environment`` instance that contains
    pkg_resources.DistributionNotFound: virtualenv==1.9.1
    

    是否已安装?

    jono@air:~/Workspace/sandbox $ pip install virtualenv --upgrade
    Requirement already up-to-date: virtualenv in /usr/local/lib/python2.7/site-packages
    Cleaning up...
    

    但奇怪的是,这次蟒蛇能看到它:

    jono@air:~/Workspace/sandbox $ python
    Python 2.7.6 (default, Mar  6 2014, 10:46:26) 
    [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import virtualenv
    >>> dir(virtualenv)
    ['ACTIVATE_BAT', 'ACTIVATE_CSH', 'ACTIVATE_FISH', 'ACTIVATE_PS', 'ACTIVATE_SH', 'ACTIVATE_THIS', 'BIG_ENDIAN', 'ConfigOptionParser', 'ConfigParser', 'DEACTIVATE_BAT', 'DISTUTILS_CFG', 'DISTUTILS_INIT', 'FAT_MAGIC', 'LC_LOAD_DYLIB', 'LITTLE_ENDIAN', 'Logger', 'MH_CIGAM', 'MH_CIGAM_64', 'MH_MAGIC', 'MH_MAGIC_64', 'OK_ABS_SCRIPTS', 'REQUIRED_FILES', 'REQUIRED_MODULES', 'SITE_PY', 'UpdatingDefaultsHelpFormatter', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_find_file', 'abiflags', 'base64', 'call_subprocess', 'change_prefix', 'codecs', 'convert', 'copy_required_modules', 'copyfile', 'copyfileordir', 'create_bootstrap_script', 'create_environment', 'default_config_file', 'default_storage_dir', 'distutils', 'errno', 'expected_exe', 'file_search_dirs', 'fileview', 'filter_install_output', 'find_wheels', 'fix_lib64', 'fix_local_scheme', 'fixup_egg_link', 'fixup_pth_and_egg_link', 'fixup_pth_file', 'fixup_scripts', 'get_installed_pythons', 'glob', 'install_activate', 'install_distutils', 'install_python', 'install_wheel', 'is_cygwin', 'is_darwin', 'is_executable', 'is_executable_file', 'is_jython', 'is_pypy', 'is_win', 'join', 'logger', 'logging', 'mach_o_change', 'main', 'majver', 'make_environment_relocatable', 'make_exe', 'make_relative_path', 'maxint', 'minver', 'mkdir', 'optparse', 'os', 'path_locations', 'py_version', 're', 'read_data', 'relative_script', 'resolve_interpreter', 'rmtree', 'shutil', 'strtobool', 'struct', 'subprocess', 'subst_path', 'sys', 'tarfile', 'tempfile', 'user_dir', 'virtualenv_version', 'writefile', 'zlib']
    >>> virtualenv.is_darwin
    True
    >>> virtualenv.__file__
    '/usr/local/lib/python2.7/site-packages/virtualenv.pyc'
    >>> 
    
    1 回复  |  直到 10 年前
        1
  •  2
  •   Jono    10 年前

    不知怎的,我在/usr/local/lib/python2.7/site-packages/中的许多包都拥有root所有权。这导致了一系列问题:/

    sudo pip uninstall XXX
    

    然后

    pip install XXX
    

    解决了大部分问题。

    让我们吸取教训,永远不要“sudo pip安装”任何东西。