Note that there are some explanatory texts on larger screens.

plurals
  1. POPython - importing package classes into console global namespace
    primarykey
    data
    text
    <p>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.</p> <p>Here's what I've got so far...</p> <pre><code>projectname/ |-__init__.py | |-main_stuff/ |-__init__.py |-main1.py |-main2.py | |-other_stuff/ |-__init__.py |-other1.py |-other2.py </code></pre> <p>Each file defines a class of the same name, e.g. main1.py will define a class called Main1.</p> <p>My <code>PYTHONPATH</code> is the absolute path to projectname/.</p> <p>I've got a python startup file that contains this:</p> <pre><code>from projectname import * </code></pre> <p>But this doesn't let me use my classes at all. Upon starting a python console I would like to be able to write:</p> <pre><code>ob=Main1() </code></pre> <p>but Main1 isn't within the current namespace, so it doesn't work.</p> <p>I tried adding things to the <code>__init__.py</code> files...</p> <p>In <code>projectname/__init__.py</code>:</p> <pre><code>import main_stuff </code></pre> <p>In <code>projectname/main_stuff/__init__.py</code>:</p> <pre><code>import other_stuff __all__ = ["main1", "main2", "main3"] </code></pre> <p>And so on. And in my startup file I added:</p> <pre><code>from projectname.main_stuff import * from projectname.main_stuff/other_stuff import * </code></pre> <p>But to use the classes within the python console I still have to write:</p> <pre><code>ob=main1.Main1() </code></pre> <p>I'd prefer not to need the <code>main1.</code> part. Does anyone know how I can automatically put my classes in the global namespace when using the python console?</p> <p>Thanks. </p> <p>==== EDIT ====</p> <p>What I need is to import a package at the class level, but <code>from package import *</code> gives me everything at the module level. I'm after an easy way of doing something like this:</p> <pre><code>for module in package do: from package.module import * </code></pre> <p>==== ANOTHER EDIT ====</p> <p>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.</p> <pre><code>from class1.py import Class1 from class2.py import Class2 from class3.py import Class3 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload