Python: Include Library Folder When Running From Command Line
I have a directory structure: root_dir ├── src │ └── p1.py └── lib ├── __init__.py ├── util1.py └── util2.py I want to
Solution 1:
You need the following steps
- Add
__init__.py
at lib folder.
Add this line at p1.py file on top
import sys
sys.path.append('../')
import lib.util1 as u1
Run the p1.py file from src dir. Hope it will work.
Edit:
If you do not want to add sys.path.append('../')
, set PYTHONPATH in env-var from this resource.
How to add to the pythonpath in Windows?
Solution 2:
Improving on Saiful's answer, You can do the following which will allow you to run the your program from any working directory
import sys
import os
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), "../"))
import lib.util1 as u1
Post a Comment for "Python: Include Library Folder When Running From Command Line"