问题
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:
- 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:
At that point you will have a dedicated directory for Python modules (for Python to function). (NOTE that the errormkdir -p ~/.local/lib/python2.7/site-packagesImportError: No module named siteis misleading as it really indicates that the correct directory structure didn't exist to allow thesitemodule to properly load.) - BASIC ALTERNATIVE -- Is your
PYTHONHOMEpointing to Python3 and yourpython --versionreporting 2.7? Doespython3 --versionreport 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.profileand.bash_profileto see if they are explicitly setting a customPYTHONHOMEand/orPYTHONPATH. One option is to change that so theENVvariables are set explicitly and manually only when a newer Python is needed. You may consider symlinkingpython3andpip3to leave the standard commands to the OS. - 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