0

I am using python 3.3

I have a folder model with several modules, each with one class defined in it. The class has the same name as the module.

Now I want it so that when in the main script I do foo=Foo() that automatically the class module model.Foo is loaded with the effect of from model.Foo import Foo

How?

asked Mar 12, 2014 at 15:37

2 Answers 2

1

It is usually not a good idea to have automatic import. How would the python interpreter know that Foo is model.Foo ?

One way to "ease" importing classes is to load them in the module __init__.py

Ex: https://github.com/masom/shopify-trois/blob/master/shopify_trois/models

Someone else asked a similar question: Python Auto Importing

answered Mar 12, 2014 at 15:43
Sign up to request clarification or add additional context in comments.

Comments

0

While agreeing with Martin's warning about doing so, if you really want to dynamically import code, have a look at the imp module.

answered Mar 12, 2014 at 15:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.