Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

Cant import my python modules despite being at the same level in the hierarchy

Im writing a library for internal use,its called "etllib", and I have the following structure:

etl-lib
├── README.md
├── etllib
│   ├── __init__.py
│   ├── client
│   │   ├── __init__.py
│   │   ├── elastic.py
│   │   └── qradar.py
│   ├── etl
│   │   ├── __init__.py
│   │   └── etl_imperva.py
│   └── util
│       ├── __init__.py
│       ├── config.py
│       ├── daemon.py
│       ├── elastic
│       │   ├── __init__.py
│       │   └── impeva_index_config.py
│       └── imperva
│           ├── __init__.py
│           ├── kpe_config.py
│           └── query_config.py
├── scripts
│   └── etl_imperva
└── setup.py

And I have a script called "etl_imperva" in etllib/scripts. The code inside looks like this :

#!/usr/bin/python3
import sys
from etllib.etl.etl_imperva import ETL


# Run with python3 imperva_run.py start|run|stop|restart
ETL.startup(sys.argv)

If I install this package(etllib) and call this script, it works just fine. But when I need to test stuff, how can I tell python to use the modules that are on my working directory instead the ones are installed? Because each time I make a change on the modules, I need to reinstall the package and this is a little time consuming.

I also tried uninstalling the package for testing, but whe I run this script I get the following error :

Exception has occurred: ModuleNotFoundError
No module named 'etllib'
  File "/home/jleonse/etl-lib/scripts/run_imperva", line 3, in <module>
    from etllib.etl.etl_imperva import ETL

Is there a better way to do this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually, it is not on the same level in the hierarchy.

from etllib.etl.etl_imperva import ETL would work only if etllib was in the same directory or in a directory in your system PATH, but the etllib is in the parent directory, hence it can not find it.

so you can make it work if you change the project structure to be:

etl-lib
├── README.md
├── etllib
│   ├── __init__.py
│   ├── client
│   │   ├── __init__.py
│   │   ├── elastic.py
│   │   └── qradar.py
│   ├── etl
│   │   ├── __init__.py
│   │   └── etl_imperva.py
│   └── util
│       ├── __init__.py
│       ├── config.py
│       ├── daemon.py
│       ├── elastic
│       │   ├── __init__.py
│       │   └── impeva_index_config.py
│       └── imperva
│           ├── __init__.py
│           ├── kpe_config.py
│           └── query_config.py
├── etl_imperva
│
└── setup.py

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...