2

I have python program that imports schedule (import schedule) at the beginning. The code executes without a problem with python3 command, but starting it from other python file with call("sudo python3 ProgramWithSchedule.py", shell=True) returns error ModuleNotFoundError: No module named 'schedule'. And I can't figure out why...

I have library schedule installed with pip, pip3 AND apt-get (tried all three just to be sure :)

Thanks!

4
  • That means the module was installed in a venv. And now you are in a different python venv. You have to re-install the module.
    – Jamiu S.
    Aug 6, 2022 at 14:15
  • I have both files in the same folder, and schedule was also installed from the same folder, so how could I be in the different python venv now. And if so, how do I install it in that different venv? Aug 6, 2022 at 14:32
  • Try installing it again. In your terminal enter pip install schedule if it is already installed, your output will be something like package already installed and if not your output will be like package installed
    – Jamiu S.
    Aug 6, 2022 at 14:52
  • tried, output is Requirement already satisfied Aug 6, 2022 at 15:03

2 Answers 2

1

You can try to force the usage of the same python interpreter with :

call(f"sudo {os.getenv('PYTHON3')} ProgramWithSchedule.py", shell=True)

and call your-script.py with :

PYTHON3=$(type python3) your-script.py ...
1

Because you are using a different interpreter/virtual environment for each project, which is generally considered the best practice.

You can apply the command below to create a file with all your installed modules, so you can use them whenever you want, by a single command to install all.

To keep/save all modules in a file:

pip freeze > requirements.txt

To install all of them with a single command in a new interpreter/virtual environment:

pip install requirements.txt

In case you tried installing a package and get an output:>>Requirement already satisfied.

You will find a path in your output where it says Requirement already satisfied, copy the path. Now go back to your working environment.

import sys
sys.path.append("/the/path/you/copied")

import schedule
6
  • 1
    so how do I know to which venv do I install it? As far as I'm concerned, I have everything in the same venv/interpreter (see the comment up). Thanks! Aug 6, 2022 at 14:43
  • 1
    You can also run in terminal pip3 list | grep schedule and if it is installed you will have a version output, something like schedule 0.6.0
    – Jamiu S.
    Aug 6, 2022 at 15:05
  • 1
    Yes I have a version output schedule 1.1.0, that's why this behavior is so weird to me. If you have any idea more why it doesn't work I would appreciate it, otherwise I will have to find a way how to do timed functions without this library... Aug 6, 2022 at 15:23
  • 2
    Ok now what you can do is, in your output where it say's: ` Requirement already satisfied` you will see an absolute path, copy the path. Now go back to your python file, before import schedule. You should import sys and then sys.path.append("the/path/you/copied")
    – Jamiu S.
    Aug 6, 2022 at 15:26
  • Whoaaa, it works! You are literally my lifesaver, I'm so happy right now. Thank you so much!!! Aug 6, 2022 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.