2

I always struggled with Python package import. I searched the web, but wasn't able to find an appropriate answer.

I have the following directory structure:

.
./__init__.py
./packages/
./packages/__init__.py
./packages/package
./packages/package/__init__.py
./packages/package/module.py

The module.py source contains only one line:

import package

If I go to "packages" directory, I am able to import package:

>>> import python
>>>

If I go to "." directory, I would like to import the module (or the package) as follow:

>>> import packages.package.module as module

but i'm getting the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "vendor/package/module.py", line 1, in <module>
    import package
ImportError: No module named package

What do I need to put in the __init__.py files, so I can do the above import? More, what do I need to put in ./__init__.py file, so I will able to import my project from ../ directory?

If possible, I would like to solve this problem without changing the sys.path variable.

5
  • What file you run first? Who starts the whole process? Commented Nov 14, 2012 at 9:46
  • Read the documentation: docs.python.org/2/tutorial/modules.html Commented Nov 14, 2012 at 9:51
  • from packages.package import module Commented Nov 14, 2012 at 9:53
  • @alexvassel, I start the process from "." directory, which is the root folder for my project. Basically, I run "python project.py" from ".". Commented Nov 14, 2012 at 10:21
  • @ragsagar, that apparently doesn't work. Commented Nov 14, 2012 at 10:23

2 Answers 2

2

In order for the import package in module.py to succeed, the python package package must be discoverable. This means it must be on the PYTHONPATH. By default this path includes site-packages (the directory in which python packages are placed with easy_install or pip). Further, PYTHONPATH also includes the current working directory.

If you are in ., then . is placed at the start of your PYTHONPATH. This directory does not include a python package called package, thus import package fails.

You can either move to ./packages before starting a python interpreter or you can install your package. To do the latter, you'll need a setup.py

Sign up to request clarification or add additional context in comments.

4 Comments

So I can't solve this issue by somehow editing the _init_.py files?
You could manipulate sys.path in the packages/__init__.py to add the /packages dir to it
The sys.path.insert is the solution i'm using right now, but i'm not satisfied with it.
What are you actually trying to achieve? Can you give an example of why you want to import a parent package from a module within it? Are you familiar with installing a python package with setuptools?
1

When running the program from . all imports have to be relative to that directory. For your module.py file that means that you have to do

import packages.package

instead of

import package

This is because python will use . as the base for all imports that you do in your program. Without a lot of magic it is not possible to import something from a parent directory, so you will have to do an absolute import as shown above.

2 Comments

It worked, but is there a possibility to do that without modifying the package modules?
I don't think that is easily possible. But it should not anyway, because you are referring to a parent in a submodule, which could be considered bad design as modules should be as self containing as possible. Nevertheless it happens of course, but only in a self contained project, in which case you will start it from the same file all the time anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.