Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Dynamically importing libraries with importlib on setuptools entrypoint

$
0
0

What I am trying to achieve

My goal is, after installing the python package, to be able to run a command from a script that was not installed together.

Example

$ excommandHello!

Directory Structure

.├── A├── __init__.py└── cli.py├── .venv├── pyproject.toml├── setup.cfg├── extra.py└── extra_test.py

cli.py

import importlibdef main():    getattr(importlib.import_module('extra'), 'hello')()

extra.py

def hello():    print("Hello!")

extra_test.py

import importlibgetattr(importlib.import_module('extra'), 'hello')()

pyproject.toml

[build-system]requires = [ "setuptools" ]build-backend = "setuptools.build_meta"

setup.cfg

[metadata]name = Aversion = 1.0.0[options]packages = find:[options.entry_points]console_scripts =     hello = A.cli:main

The Problem

After installing the package:

$ pip install .

The command hello does not work because importlib raises an Error:

$ helloModuleNotFoundError: No module named 'extra'

What was tried

I have assured that has something to do with the installation or importlib, because if I run python3 extra_test.py it runs normally and it does exactly the same as the command hello is suposed to do.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>