1

I am currently having an issue with importing files from other directories in my python project.

My current file structure is

Project
 - Backend
     - Config
         + __init__.py
         + databaseConfig.py  
     - DataAccess
         + __init__.py
         + sqlConns.py
     - __init__.py
     - api.py
     - main.py
     - setup.py
   

What I am trying to do is import /Config/databaseConfig.py file into /DataAccess/sqlConns.py file. I get the following error when trying to run the sqlConns.py file

PS C:\source\repos\aaStats\aaStats> py .\Backend\DataAccess\sqlConns.py      
Traceback (most recent call last):
  File "C:\source\repos\aaStats\aaStats\Backend\DataAccess\sqlConns.py", line 2, in <module>
    import Config.databaseConfig
ModuleNotFoundError: No module named 'Config'

I have also tried using relative imports, but I am met with another error.

PS C:\source\repos\aaStats\aaStats> py .\Backend\DataAccess\sqlConns.py      
Traceback (most recent call last):
  File "C:\source\repos\aaStats\aaStats\Backend\DataAccess\sqlConns.py", line 2, in <module>
    from ..Config import databaseConfig as dbcfg
ImportError: attempted relative import with no known parent package

Config/databaseConfig.py contains database configuration parameters that I want to reference is various places in my project. It isn't a huge deal if I had to move this single file in order to get it to be referenced properly, but I will want to use structures like this for files later on in my project.

Here are some details about my files:

/Config/__init__.py

from . import databaseConfig

/DataAccess/__init__.py

from . import sqlConns

Backend/__init__.py

from . import DataAccess
from . import Config

Backend/setup.py

from setuptools import setup, find_packages

setup(
    name='aaStatsApi',
    version='0.1.0',
    packages= ['DataAccess','Config'],
    install_requires=[
        'fastapi==0.63.0',
        'uvicorn==0.13.4',
        'requests==2.25.1',
        'pyodbc==4.0.30',
    ]
)
Paul P
  • 3,346
  • 2
  • 12
  • 26
Dave
  • 13
  • 4
  • I would check out [this answer](https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python). – trozzel Mar 10 '21 at 21:42

1 Answers1

0
  1. Check out this post.
  2. The fact that you can't perform relative imports so easily is by design, for better or for worse. The ideal way is have your main script in the root (Backend) directory and do all your calls from there. The function that has __name__ == __main__ is your calling function. If you do not directly call Calls.py or Configs.py from a console, but are calling them from another main function within your root directory, you should be able to place the following into Conns.py:
# FILE: DataAcess\sqlConns.py
from Config.dataBaseConfig import * # or whatever you need to import

Again, the key is to ensure that your starting point in from your root project directory.

  1. NOT RECOMMENDED: For risk of getting downvoted, and I do not recommend this, but you could append the relative path to your calling class:
import sys, os
sys.path.append(os.path.abspath("../Config"))
from sqlConns import * # or whatever
sys.path.pop() # clear sys.path
trozzel
  • 479
  • 5
  • 12
  • 1
    Thank you! That makes a lot of sense and everything does work when say importing and accessing ```Configs.py``` from ```api.py``` – Dave Mar 10 '21 at 22:01