Importing a Python module from another directory

I have a python program that has the following project structure:

ProjectName
     |
     |----ProjectMain.py
     |
     |----__init__.py
     |
     |----Common
            |
            |----Dictionaries
            |         |
            |         |----Dicts.py
            |         |
            |         |----__init__.py
            |
            |----Core.py
            |
            |----LogoCreator.py
            |
            |----__init__.py

      

The contents of each file, except for all __init__.py

files, which are all empty, are as follows:

LogoCreator.py

def random_logo():
#    do stuff

      


 Core.py

from LogoCreator import random_logo
import Dictionaries.Dicts as Dicts

#do stuff

      


 Dicts.py

i_am_a_dictionary = {}

      


 ProjectMain.py

from Common.Core import *
import Common.Dictionaries.Dicts as Dicts

#do stuff

      


When I run ProjectMain.py I get the following error:

Traceback (most recent call last):
  File "ProjectName\ProjectMain.py", line 1, in <module>
    from Common.Core import *
  File "ProjectName\Common\Core.py", line 1, in <module>
    from LogoCreator import random_logo
ModuleNotFoundError: No module named 'LogoCreator'

      

This is the first time working with packages, so any help would be really appreciated.

+3


source to share


1 answer


If you want to use the random_logo method, you must use dot notation after importing LogoCreator.



from Common import LogoCreator
def create (self):
  x = self.random_logo()
  #code that generates a random logo

      

0


source







All Articles