Search code examples
python-3.xpippython-venv

Imported module was not found?


I am building a web scraper and I am trying to import the 'requests' package but I am getting an error. I am being told the following:

ModuleNotFoundError: No module named 'requests'

Full Error:

(venv) USERs-MacBook-Pro:Scraper user$ /usr/local/opt/[email protected]/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
Traceback (most recent call last):
  File "/Users/user/git/ML/Python/Practice/Scraper/Scraper.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

Steps I took when setting up project:

  1. python3 -m venv project_name/venv
  2. source project_name/venv/bin/activate
  3. python3 -m pip install SomePackage

Confirmation package and dependences were installed:

(venv) USERs-MacBook-Pro:Scraper user$ pip list
Package    Version
---------- ---------
certifi    2020.12.5
chardet    4.0.0
idna       2.10
pip        20.2.3
requests   2.25.1
setuptools 49.2.1
urllib3    1.26.2

Solution

  • By using the absolute path to system Python like that:

    /usr/local/opt/[email protected]/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
    

    you are using system's Python 3.9 and the packages that are installed for that one although you are in a virtual environment.

    When creating a virtual environment you are creating a separate environment with the specified python version and all the packages you install with pip are installed to this environment and this python version.

    You can better understand that if you run:

    which python
    

    inside your virtual environment.

    This will show you the python that is used when you run python inside your venv and it's going to be different than

    /usr/local/opt/[email protected]/bin/python3.9
    

    So, by having installed requests using pip inside your environment, it is installed in the environments python which is executed when you run just python.

    To sum up, to use the packages installed inside your venv with pip you should run your script (after activating the venv) with:

    python /Users/user/git/ML/Python/Practice/Scraper/Scraper.py