Python Forum

Full Version: [FIXED] User-defined module: ModuleNotFoundError error in VSCode but not in PyCharm
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear all,

My apologies in advance for such a simple question, but I've spent hours browsing on the Net w/o obtaining a solution. I am newbie to Python and coding, and I am working on a project for my studies. I am using VSCode casue PyCharm is too heavy for my computer.

I have a project with the following structure:

Project
|_ database: __init__.py, connection.py
|_ recipes: __init__.py, recipe_modules.py, recipe_functions.py
|_ (other irrelevant packages)

The package recipe_modules.py has the following statements:
import peewee
import datetime
from database.connection import BaseModel, db
And when running the module I get the following error in VSCode:
Error:
ModuleNotFoundError: No module named 'database'
However, when I run the module in PyCharm I do not get any error - meaning the statements are correctly defined and all the rest is OK.

I know is an issue with the configuration of VSCode but I have spent hours w/o being able to fix it Wall .

Btw, my file .vscode/launch.json already has the follwing code:
{
        "env": {
            "PYTHONPATH":"${workspaceFolder}"
            }
{
I am absolutely lost here, and this is killing me. Hopefully you have already faced this.

Thanks in advance for your help!
A quick guess would be that two different versions of python are being used.
Bring up a terminal (command) window in each, and type python -V,
see if versions match.
In VSCode, you can change python version with ctrl-shift-P then "Python: Select Interpreter"
Don't remember pycharm's command
(May-31-2021, 03:26 AM)Larz60+ Wrote: [ -> ]A quick guess would be that two different versions of python are being used.
Bring up a terminal (command) window in each, and type python -V,
see if versions match.
In VSCode, you can change python version with ctrl-shift-P then "Python: Select Interpreter"
Don't remember pycharm's command

Hi Larz60+,

Thanks a lot for your suggestion. I did what you suggested, and both IDEs replied to use the same version:

PyCharm:
import sys
print(sys.version)
Output:
3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]
VS Code
import sys
print(sys.version)
Output:
3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]
Yesterday in a desperate attempt I checked if other IDEs would solve the issue: I installed Atom, Miniconda, Anaconda, etc... Nothing fixed this.

This morning I tried forcing adding the path to the the sys.path, bud didn't work neither:
x = os.getcwd() + "\\database"
sys.path.append(x)
print(sys.path)
Output:
'C:\\Users\\Alejandro\\Desktop\\Python\\Curso Profesional de Python AEPI\\Proyecto Final - Alejandro Rio\\database' Traceback (most recent call last): File "c:\Users\Alejandro\Desktop\Python\Curso Profesional de Python AEPI\Proyecto Final - Alejandro Rio\recipes\recipe_models.py", line 16, in <module> from database.connection import BaseModel, db ModuleNotFoundError: No module named 'database'
Now the full path appears when printing sys.path, but the module is still not recognize. I thought this would fix it, but is not working.

I am totally lost here Huh .

Cheers.
It's not the IDE, I have been using VSCode for quite a few years and never saw such a problem.
Difficult without seeing full code.
Perhaps someone else has had a similar issue and will suggest solution.
ModuleNotFoundError in user-defined module fixed

After some hours of reading and a lot of trial and error, I managed to fix the ModuleNotFoundError error programatically.

System
Windows 10 21H1
Python 3.9.5
VSCodium 1.56.2


Solution

1. Check in PowerShell the environments loaded in Windows with the command:
dir env:

This shown me I didn't have a PYTHONPATH entry in the Windows environment - however, when I wrote python the program started...

I set the variable with the command (following this post):
SETX PYTHONPATH C:\Users\my_user

I link the variable to the folder my_user cause I don't want to put my project's folder in the variable.

I check it was properly created:
SET PYTHONPATH

2. Now I go to my code in Python. I need to upload the proper PYTHONPATH. I do it by including the following code before I invoke the module causing the problem:

import os, sys
sys.path.append(os.getcwd())
* For some reason I tried this before but didn't work. I am not sure how necessary is to do step 1., but I did it just in case.

I include the code above in every module in which I want to import user-defined modules in. However, this creates a lot of garbage (__pycache__ folders in each package from which I import modules).

3. To avoid the generation of this garbage, I included the following command following this post:

sys.dont_write_bytecode = True
So my final code looks like this:

import os, sys
sys.dont_write_bytecode = True
sys.path.append(os.getcwd())
from [user-defined module] import [user-defined function]
I don't know how heterodox this solution is, but seems not absolutely botch to me :) and for the time being could be enough.

I read a lot of posts, but those I especially remember are:

Python import, sys.path, and PYTHONPATH Tutorial -> super good

sys.path in Python

Python | os.environ object

And this post is the one which helped me go in the right direction.


Thanks a lot to Larz60+ for his / her suggestions and help!

Take care!
env defines pythonpath for debug and run without debug but does not do so for the terminals or the run button. Annoying. I use f5 to run rhe program.