-1

I refactored some of my code and put it in individual directories, and suddenly the interpreter can't find any of my custom modules.

My package structure looks like this:

etl3
    __init__.py
    inst_config
        __init__.py
        config3.py
    inst_utils
        __init__.py
        etc...
    jira
        filetorun.py
    etc...

I'm trying to run the file in the jira directory. When it was just under the etl3\ directory it can find it due to them being in the same directory. Once nested however, it cannot find them.

I run the python interpreter, and print my sys.path. I can see the path to C:\etl3\inst_config\ on there, which is why I'm confused as to why my file in the jira directory can't find it.

flybonzai
  • 3,763
  • 11
  • 38
  • 72
  • What are the errors? – Jason Apr 22 '16 at 18:47
  • `ImportError: No module named 'inst_config'`. That module is just where it's been for the last however long. – flybonzai Apr 22 '16 at 18:48
  • On my `sys.path` I can see the module... I'm not sure why python isn't correctly finding my module – flybonzai Apr 22 '16 at 19:04
  • you need to show the actual structure of your project, and how you are trying to use import. The way packages work is [well documented](https://docs.python.org/3/tutorial/modules.html#packages). If you cannot answer your own question from the documentation then add much more information to your question. – Tadhg McDonald-Jensen Apr 22 '16 at 19:15
  • @TadhgMcDonald-Jensen I have updated my OP as requested. I am thoroughly stumped even after reading the documentation. – flybonzai Apr 22 '16 at 19:23
  • naive question but, do you have your `PYTHONPATH` set properly? – Hugues Fontenelle Apr 22 '16 at 19:26
  • @HuguesFontenelle I think so, but I have my module set in the `PATH` as well. When I run `sys.path` I can see the modules I have placed there. Then I have another variable called `PYTHONPATH` where I have also placed the paths to those locations. – flybonzai Apr 22 '16 at 19:30
  • so let me get this strait... you have been running your script from `C:\etl3\ ` for a while and after reorganizing your project you are wondering why you cannot make imports in the same way as if `C:\etl3\ ` was on `sys.path`? If you include `C:\etl3\inst_config\ ` on the path that will let you import modules **inside** the folder, not the folder itself. – Tadhg McDonald-Jensen Apr 22 '16 at 19:38
  • you may also want to look at [Relative imports in Python 3](http://stackoverflow.com/questions/16981921/relative-imports-in-python-3) for why running a submodule of a package doesn't make all that much sense – Tadhg McDonald-Jensen Apr 22 '16 at 19:40
  • @TadhgMcDonald-Jensen The command that was failing was `from inst_config import config3`. So that should be covered by having `C:\etl3\inst_config` on the `PATH` – flybonzai Apr 22 '16 at 19:40
  • 1
    if `config3` is in a folder that is on `sys.path` then you would just do `import config3` that is how the PATH works! Otherwise put `C:\etl3` on your path. – Tadhg McDonald-Jensen Apr 22 '16 at 19:42
  • @TadhgMcDonald-Jensen You solved it. If you want to write an answer I'll accept it. Thanks! – flybonzai Apr 22 '16 at 19:43

2 Answers2

2

When the inst_config folder is on the path you would simply import the files from it directly:

import config3

However when C:\etl3\ is on your PATH you can import modules that are defined there, in this case it would be the package inst_config and a submodule of it:

import inst_config.config3
# or
from inst_config import config3
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
0
from etl3.inst_config import YourClassHere

(recommended), or

import etl3.inst_config.config3

https://docs.python.org/2/tutorial/modules.html#packages

Hugues Fontenelle
  • 5,275
  • 2
  • 29
  • 44
  • 1
    Hmm, I tried this as well and it didn't work. I'm using Python3.5 if that makes a difference in this case. – flybonzai Apr 22 '16 at 19:42