3

I have created a folder named "custom_module" and I have the __init__.py inside the folder which contains:

__all__ = [
        'Submodule1',
        'Submodule2'
]

From what documentation I read I should be able to call import custom_module and get access to the package, however this isn't happening. How can I make python recognize my package? I am using python 3.2

Update: The package is not located in the python folder. How does the python environment find it, so I can successfully import it by name.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100

2 Answers2

11

There are two distinct concepts you are confusing: packages and modules.

A module is what you think it is: a Python script containing classes, variables, whatever. You import it by its filename, and can then access the variables in its namespace.

A package is a collection of modules which are grouped together inside a folder. If the folder contains a file called __init__.py, Python will allow you to import the entire folder as if it were a module. This will run the code in __init__, but will not necessarily import all of the modules in the folder. (This is a deliberate design choice: packages are often very large, and importing all of the modules could take a very long time.)

The only things which are exported (as package.thing) by default are the variables defined inside __init__. If you want submodule to be available as package.submodule, you need to import it inside __init__.

__all__ is a related concept. In brief, it defines what is imported when you do from package import *, because it's not easy for Python to work out what that should be otherwise. You don't in general need it.

Katriel
  • 120,462
  • 19
  • 136
  • 170
  • 2
    I understand all this (sorry it's a package not a module). However how do I make python find my package, because when I say import custom_module nothing happens. My package is not located in the python folder. – Konstantin Dinev Sep 18 '12 at 11:23
8

sys.path holds the Python search path. Before trying to import your modules and packages, set it to include your path:

import sys
sys.path.insert(0, 'your_path_here')
import custom_module

More detail in the Python docs and in this question

Community
  • 1
  • 1
cfi
  • 10,915
  • 8
  • 57
  • 103
  • in this case if I make an installer package for my package I should be extracting it within the python directory or can I make the installer modify the sys.path with user selected directory? – Konstantin Dinev Sep 18 '12 at 11:50
  • sys.path modifications are only valid for your currently runnig script. If you want to permanently install your package into a given Python install, I recommend reading the [Official docs about the standard installation procedure](http://docs.python.org/py3k/install/index.html#standard-build-and-install). Generally installers call a `setup.py` inside your package and put your code into standard locations. You probably want to follow the standards so your module could be released on http://pypi.python.org. [Pypi docs](http://wiki.python.org/moin/CheeseShopTutorial) – cfi Sep 18 '12 at 11:59
  • 2
    In general, instead of telling Python to look somewhere non-standard, you should install your package to somewhere standard. – Katriel Sep 19 '12 at 15:28