0

I have a project structure like:

MyPackage
|
├──mypackage
|     |
|     ├── __init__.py
|     |
|     ├── config.py 
|     |
|     ├── data_clean
|     |   ├── __init__.py
|     |   └── f1.py
|     |
|     ├── data_transform
|     |   ├── __init__.py
|     |   └── g1.py
|     |
|     └── stat_calc
|         ├── __init__.py
|         ├── s1.py
|         └── command_line_interface.py # <- users will use this from cmd.exe
| 
├── README.txt    
|
└── setup.py

I need to be able to do python path/to/command_line_interface.py arg1 but when I do this I get ImportError: No module named 'stat_calc'. Inside of command_line_interface.py I have tried putting both:

import sys
sys.path.append('path/to/MyPackage')

and also:

import sys
sys.path.append('path/to/MyPackage/mypackage')

and also:

import sys
sys.path.append('path/to/MyPackage/mypackage/data_clean')
sys.path.append('path/to/MyPackage/mypackage/data_transform')
sys.path.append('path/to/MyPackage/mypackage/stat_calc')

but none of them worked. When I run all my stuff in my Pycharm Editor, without command line, my packages like stat_calc and data_clean import from each other using import mypackage.data_clean.f1 syntax and that seems to work fine, but running from command line doesn't work.I'm not sure what I'm doing wrong when trying to setup command line access. All of my init.py files are empty.

user1367204
  • 4,549
  • 10
  • 49
  • 78
  • `python -m mypackage.statcalc.command_line_interface [args]` while cwd is `MyPackage`? – Łukasz Rogalski Apr 13 '17 at 13:14
  • `cwd` or `current working directory` would be like my `C drive` or anything else. So `cwd` could be anything, not just `MyPackage` – user1367204 Apr 13 '17 at 13:20
  • My method will allow you to run your code without further actions. Said working directory is necessary for proper discovery of your package. In terms of general solutions you are looking for [`entry points`](http://stackoverflow.com/questions/774824/explain-python-entry-points), which will create relevant "runner" (entry point) during installation of package. Notice that after installing a package it'll be discovered in standard Python package location therefore it'll be runnable from any working dir. – Łukasz Rogalski Apr 13 '17 at 13:30
  • Just to repeat what you said to make sure I understand, I should `cd` into `MyPackage` and then do `python -m mypackage.statcalc.command_line_interface [args]`, is that right? – user1367204 Apr 13 '17 at 13:33

0 Answers0