0

Structure:

|--folder/
    |--a.py
|--main.py

When loading module 'main' into module 'a'

#a.py
import main

the following error occurs - ModuleNotFoundError: No module named 'main'.

PyCharm copes with this task, but VSCode does not. What's the matter?

1 Answers1

0

Looking at your directory structure, I presume that a.py and main.py are in different directories. If that is the case, then the answers to [this question][1] should be useful. To elaborate a bit further, you can use the sys module to specify the path to the other module (main.py) i.e. if main.py is in a different directory from a.py.

import sys
sys.path.append('/path_to_main_module_directory/')

import main
print("This import is for the main module")

I hope this helps! [1]: Importing files from different folder

Hartek66
  • 93
  • 1
  • 4
  • thanks. great answer other than that it`s hardcoded. What to do if I transfer the project to another server. – Anton Migulev Dec 21 '22 at 20:13
  • I used an absolute path in my example, you should use a relative path, so if you move your project to a different server, it will/should be mapped to the directory specified. Lookup absolute and relative paths...just in case :) – Hartek66 Dec 21 '22 at 20:17