Jul-20-2022, 09:13 AM
There are different ways to solve this problem. The first thing to understand is that there are two related but distinct hierarchies that must not be mixed up:
A very flexible solution to your problem is to build a library containing the modules 'info' and 'country'. Let us call this library 'greatlib'. We are going to package it in a way that allows you to import it from any other program. For this, create a directory hierarchy like this one:
In the file
In the file
- The file system tree of files and directories.
- The tree of Python packages and modules.
from A.info import spam
it is understood that we are speaking about the tree of Python packages, so A is a Python package and 'info' is a subpackage or a submodule. The import statement does not directly refer to the directories hierarchy.A very flexible solution to your problem is to build a library containing the modules 'info' and 'country'. Let us call this library 'greatlib'. We are going to package it in a way that allows you to import it from any other program. For this, create a directory hierarchy like this one:
Output:the_great_lib/
greatlibsrc/
__init__.py
country.py
info.py
setup.py
README.md
You can leave the file __init__.py
empty but it must exist.In the file
README.md
just add the line # Greatlib library
for example.In the file
setup.py
, create the following content:from setuptools import setup, find_packages setup( name='greatlib', version='0.1.0', packages=find_packages(include=['greatlibsrc', 'greatlibsrc.*']) )Once you are done, move to the directory the_great_lib in a terminal and type the command (don't forget -e and the dot)
Output:python -m pip install -e .
That's it, now from any other program, you can usefrom greatlib.info import Person