Modules from specified sub-folders not recognized/loaded

Hi there,
this might be a bit embarrassing, but I am having trouble with importing custom modules.

The search paths are specified in the options of the PythonEditor of course.

C:/folder1
C:/folder1/subfolder

The superior folder is recognized, but the subfolder is not.
To me it does basically make no sense at all, but maybe I am overlooking something.

The reason I am doing this is to organize my modules a bit more tidy.
I also found a similar issue here.

Thanks,
T.

Hi Tobias,
Is ‘C:/folder1/subfolder’ found inside the following list:

import sys
print (sys.path)

@djordje , yes. Just checked and it is contained in the list.

Okay. Not sure really.
Can you position all the modules as subfolders inside the C:/folder1?
For example:
C:/folder1/module1, C:/folder1/module2

I did and now it does not recognize anything at all, @djordje .
It recognizes the following though:

from folder1.module1 import class1

is fully recognized by autocomplete.

When I run it, I get the following:

No module named folder1.module1

Try making the __init__.py file inside each of the C:/folder1/module1, C:/folder1/module2. With the following code inside it:

import file1
import file2

Where file1, file2 are .py files inside the C:/folder1/module1, C:/folder1/module2 folders.

Hi @djordje , I guess this is working.
Thanks first of all.

As Steve described here…

“Python was designed with the concept of modules and packages where sets of scripts are grouped together inside a directory (package). I don’t want to make any change that breaks this behavior.”

My problem is that files from one folder need the other ones.
So it would be a “bummer” to organize them in folders and then having to keep the init files up to date every time you add a new file to a folder, right?

Are there any other strategies to organize the code or do you think I simply have to live with that?

Thanks,
T.

Hm, I see what you mean. Yes, it can be tedious. Maybe someone else has an advice for that.

As I am working with this incredible tool

I thought maybe creating workspaces in vs code might work.

I will test this as soon as I find time and let you know.

1 Like

Hi Tobias,

The __init__.py files do not need to contain anything they can be left empty.

If you want to load a module from a sibling folder you wil need to add the common root path to the sys.paths like so

sys.path.append(root_dir)

Does that nake sense?

Im on my phone now but let me know if I need to elaborate.

1 Like