2

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.

  • What are you doing exactly when you try to get a new python package? – Green Cloak Guy May 15 '19 at 18:18
  • Possible duplicate of [ImportError: No module named site on Windows](https://stackoverflow.com/questions/5599872/importerror-no-module-named-site-on-windows) – Laurent LAPORTE May 15 '19 at 18:22
  • @Green Cloak Guy: Using pip install. for example, I wanted to get nilearn and I used this command: `pip install -U --user nilearn` – niloofar latifian May 15 '19 at 18:24
  • @LaurentLAPORTE : It told for windows and I don't know how to do it for mac? – niloofar latifian May 15 '19 at 18:28
  • Have you tried completely reinstalling python? `site-packages` is, by my understanding, one of the fundamental/core libraries and should be built-in to your python installation. If it's not there then something is borked. – Green Cloak Guy May 15 '19 at 18:53
  • Why installing in `--user` mode? Your user environment may be corrupted… It is not very useful unless you want to install a kind of dev tool (like pipx). Why not using a [`virtualenv`](https://virtualenv.pypa.io)? – Laurent LAPORTE May 15 '19 at 19:01
  • @GreenCloakGuy yes I tried but I have the problem again. maybe some broken files left on my computer even when I uninstall python. I don't know! – niloofar latifian May 15 '19 at 20:06

1 Answers1

0

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
    
ingyhere
  • 11,818
  • 3
  • 38
  • 52