Search code examples
pythonpython-module

Importing a module from another module in python


I know this question is very common and may be tagged as duplicate, however I have read almost all answers and nothing helps. I am writing this question in desperation. My file structures is the following:

my_project
|--__init__.py
|--some_file.py
|--module1 
  |--a.py 
  |--__init__.py
|--module2
  |--b.py
  |--__init__.py

what I want to do is to import functionality of a.py in b.py I write

from module1 import a

This throws an error "there is no module named module1"
I am working in python3.7.4


Solution

  • What you are doing there is called a relative import, you can read more about them here. try doing the following.

    from .module1 import a
    

    A relative import specifies the resource to be imported relative to the current location.