From within Visual Studio Code [1.91.1] on my Raspberry Pi 4 Model B, running Debian 12, the code runs successfully when I start with Main.py using “Run and Debug”. I have installed all of the necessary packages within the virtual environment. However, when I try to run from terminal:
sudo python3 /home/pi/CodeAsynchronous/Main.py
The execution fails, ending with the error message ModuleNotFoundError: No module named 'xlrd'
.
The project uses a virtual environment and seems to not be loading the packages correctly when I run from terminal, possibly not loading the correct interpreter?
6
Probably because the IDE automatically activates the virtual environment for you.
Try running: source <venv>/bin/activate
on Linux or <venv>Scriptsactivate.bat
on windows
Full documentation here:
A virtual environment may be “activated” using a script in its binary directory (
bin
on POSIX;Scripts
on Windows). This will prepend that directory to your PATH, so that running python will invoke the environment’s Python interpreter and you can run installed scripts without having to use their full path. The invocation of the activation script is platform-specific (<venv>
must be replaced by the path to the directory containing the virtual environment):
1