SlakNation SlakNation - 2 years ago 106
Python Question

having trouble importing python module from root directory in PyCharm

Here is my folder structure:

exception\ --> has "class SimulatorException(Exception):"

I opened the project from the "lib" folder in PyCharm. In the "" script, when I try to import SimulatorException by doing:

from pt_hil.utilities.PT_HIL_dSPACE_Utils.exception import SimulatorException

It doesn't work. However if I do:

from exception import SimulatorException

It works. Shouldn't the root directory be from the lib folder? When I look in settings the content root is set to the lib folder.

Answer Source

One of the purposes of a file, is to mark the directory as a valid Python package that you can import.

You only declared the exception folder to be a valid Python package and therefore that's the root package you can start importing from.

It's either you create top level packages by adding empty files to each top level folder you have and then can import as you originally wanted, or keep the current structure and import directly from exception package.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download