├── ledger
│ ├── __init__.py
│ ├── ledger_data.py
│ └── ledger_model.py
├── main.py
├── sscommon
│ ├── __init__.py
│ └── logging.py
└── tests
└── test_ledger_data.py
I need to import classes from ledger_data
module when running test_ledger_data.py
. I currently do sys.path.append("../")
in test_ledger_data.py
or I have to add symbolik links to all modules being used to tests
directory. Both options seem incorrect. How to do it correctly?
If I just run the file either from project root or tests
directories I get error:
from ledger.ledger_data import LedgerData
ImportError: No module named 'ledger'
You can create an __init__.py
file in your folder, and import the parent dir using:
parent_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), os.pardir))
sys.append(parent_dir)
This uses os.path
to find out the directory based on your file location.
Update : create the above __init__.py
and reside it inside tests/
folder. Then, in your test_ledge_data.py
put at the head of the file from __init__ import *
; this will import everything in your init file to your module namespace.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.