Importing Packages in Python

Importing packages in Python

You need to import the submodule:

import FooPackage.foo

What you're doing is looking for foo in FooPackage/__init__.py. You could solve it by putting import FooPackage.foo as foo (or from . import foo) in FooPackage/__init__.py, then Python will be able to find foo there. But I recommend using my first suggestion.

python import module from a package

Short answer

So what does the from packages import * really mean inside that __init__.py?

The __init__.py imports itself.

Explanation

You can only import modules, not packages. Packages are just containers for modules or sub-packages. When you "import" a package you actually import the module __init__.py.

The __init__.py with this content:

from packages import mod

imports the module mod into __init__.py. Therefore, it will be available
in your main.py via packages.mod (remember packages is represented by __init__.py).

When you change the content of __init__.py to:

from packages import *

You are importing the module __init__.py, the very same file you are in.
This works (a second import just triggers a lookup in sys.modules)
but won't give you the content of mod.

This means, you can use:

from module import *

but you cannot sensibly use this with an empty __init__.py:

from package import *

Because package is actually represented by the __init__.py and there
is nothing in it yet. You can check this (interactively or in file):

>>> import packages
>>> print(packages)
<module 'packages' from '/.../packages/__init__.py'>

In __init__.py you can write:

from packages.mod import *

and then in main.py:

print packages.hello()

works. Because the function hello() is now in the global name space of the
file __init__.py.

As mentioned in the answer by mozman, you can use __all__ in __init__.py to
list the modules that should be imported if from packages import * is used. This is designed for this case.

The __init__.py has only this content:

__all__ = ['mod']

Now you can do this in main.py:

from packages import *

print mod.hello()

If you extend your __init__.py:

__all__ = ['mod']

from packages import *

You can do this in main.py:

import packages

print packages.mod.hello()

But if you remove the from packages import * from __init__.py:

__all__ = ['mod'] 

You will get an error:

AttributeError: 'module' object has no attribute 'mod'

because the __all__ is only used for the from packages import * case.
Now we are back to the __init__.py imports itself.

Can't import my own modules in Python

In your particular case it looks like you're trying to import SomeObject from the myapp.py and TestCase.py scripts. From myapp.py, do

import SomeObject

since it is in the same folder. For TestCase.py, do

from ..myapp import SomeObject

However, this will work only if you are importing TestCase from the package. If you want to directly run python TestCase.py, you would have to mess with your path. This can be done within Python:

import sys
sys.path.append("..")
from myapp import SomeObject

though that is generally not recommended.

In general, if you want other people to use your Python package, you should use distutils to create a setup script. That way, anyone can install your package easily using a command like python setup.py install and it will be available everywhere on their machine. If you're serious about the package, you could even add it to the Python Package Index, PyPI.

importing a package from within another package in python

Thanks mguijarr.

I found a solution here on stackoverflow:
source:
How to fix "Attempted relative import in non-package" even with __init__.py

when I am in the project folder /project, I can call the module like this:

python -m packages.files.fileHash (no .py here, because it is a package)

This is wokring well.
In this case PYTHONPATH is known and the import can look like this:

from packages.files import fileChecker

If it is not called directly, but from within the package directory in my case /packages/hasher --> setting the PYTHONPATH is needed:

if __package__ is None:
import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
from packages.files import fileChecker
else:
from packages.files import fileChecker

The important thing for me here is, that the path to include is the PROJECT path.

The code snippet above(the last one) already includes the case describes both cases (called as package and directly).

Thanks alot for your help.

Update:

  1. Just to make my answer more complete

Python adds the current path to the PYTHONPATH automatically when doing

python fileHash.py

Another option, in addition to the one above, is to set the PYTHONPATH when running the program like this

PYTHONPATH=/path/to/project python fileHash.py

  1. I gained some experience, I would like to share:

    • I don't run modules from within their directories anymore.
    • Starting the app, running tests or sphinx or pylint or whatever is all done from the project directory.
    • This ensures that the project directory is contained in the python path and all packages, modules are found without doing additional stuff on imports.
    • The only place I still set the python path to the project folder using sys.path is in my setup.py in order to make codeship work.

Still, in my opinion this is somehow not an easy matter and I find myself reflecting the PYTHONPATH often enough :)

What's the difference between a Python module and a Python package?

A module is a single file (or files) that are imported under one import and used.
e.g.

import my_module

A package is a collection of modules in directories that give a package hierarchy.

from my_package.timing.danger.internets import function_of_love

Documentation for modules

Introduction to packages

Importing modules from parent folder

It seems that the problem is not related to the module being in a parent directory or anything like that.

You need to add the directory that contains ptdraft to PYTHONPATH

You said that import nib worked with you, that probably means that you added ptdraft itself (not its parent) to PYTHONPATH.



Related Topics



Leave a reply



Submit