Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add path to source code in python (pycharm) project

I have inherited a project that has code in a package that has imports that are assuming code in that package are in the root directory.

For example, given this directory structure

my_project
  mod_1
  mod_2
  - folder1
    mod_3
    mod_4
    - folder2
      mod_5

mod_5 would have an import like:
  import mod_3
  import mod_4

When I try to run this code I get the error that mod_1 cannot be found.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If I change the imports to this everything seems to work

  import folder1.mod_3
  import folder1.mod_4

However, this is not a viable solution as this is part of a long running opensource project and clearly this is not the intent of the original authors and clearly something that has been working for others without the above modifications.

How do I craft a virtual environment that will essentially add folder_1 directly to the path such that modules in folder_1 can be referenced as being in the root folder?

>Solution :

In Pycharm, use this flow

Select the source dir -> Right-Click -> Choose Mark Directory As -> Choose Sources Root

It will make the directory as a root and solve the import issues.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading