1

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

3
  • Do you use a main.py or equivalent to run the whole project? If so, is the main file being run from the correct working directory? I.e. is python main.py command entered in the same directory where connections and queries folders are? Commented Nov 24, 2021 at 12:16
  • I am trying to run only the LogObserver class Commented Nov 24, 2021 at 12:18
  • Since it seems like you're using VSCode, make sure the path VSCode is running LogObserver.py from is in whatever folder the connections and queries folders are. You can change the working directory manually in VSCode terminal/console. Python tries to import local modules from the current working directory, meaning from the subfolders. It can not go up/back from the working directory. Commented Nov 24, 2021 at 12:25

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.