Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip over levels of a directory when importing python package

I've got a directory structure like:

Folder_in_PYTHONPATH/
├── Package1
│   ├── __init__.py
│   ├── src
│   │   ├── Class1.py
│   │   ├── Class2.py
│   │   └── __init__.py
│   └── test
│       └── testfile.py
├── Package2
│   ├── __init__.py
│   ├── src
│   │   ├── Class1.py
│   │   ├── Class2.py
│   │   └── __init__.py
│   └── test
│       ├── test1.py
│       └── test2.py
.
.
.

When I import things from this folder, I need to always type

import Package1.src.Class1

Is there any way to set up my __init__.py so that I can just type

import Package1.Class1

instead?

like image 430
George Avatar asked Oct 19 '22 03:10

George


1 Answers

Add them into your packages' __init__.py files so they look like:

from src import Class1
from src import Class2

Have a look at the docs

I would recommend putting the *.py files in the top level folder of their package to get the import Package_1.Class1 behaviour you are after. The unit tests can stay in their own folder to keep them separate.

like image 79
Holloway Avatar answered Oct 21 '22 15:10

Holloway