Python Forum
[FIXED] User-defined module: ModuleNotFoundError error in VSCode but not in PyCharm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[FIXED] User-defined module: ModuleNotFoundError error in VSCode but not in PyCharm
#1
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!
Reply
#2
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
Reply
#3
(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.
Reply
#4
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.
Reply
#5
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!
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] ModuleNotFoundError: No module named 'PyQt5' matklt 2 16,717 Jun-02-2020, 07:05 AM
Last Post: Knight18
  [Kivy] ModuleNotFoundError: No module named 'kivy' Exsul 7 30,288 Mar-30-2019, 12:58 AM
Last Post: Exsul
  "ModuleNotFoundError: No module named '_tkinter' in Python 3.8 Alfa 0 programmerc 1 6,351 Oct-21-2018, 06:32 PM
Last Post: Larz60+
  [Tkinter] Scroll bar height is not fixed with Text widget MeeranRizvi 2 15,455 Feb-06-2017, 12:24 PM
Last Post: MeeranRizvi
  [pygtk] fixed.move not working as expected DennisT 4 6,138 Nov-10-2016, 11:36 PM
Last Post: DennisT

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020