Getting a Module Error When Running Pytest Even Though the Module is Installed in the Current Virtual Environment

Hello everyone,

I am encountering an issue when running pytest in my virtual environment. Although I have installed the required modules in my virtual environment using pip, I am still getting a ModuleNotFoundError when running pytest. I had to install some packages globally using the package manager to stop seeing these errors. However, this only works with popular packages since not all of them are available on the package manager.

As a workaround, I had to uninstall pytest from the package manager and install it in the virtual environment. However, I would prefer to use the pytest from the package manager since it makes sense to me to install a package I use often globally instead of installing it in each virtual environment.

In summary, I would like to install only pytest globally while all other packages are installed in the virtual environment. And I want to know how to use the global pytest from the virtual environment without getting any errors.

Thank you in advance for the help.

Here is the output of pip freeze in my virtual environment:

venv ❯ pip freeze
backoff==2.2.1
certifi==2023.5.7
charset-normalizer==3.2.0
feedparser==6.0.10
idna==3.4
pythorhead==0.12.3
requests==2.31.0
schedule==1.2.0
sgmllib3k==1.0.0
urllib3==2.0.3

And here is the error I am getting when running pytest:

venv ❯ pytest my_module.py
E   ModuleNotFoundError: No module named 'schedule'

Hello @Individual-Heart-719 :wink:

No issue here…

I used this example:

import schedule
import time

def job():
    print("I'm working...")

schedule.every(10).seconds.do(job)
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().day.at("12:42", "Europe/Amsterdam").do(job)
schedule.every().minute.at(":17").do(job)

def job_with_argument(name):
    print(f"I am {name}")

schedule.every(10).seconds.do(job_with_argument, name="Peter")

while True:
    schedule.run_pending()
    time.sleep(1)
backoff==2.2.1
certifi==2023.5.7
charset-normalizer==3.2.0
feedparser==6.0.10
idna==3.4
iniconfig==2.0.0
packaging==23.1
pluggy==1.2.0
pytest==7.4.0
pythorhead==0.12.3
pytz==2023.3
requests==2.31.0
schedule==1.2.0
sgmllib3k==1.0.0
urllib3==2.0.3

In a virtual environment, it will never use the system package. It only links the python binaries, not the site-packages.

You don’t have pytest installed in your venv.