ImportError: No module named site - mac

安稳与你 提交于 2020-06-01 12:42:31

问题


I have had this problem for months. every time that I want to get a new python package and use it I get this error in terminal:

ImportError: No module named site

I don't know why do I get this error. actually, I can't use any new package because every time that I wanna install one I get this error. I searched and I found out that the problem would be for PYTHONPATH and PYTHONHOME but I don't know what they are and how can I change them. My operating system is mac os and I don't know how to solve this problem in mac.

every time that I open the terminal I use these two commands to solve the problem:

unset PYTHONHOME
unset PYTHONPATH

but the next time I get the error again.


回答1:


The Python tooling is evolving in Mac OS X such that Python 2.7 is being deprecated. System default paths may no longer apply as I learned after upgrading to Mac OS X Catalina (10.15.x). There are a few scenarios here:

  1. GENERIC FIX -- According to this primer on the site package and Python internal paths, Python 2.7 requires a specific user path for site packages. The solution I settled on was simply this:
    mkdir -p ~/.local/lib/python2.7/site-packages
    
    At that point you will have a dedicated directory for Python modules (for Python to function). (NOTE that the error ImportError: No module named site is misleading as it really indicates that the correct directory structure didn't exist to allow the site module to properly load.)
  2. BASIC ALTERNATIVE -- Is your PYTHONHOME pointing to Python3 and your python --version reporting 2.7? Does python3 --version report something different (or work at all)? This error started happening for me after the Catalina upgrade when I was trying to use built-ins. Check your .profile and .bash_profile to see if they are explicitly setting a custom PYTHONHOME and/or PYTHONPATH. One option is to change that so the ENV variables are set explicitly and manually only when a newer Python is needed. You may consider symlinking python3 and pip3 to leave the standard commands to the OS.
  3. DEBUGGING -- If you would like to test and/or get more information, try executing Python in one of the increasingly verbose modes:
    python -v
    python -vv
    python -vvv
    


来源:https://stackoverflow.com/questions/56155402/importerror-no-module-named-site-mac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!