-2

When I run my code that includes psutil module I encounter the following error:

Traceback (most recent call last):
  File "/Users/alek/Desktop/coloring/solver.py", line 4, in <module>
    from psutil import cpu_count
ModuleNotFoundError: No module named 'psutil'

Although, when trying to install the module in terminal, I can see the following

DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Requirement already satisfied: psutil in /opt/homebrew/lib/python3.9/site-packages (5.9.1)
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
WARNING: There was an error checking the latest version of pip.

How should I ensure that the module is in the right environment and can be used by my program?

Thanks in advance!

catfood
  • 1
  • 3
  • Did you install python yourself? or Are you using Python version came with your operating system. If you want to make life easier, I suggest you install Miniconda or Anaconda on your computer and install packages or modules using conda. – Supertech Jul 21 '22 at 12:51
  • I have installed python by myself, but I am new to programming. I have tried anaconda, but it seems complex to me (It is part of my programming class that I need to submit and not sure how to do it through conda). Unless I can still program on my desktop and use conda only to keep my modules? Can you recommend any tutorial that is easy to understand to a newbie? Appreciate it a lot! – catfood Jul 21 '22 at 13:08
  • 1
    error may suggest common problem: you have two Pythons installed and you installed module `psutil` in one Python but you use other Python to run code. Pythons don't share modules and you have to install module with correct Python. Error shows `python3.9` so you may need to use `pip3.9 install ...` or `python3.9 -m pip install ...` to install module in `Python 3.9`. Using `pip` it may install module in other Python. You can also check versions `pip -V` (upper `V`) – furas Jul 21 '22 at 13:08
  • Miniconda is a lighter version of Anaconda, which installs many packages in advance, but it will allow you to install anything you want later. Here is the link to Miniconda webpage: https://docs.conda.io/en/latest/miniconda.html Once installed you can use “conda” commands to install packages and create virtual environment. https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html Just do google search for conda tutorial, you will also find many youtube tutorial. Here is one. https://www.youtube.com/watch?v=YJC6ldI3hWk – Supertech Jul 21 '22 at 13:16
  • Thank you! It didn't help, but then I wonder if it makes sense to simply uninstall one python and keep the other one? If so - what is the smartest way to do it? – catfood Jul 21 '22 at 13:19
  • don't uninstall original Python because system may need it. Maybe check what version you have `python -V` and if it is `3.6`, `3.7`, `3.8` then you can uninstall version which you installed. Sometimes it better to use little older version because some modules may not exist for the newest version or may have some changes which you may not see in older tutorials. And if you have installed two version then check if you have command `python3.9`, `python3.8`, `python3.7` and `pip3.9`, `pip3.8`, `pip3.7` to run selected version - and simply always use command with version number. – furas Jul 21 '22 at 13:33

2 Answers2

0

try to upgrade it

sudo pip install --upgrade psutil
Ola Galal
  • 162
  • 1
  • 11
  • Thanks! Unfortunately it doesn't work, it shows the following message: The directory '/Users/alek/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag. – catfood Jul 21 '22 at 12:55
  • Did you try with -H flag? – Ola Galal Jul 21 '22 at 12:56
  • I did - I have reinstalled the module, but getting the same error. After installing I have used command `sudo -H pip install psutil` – catfood Jul 21 '22 at 13:02
  • `sudo -H pip install --upgrade psutil` did you try this exactly? with both `--upgrade` and `-H` – Ola Galal Jul 21 '22 at 13:03
  • I did, the output is following: `WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv WARNING: There was an error checking the latest version of pip.` Module still cannot be found. – catfood Jul 21 '22 at 13:06
  • maybe try `sudo pip3 install --upgrade psutil` because it seems you have two versions of python installed. – Ola Galal Jul 21 '22 at 13:11
0

try to run this command in python shell:

import site
site.getsitepackages()

actually, it is trying to search psutil module in one of the Xcode path.

see if you have psutil in this path:

/Applications/Xcode_13.4.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages

if not add the psutil module here and it should work. it worked for me.

Jonathan Ciapetti
  • 1,261
  • 3
  • 11
  • 16