Import a module into a module, can't explain what i'm trying to do
So, the following is an example. Here's a module (called feedy.py) in let's say, the core directory:
import feedparser
feed = feedparser.parse("http://site.com/feed")
[...]
A ba开发者_如何学JAVAd example, but anyway: my problem is in the main script (parent dir), I have to do
import feedparser
as well as
from core import feedy
However, is there a way to eliminate the need to import feedparser, since it's already imported in feedy.py?
Hope you understand, Fike.
In principle, yes. However, it's not usually a good idea.
When you import
a module, you effectively declare a local variable which is a reference to that module. So in feedy
you have an object called feedparser
, which happens to be the module feedparser
, although you could at any time reassign it to be any other Python object.
When you import feedy
, you can refer to any of feedy
's exported variables as feedy.name
. So in this case, feedy.feedparser
is the module feedparser
.
However, if you change the way feedy
is implemented, so that it doesn't import (or export) feedparser
, this will break your main script. In general you don't want to export everything you have defined, although it's fine for a quick hack.
精彩评论