Home > Enterprise >  Cannot import python class from another folder
Cannot import python class from another folder

Time:11-24

I have created 2 classes Connections.py and LogObserver.py

I am trying to import Connections.py in LogObserver.py but python keep throwing an error

ModuleNotFoundError: No module named 'connections'

The way i am importing it is

from connections.Connections import Connections


class LogObserver:

The file structure is enter image description here

CodePudding user response:

If your Class name in Connections file is called Connections then you can try following:

from connections import Connections
c = Connections.Connections()

Or:

import connections.Connections as myModule
c = myModule.Connections()

make sure when you import that you do following:

from <folder>.<filename> import <class_name>

CodePudding user response:

There is a problem in your file structure. Try keeping connections folder inside queries folder OR specify the file path for connections.py correctly.

Hope it resolves the issue.

  • Related