1

I wrote a package, which lives in /home/michael/python/mcdb-mail-parser/

The file structure in there is:

 .
 ├── __init__.py
 ├── mcdb_mail_parser
 │   ├── __init__.py
 │   ├── MCDBAttachment.py
 │   ├── MCDBEmail.py
 │   ├── Options.py
 ├── mcdb-mail-parser.conf.sample
 ├── mcdb-mail-parser.py
 ├── README.md

mcdb-mail-parser.py imports from the mcdb_mail_parser subdirectory.

if I run the scripts from the source directory (/home/michael/python/src/mcdb_mail_parser) it works fine because the mcdb_mail_parser directory is immediately available in the current directory. However, I need to run it from the home directory of another user (via a cronjob, or from another script via subprocess), python complains it cannot find the module:

I tried to execute it with python3 -m /home/michael/python/src/mcdb_mail_parser, but it complains:

michael@d8:~$ python3 -m /home/michael/python/mcdb-mail-parser/
/usr/bin/python3: No module named /home/michael/python/mcdb-mail-parser/

I am not sure where to go from here. I think that it's a path issue. I could add /home/michael/python/src/mcdb_mail_parser to the system path, or perhaps python path, but that seems like the wrong solution. I certainly don't want to hard code paths into any scripts either.

How do I tell python: "Run the mcdb-mail-parser.py script from /home/michael/python/src/mcdb_mail_parser directory?

Closing notes The accepted answer was useful, and so was the link they provided. Here's what I eventually did: 1. I moved the contents of mcdb_mail_parser from the subdirectory into the same directory as README.md, thus removing one level of complexity. 2. I added the import statements to __init__.py as suggested. 3. Python complained it couldn't find __main__.py, so I renamed mcdb-mail-parser.py to __main__.py

DrDamnit
  • 4,736
  • 4
  • 23
  • 38
  • 1
    `python -m` wouldn't work here anyway because there's no `__main__.py`. You could just leave the `-m` out. –  Nov 15 '17 at 02:56

1 Answers1

1

List the modules in the __init__.py that's in the sub directory and then have the Import in mcdb-mail-parser.py reference that directory

Very similar to this previous StackOverflow Post -> Loading all modules in a folder in Python

MosaicOrange
  • 90
  • 12