0

When I run code importing a module, I get: ModuleNotFoundError: No module named '[module name]'

However, if I run the Python file in the terminal , in a virtual environment, it works.

I am working with:

  • Python 3.10.6
  • Vscode (version 1.75.1 - Universal)
  • venv
  • mac os Monterrey

My workflow for starting and working on a project (at the moment is learning exercises as I am a beginner) is the following:

  • I create a folder for the project, using mac Finder
  • In vscode I open the project folder
  • In vscode terminal, inside the project folder, I create a virtual environment for the project, using: python3 -m venv env.[some name]
  • I activate the virtual environment with: source ./env.[some name]/bin/activate
  • Using the command palette, I select the interpreter of the virtual environment i just created
  • In the vscode terminal screen, with the activated venv, I install modules using: python3 -m pip install [module name]
  • Inside the project folder I create the .py files

I don't understand why running the code does not find the module, but running the Python file works.

I use this dropdown to either run code or run file in vscode

  • Have you read this yet? It might help https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time – Arseny Feb 24 '23 at 02:16
  • Depending on what you use to run your code, python's module detection might work differently – Arseny Feb 24 '23 at 02:19
  • It could also help if you included the source code. It's best to reduce it to the minimal version of it that still causes the problem – Arseny Feb 24 '23 at 02:22
  • are you sure that you choose your custom env as a Python interpreter in VS? – Hoang Minh Quang FX15045 Feb 24 '23 at 02:27
  • HoangMinhQuangFX15045 - Yes, I verified, and I chose the custom env as the Python interpreter Arseny - Thisline of code still causes de error: import matplotlib.pyplot as plt – Diego Brenes Feb 24 '23 at 02:53

0 Answers0