1

I am new to python. Although the language is clean and easy to learn, I am finding a hard time understanding the mechanics of imports. I have searched a lot about it on the web, most of the articles are not comprehensive and are rather confusing for a beginner like me. So, once for all, I want to ask the experts here to help me understand this concept.

Python3 --version = Python 3.10.1

System - Mac M1 (2020)

Problem 1 - I have the following folder structure -

Package1/
        /__init__.py
        /module1.py
        /module2.py
        /SubPackage1/
                    /__init__.py
                    /subpack1_module1.py
        /SubPackage2/
                    /__init__.py
                    /subpack2_module1.py

Now I want to import the entire SubPackage2 in SubPackage1 to use its modules. This is the code I am writing in the '__init__.py' file of SubPackage1

from Package1 import SubPackage2

This is showing me the following error -

ModuleNotFoundError: No module named 'Package1'

What is the right way to import one sub-package into another sub-package? Although I have found a cheat to this as follows -

import sys
sys.path.append('root/Package1/')
import SubPackage2

But I seriously feel that this method is a noob method and is not scalable.

Problem 2 - Now let's say I have completed the above package and want to use it in another Folder/Package/Program, in the same way, we use Pandas by just writing import Pandas as pd. What is the best way? To be more clear let me define the folder structure -

Package 1/
         /__init__.py
         /all other subpackages and modules
Folder 2/
        /test.py

Now in test.py I want to use Package1. So I want to write on test.py -

import Package1

But again it gives me the same ModuleNotFoundError. sys.path.append('path') trick works but it is not scalable. Another way is to create wheels (using setup.py), but in that case, how to use wheels in development mode so that I don't have to create wheels every time there is a change in the version.

1 Answers1

0

This is probably one of the best features of python over other languages. The importing is simple.

Package1/
        /__init__.py
        /module1.py
        /module2.py
        /SubPackage1/
                    /__init__.py
                    /subpack1_module1.py
        /SubPackage2/
                    /__init__.py
                    /subpack2_module1.py

You can import directly from any file in the same folder or below like this:

If you are in module1.py and want to import code from module2.py you simple do this:

import module2

you can even add a convenient alias like this:

import module2 as m2

You then have access to all the classes, attributes and functions of module2.

likewise, you can import from subfolders in a very similar way (as python sees all the subfolders). So if you are in module1.py and want to import subpack2_module1.py you do the same. Here we use the dot notation for the folder paths.

import SubPackage2.subpack2_module1

again, you could even add an alias like this:

import SubPackage2.subpack2_module1 as sp

Using the above, there is no ambiguity and the structure is clean. The coder know exactly where the code is situated.

note: There are also ways of importing from modules outside of the directory structure, but that is less common as the directory represents the project.

D.L
  • 4,339
  • 5
  • 22
  • 45
  • How to import subpack1_module1.py in subpack2_module1.py? – Anupreet Choudhary Mar 12 '22 at 14:22
  • Typically, whilst possible, this is less common (logically one traverses down the folders). The importing works best (conveniently) looking `in the same folder` and the `children folders`. However, you could use relative imports like this: `from ... import some_package`. Here is a link that explains this too: https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder – D.L Mar 12 '22 at 14:29
  • Thanks for the reply. But I am getting this error - `ImportError: attempted relative import with no known parent package` – Anupreet Choudhary Mar 13 '22 at 01:53
  • This is a subject for a new question. In order to answer that users will be asking which operating system, path and filenames you are using... – D.L Mar 13 '22 at 08:03