For this file layout:
devel/
pkg/
__init__.py
moduleA.py
moduleB.py
test/
__init__.py
test_A.py
test_B.py
If I am in the directory which contains the pkg (devel), I can run:
python -m pkg.test.test_A
But what if I want to run the same but with absobule path? I tried:
python -m /Users/me/docs/devel/pkg.test.test_A
Assuming I don't want to do the following and change the directory in my bash script:
cd /Users/me/docs/devel/
python -m pkg.test.test_A
Is there any direct way from python command?
asked Apr 16, 2014 at 17:16
Diolor
13.5k31 gold badges121 silver badges184 bronze badges
1 Answer 1
Add /Users/me/docs/devel to your module search path:
PYTHONPATH=/Users/me/docs/devel python -m pkg.test.test_A
answered Apr 16, 2014 at 17:24
chepner
539k77 gold badges596 silver badges748 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Diolor
So that's the only way? Because you know, you can run any sole file with the absolute path
python /path/to/file/file.py. Thankschepner
Either way should work, although note that you can't mix file paths and dotted module names; you would need
python /Users/me/docs/devel/pkg/test/testA.py.Diolor
Yes I tried that unsuccessfully on my first attempt. I have relative imports (
from .. import) inside the testA so I can only run it as a module. Pythonpath should be fine. Thank youdefault