Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to import locally installed library?

I’ve installed my library using the following command:

pip install .

Here is the directory structure:

└───module1
    ├───__init__.py
    └───mod_1.py
└───module2
    ├───__init__.py
    └───mod_2.py
__init__.py
setup.py

inside setup.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from setuptools import setup,find_packages
setup(
    name = "my_lib",
    version="1.0.0",
    packages=find_packages(),
    python_requires='>=3.7',
    include_package_data=True,
    zip_safe=False)

Installed in:

.\Python\Python312\Lib\site-packages
└───module1
    ├───____pycache__
    ├───__init__.py
    └───mod_1.py
└───module2
    ├───____pycache__
    ├───__init__.py
    └───mod_2.py
└───my_lib-1.0.0.dist-info
    ├───direct_url.json
    ├───INSTALLER
    ├───METADATA
    ├───RECORD
    ├───REQUESTED
    ├───top_level.txt
    └───WHEEL

Expected behavior/import:

from my_lib import mod_1, mod_2

Current error is:

ModuleNotFoundError: No module named 'my_lib'

Work around:

import mod_1, mod_2

Need help with:

What do I need to change in my structure\setup.py in order to import "my_lib" as following?

from my_lib import mod_1, mod_2

>Solution :

Move module1 and module2 into a my_lib directory, and add this to my_lib/__init__.py (also note I removed the root __init__.py as it is not needed.)

from .module1 import mod_1
from .module2 import mod_2
.
├── build
│   ├── bdist.linux-aarch64
│   └── lib
│       └── my_lib
│           ├── __init__.py
│           ├── module1
│           │   ├── __init__.py
│           │   └── mod_1.py
│           └── module2
│               ├── __init__.py
│               └── mod_2.py
├── my_lib
│   ├── __init__.py
│   ├── __pycache__
│   │   └── __init__.cpython-312.pyc
│   ├── module1
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-312.pyc
│   │   │   └── mod_1.cpython-312.pyc
│   │   └── mod_1.py
│   └── module2
│       ├── __init__.py
│       ├── __pycache__
│       │   ├── __init__.cpython-312.pyc
│       │   └── mod_2.cpython-312.pyc
│       └── mod_2.py
├── my_lib.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   ├── not-zip-safe
│   └── top_level.txt
└── setup.py

13 directories, 21 files

Now the imports work as you expect

Python 3.12.0 (main, Nov 29 2023, 02:50:35) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from my_lib import mod_1, mod_2
>>> 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading