0

I am working on a project based on Python 2.7 and I am trying to import a module which is in a package folder that contains __init__.py and the file that I want to import called package1.py, but I am unable to do so. This is my folder structure: main_project/Tools/common/package1.py

Note that my project files are in the folder main_project. So, I am trying to call the package1.py by using an import statement in my script:

from Tools.common.package1 import variable

But I am getting an ImportError: No module named Tools.common.package1.

What is the solution to solving this error as I want to use the package feature for my project?

martineau
  • 119,623
  • 25
  • 170
  • 301
Ashish101
  • 135
  • 10
  • Your directory structure is unclear. Please [edit] your question and try formatting the layout as an indented list of file and folders starting from the root. – martineau Oct 08 '20 at 23:47

2 Answers2

0

Maybe use the solution i found here :

# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')

import file

or verify your module has a __init__.py

Importing files from different folder

Amiral ASTERO
  • 365
  • 1
  • 6
0

Ok I have found the answer. I also had to insert an init.py in the folder Tools as well. Initially I only inserted init.py in common but not in Tools as we should also make Tools a package if we want to access common

Ashish101
  • 135
  • 10