Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - importing package classes into console global namespace

I'm having a spot of trouble getting my python classes to work within the python console. I want to automatically import all of my classes into the global namespace so I can use them without any prefix.module.names.

Here's what I've got so far...

projectname/
|-__init__.py
|
|-main_stuff/
  |-__init__.py
  |-main1.py
  |-main2.py
  |
  |-other_stuff/
    |-__init__.py
    |-other1.py
    |-other2.py

Each file defines a class of the same name, e.g. main1.py will define a class called Main1.

My PYTHONPATH is the absolute path to projectname/.

I've got a python startup file that contains this:

from projectname import *

But this doesn't let me use my classes at all. Upon starting a python console I would like to be able to write:

ob=Main1()

but Main1 isn't within the current namespace, so it doesn't work.

I tried adding things to the __init__.py files...

In projectname/__init__.py:

import main_stuff

In projectname/main_stuff/__init__.py:

import other_stuff
__all__ = ["main1", "main2", "main3"]

And so on. And in my startup file I added:

from projectname.main_stuff import *
from projectname.main_stuff/other_stuff import *

But to use the classes within the python console I still have to write:

ob=main1.Main1()

I'd prefer not to need the main1. part. Does anyone know how I can automatically put my classes in the global namespace when using the python console?

Thanks.

==== EDIT ====

What I need is to import a package at the class level, but from package import * gives me everything at the module level. I'm after an easy way of doing something like this:

for module in package do:
    from package.module import *

==== ANOTHER EDIT ====

I ended up adding the class imports to my python startup file individually. It's not ideal because of the overhead of maintaining it, but it does what I want.

from class1.py import Class1
from class2.py import Class2
from class3.py import Class3
like image 653
Fish Avatar asked Sep 30 '09 16:09

Fish


People also ask

How do you import a namespace in Python?

Importing is a way of pulling a name from somewhere else into the desired namespace. To refer to a variable, function, or class in Python one of the following must be true: The name is in the Python built-in namespace. The name is the current module's global namespace.

Can classes be imported in Python?

Importing classes from other programs allows us to use them within the current program. Thus, helping in improved readability and reusability of code. Importing can be done within the same or from different folders. If you want to learn more about python Programming, visit Python Programming Tutorials.

What is __ import __ in Python?

__import__() Parameters name - the name of the module you want to import. globals and locals - determines how to interpret name. fromlist - objects or submodules that should be imported by name. level - specifies whether to use absolute or relative imports.

What is global namespace in Python?

The global namespace contains any names defined at the level of the main program. Python creates the global namespace when the main program body starts, and it remains in existence until the interpreter terminates.


1 Answers

You want to use a different form of import.

In projectname/main_stuff/__init__.py:

from other_stuff import *
__all__ = ["main1", "main2", "main3"]

When you use a statement like this:

import foo

You are defining the name foo in the current module. Then you can use foo.something to get at the stuff in foo.

When you use this:

from foo import *

You are taking all of the names defined in foo and defining them in this module (like pouring a bucket of names from foo into your module).

like image 56
Ned Batchelder Avatar answered Nov 15 '22 17:11

Ned Batchelder