Issue importing modules

I am creating a python app and I have been able to import modules into files within the same directory but I seem to be having issues importing those same modules into python files in another directory.

Here is my project layout:

root/
     -__init__.py
     -data/
        -__init__.py
        -db.py
     -anomaly/
        -anomaly.py
        -__init__.py

In the anomaly.py file I am trying to import the db.py. Here is the import statement I am using.

from root.data.db import DB

This should work according to the docs: https://docs.python.org/3/tutorial/modules.html#packages

However I am getting the following message:

Traceback (most recent call last):
  File "anomaly.py", line 2, in <module>
    from root.data.db import DB
ModuleNotFoundError: No module named 'root'

I have tried reformatting the import command but no luck so far. According to the docs this should work so I was wondering if anyone knew how to get around this.

Your top directory is root?

You’re importing module from source directory…
Hope you’ve added top level directory to the source path…
Check this if you haven’t

Sorry didn’t realize I had put that into the question. To make it make sense the import command I was using was:

from root.data.db import DB

And I was getting root package not found errors

Also I should mention that I attempted importing via relative paths and got the same thing. I also tried setting the sys.path to the path of the project and tried importing that way. I was able to import the asset but the asset I was importing imports an asset and I get errors importing the asset from the asset I imported. Sorry if all that sounds confusing.

It’s very confusing asset, could you please clarify​:confused::confused:

Sorry for the confusion. So here is the module I am importing.

# Main DB
import os
import pandas as pd
from influxdb_client import InfluxDBClient, Point, Dialect
from influxdb_client.client.write_api import SYNCHRONOUS

# Local module within the same dir. This works when I run this module by itself
from query_builder import QueryBuilder

class DB:

        def _make_client(self):
                # Establish client            
                client = InfluxDBClient(url = url, token = token, org = org)
                return client



        def run_query(self, param, tank, time_range, time_unit, bucket = bucket):
                # Build out value and validity objects
                # Testing params=
                value_query_obj = QueryBuilder(param, tank, time_range, time_unit, bucket)

                # Set up client to run the query
                value_query = value_query_obj.query_builder()
                client = self._make_client()
                
                # Run queries
                query_api = client.query_api()
                value_records = query_api.query_stream(query = value_query)
                
                # Close out client connection
                client.__del__()
                
                # Create 2D array and use that to populate dataframe
                record_arr = []
                for records in value_records:
                        record_arr.append([records["_time"], records["_value_vals"], records["_value_valid"]])

                # Create dataframe 
                df = pd.DataFrame(record_arr, columns = ['timestamp', 'reading', 'validity'])
                        
                return df

Here is the module that I am trying to import this into:

import pandas as pd
import sys

# This does seem to import the module but causes other errors
sys.path.insert(0, 'C:/Dev/UpwardFarms/')
from data.db import *

databass_obj = db.DB()
#Testing data imports. Come back to this when you figure out how to do module imports
print_df = databass_obj.run_query("Temperature", "Tank1", "10", "m")
print(print_df)

This seems to work but I get the following error when I try to run the anomaly file:

Traceback (most recent call last):
  File "anomaly.py", line 4, in <module>
    from data.db import *
  File "C:/Dev/UpwardFarms\data\db.py", line 8, in <module>
    from query_builder import QueryBuilder
ModuleNotFoundError: No module named 'query_builder'

This is the project directory:

root/
     -__init__.py
     -data/
        -__init__.py
        -query_builder.py
        -db.py
     -anomaly/
        -anomaly.py
        -__init__.py

I would prefer to use absolute imports rather than setting the sys path manually. Especially since I seem to be getting errors otherwise.

1 Like

Sorry, I’m unable to get the cause of error…
maybe you should write it as data.query_builder (it shouldn’t work but give it a try)

1 Like

That actually seems to have worked. I’m going to keep working and see if the issue keeps coming up for other stuff. Thanks for your help! Really wish I could figure out why the absolute imports don’t want to work.

I didn’t even thought it would work…

Please reply here also if you find the cause…

Oh I will. Thanks for your help!