1

Currently, I have a package name (let's say DummyPackage). DummyPackage contains three modules with functions, classes, etc. So the directory structure looks like this:

project_dir/
    __init__.py
    DummyPackage/
        __init__.py
        Module1/
            __init__.py
            module_x.py
            module_y.py
        Module2/
            __init__.py
            module_z.py

So importing methods from modules looks like this

from DummyPackage.Module1.module_x import method_x

We are adding new stuff to the project and I would like to create a module, with the name DummyProject, which should be importable like this

from DummyProject import new_method

I assumed, only adding file DummyPackage.py would be enough, but apparently, it's not. I tried to add it to the project_dir/ dir and to DummyPackage/ dir, but neither works.

Is it because of name conflict? Is it possible to have a code like this?

import DummyPackage
from DummyPackage.Module1.module_x import method_x

DummyPackage.new_method
method_x
TylerH
  • 20,799
  • 66
  • 75
  • 101
jprochaz
  • 13
  • 2
  • if you added `DummyProject.py` to the `DummyPackage` folder you would import it with `import DummyPackage.DummyProject` would you not? – Tadhg McDonald-Jensen Mar 02 '16 at 15:18
  • or if you were importing it from within `module_x.py` you would use `from ..DummyProject import new_method` see [the documentation](https://docs.python.org/2/tutorial/modules.html#intra-package-references) for details. – Tadhg McDonald-Jensen Mar 02 '16 at 15:31
  • or if you want `new_method` to be a package level variable you just need to [add some code to your `__init__.py`](http://stackoverflow.com/a/18979314/5827215) – Tadhg McDonald-Jensen Mar 02 '16 at 15:36

1 Answers1

0

to put my three comments in an answer:

First let me explain relative imports using the modules you already have, if you wanted to import module_x from module_y you can do this:

module_y.py

from .module_x import method_x

or similarly in module_z.py

from ..Module1.module_x import method_x

so depending on the location of your DummyProject in the package Intra-package References may be all you need.

As for the second part yes it is possible to have (runnable) code like this:

import DummyPackage
from DummyPackage.Module1.module_x import method_x

DummyPackage.new_method
method_x

in this case it looks like you want new_method to be a package level variable. To quote this great answer:

In addition to labeling a directory as a Python package and defining __all__, __init__.py allows you to define any variable at the package level.

I highly recommend taking a look at the source code for json/__init__.py in the standard library if you want an real world example.

Or as an example with your setup to be able to import method_x right from the package you would just need to add this to the top level __init__.py:

from .Module1.module_x import method_x

then from any file importing the package you could do this:

import DummyPackage

DummyPackage.method_x

(Although obviously you would do it for new_method according to where you place DummyProject)

Community
  • 1
  • 1
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59