Here is my directory tree:
prediction_model ├─ prediction_model | ├─ __init__.py | ├─ data | | ├─ SAAA.csv | | └─ VDFF.csv | ├─ models.py | ├─ preprocess.py | ├─ README.md | └─ tests └─ setup.py Here is my ‘setup.py’:
from setuptools import find_packages, setup setup( name='prediction_model', version='0.7', url='https://project.org/', author='JL', author_email='[email protected]', packages=find_packages(), scripts=['models.py', 'preprocess.py'] ) Here is my ‘__init__.py’:
from prediction_model import models from prediction_model import preprocess ‘models.py’ has a function main and ‘preprocess.py’ has a function run that I want to use.
I install the project using:
python -m pip install --user . Then I run the following code in the Python interpreter but it raises an exception AttributeError:
>>> import prediction_model >>> prediction_model.src.main() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'prediction_model' has no attribute 'src' >>> prediction_model.src.run() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'prediction_model' has no attribute 'src' >>> import prediction_model >>> prediction_model.main() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'prediction_model' has no attribute 'main' >>> prediction_model.run() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'prediction_model' has no attribute 'run' What am I doing wrong?
Environment: Python 3.7, MacOS.
import prediction_modelin your client code, it runs implicitly the __init__.py file. Since in it you dofrom prediction_model import preprocess, models, thepreprocessandmodelsmodules will be in your namespace. Then instead of doingprediction_model.src.main()in your client code which raises anAttributeErrorsince there is nosrcobject in your namespace, you should just doprediction_model.preprocess.run()andprediction_model.models.main().prediction_model.run()andprediction_model.main()in your client code, you should dofrom prediction_model.preprocess import run; from prediction_model.models import mainin your __init__.py file.scriptsargument should be['prediction_model/models.py', 'prediction_model/preprocess.py']instead of['models.py', 'preprocess.py'](since relative paths are resolved against the directory of setup.py). In addition the models.py and preprocess.py files need a shebang line at their top or the installation will fail:#!/usr/bin/env python. But since they are Python scripts and are part of theprediction_modelpackage, you should use theentry_pointsargument instead of thescriptsargument and you won’t need this shebang line.