ImMikeUrNot
ImMikeUrNot

Reputation: 25

Pycharm python console path change

I am using Pycharm and I have several .py files in my Collection project directory.

When I am using python console I can import whichever class I want, module (as long as it exists). But after some time the directory got a little messy so I tried re-organizing it into subdirectories/files based on their common grounds. (like Calculator subfile includes several .py files with calculating programs etc. But while working with those nested files I cannot use Python Console - I cannot import any custom made classes:

I get an error: ModuleNotFoundError

I believe it is because the sys.path.extend is not...extended to that very Calculator subdirectory.

I really do not want to have all of them in one directory because it can get quite messy. How do I change the path for that particular subfile?

Upvotes: 2

Views: 1719

Answers (1)

John Moutafis
John Moutafis

Reputation: 23154

You may have forgotten to add __init__.py files on your newly created sub-folders. Add this file to every module folder, as shown below:

project_parent_folder
 |_ module1
 |   |_ __init__.py
 |   |_ module1_code.py
 |_ module2
     |_ __init__.py
     |_ module2_code.py

The __init__.py file can be empty, but it is utilized by python to recognize a folder as a package containing code as stated here: https://docs.python.org/2/tutorial/modules.html#packages

Upvotes: 1

Related Questions