Jul-20-2022, 09:24 AM
(Jul-20-2022, 09:13 AM)Gribouillis Wrote: 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:when you write
- 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:
You can leave the file
Output:the_great_lib/ greatlibsrc/ __init__.py country.py info.py setup.py README.md__init__.py
empty but it must exist.
In the fileREADME.md
just add the line# Greatlib library
for example.
In the filesetup.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)
That's it, now from any other program, you can use
Output:python -m pip install -e .
from greatlib.info import Person
Thank you so much, I'm going to impliment it right now. More power to you!