2

I see this question has been asked before, but I still trying to get my head around working with python modules. My app has a very basic structure:

app/ __init__.py driver.py dbloader/ __init__.py loader.py 

both __init__.py files are empty. driver.py only has one class Driver() and loader.py has only class in it Loader()

So, to test this setup, cd to inside the app/ directory. From here I start a python shell. I then try:

import dbloader which works (i.e. no errors). However, I've tried every permutation to get to instantiate Loader() inside loader.py to no avail. A few ones of the ones I've tried are:

from dbloader import loader from dbloader.loader import Loader 

I've also tried

importing just dbloader and then trying to instantiate as follow:

import dbloader l = dbloader.Loader() 

All to no avail. I believe reading elsewhere that the current directory and subdirectories are automatically included in the pythonpath when executing the python shell (is this true?)

Anyhow, any assistance would be much appreciated.

3
  • when you do import dbloader l = dbloader.Loader(), what error do you get? Commented May 2, 2012 at 15:57
  • I don't know why you're seeing issues with your two solutions: from dbloader import loader and from dbloader.loader import Loader. The first should provide Loader as loader.Loader, and the second as Loader. Could you post the stack trace? Commented May 2, 2012 at 16:25
  • Niek and Darth, the error I was getting was that 'could not import name loader' but it has since been fixed by following Daniel's suggestions below which look similar to what I was already doing, so it might've well been a bad case of fat fingers. Commented May 2, 2012 at 17:29

3 Answers 3

3

dbloader by itself doesn't have any reference to the Loader class. However you do it, you need to go through the loader namespace. So, two possible ways:

from dbloader import loader l = loader.Loader() 

or

from dbloader.loader import Loader l = Loader() 

It helps to think about namespaces, rather than modules or classes. Loader is in the dbloader.loader namespace, and to have access to that class you either need to import the class itself, or the module that contains it, into your current namespace.

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

1 Comment

Thanks much. This helped clarify things for me.
2

import X adds X to your namespace.

import dbloader - This adds the module dbloader. You'd get to your class with dbloader.loader.Loader

from dbloader import loader - This adds the module loader to your namespace. You'd access your class with loader.Loader.

from dbloader.loader import Loader - This imports the class Loader to your namespace. You'd just use Loader() here.

Try playing around in the python shell with dir and help, you should be able to understand the structure a little better then.

1 Comment

Actually, if dbloader's __init__.py is empty, doing this: import dbloader; dbloader.loader will fail, since import dbloader is simply executing the __init__.py file and linking the name dbloader to it's scope, which loader does not exist in. You need to run from dbloader import loader or import dbloader.loader to gain access to loader.
0

In addition to using the solution proposed by Daenyth and Daniel Roseman, you can access Loader directly from dbloader, if you from-import Loader in dbloader:

# app/dbloader/__init__.py from dbloader.loader import Loader 

Then, in your shell:

import dbloader l = dbloader.Loader() # or... from dbloader import Loader l2 = Loader() 

This is a nice solution for cleaning up namespaces.

4 Comments

Needing this is usually a hint that your module structure is wrong.
@Daenyth I wouldn't say 'usually'. If you have a very large self-contained class, it makes sense to put it in its own file. For example, a mathematical vector class Vector in a util module would be annoying to import everywhere as from util.vector import Vector.
That's probably a valid case for it, but I'm referring more to things where you get holdovers from java like one class per file, when the class is 10 lines (and didn't need to even be a class in the first place...)
Ah, I see what you mean. One of the first things all Java programmers learning python should read is the python is not java article.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.