0

I have a folder structure as follows:

python_projects
|
|- common_tools
|  |- __init__.py
|  |- some_functions.py
|
|- some_project
   |- main.py

I currently import some_functions.py into main.py as follows, similar to what is described here:

import sys
import os

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.dirname(CURRENT_DIR))
from common_tools import some_functions

This seems to work correctly and Python raises no errors when calling some_functions.a_function(). However, Pycharm doesn't seem to recognise the references to common_tools or some_functions, and thus doesn't automatically suggest a_function() when I type some_functions.

I have seen answers which handle including modules within some_project, but I plan to use common_tools for several other projects (which would be located as siblings to some_project) and would prefer to keep the folder structure in such a way that I can set up each individual project to access common_tools without having to have a separate copy of the module within each project folder.

I have tried adding various folders to Content Root and/or marking them as Sources..., but I haven't found a combination that seems to work yet. What is the best way to handle this?

pt1243
  • 1
  • Add `python_project` as Content Root and `common_tools` as Sources. – Devang Sanghani Feb 17 '22 at 05:25
  • 1
    you may have to add to `sys.path` parent folder `os.path.join(CURRENT_DIR, "..")` if you want `from common_tools import some_functions` or `os.path.join(CURRENT_DIR, "..", "common_tools")` if you want `import some_functions` – furas Feb 17 '22 at 07:29
  • always put full error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information. – furas Feb 17 '22 at 07:33

0 Answers0