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 bad 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
importa module, you effectively declare a local variable which is a reference to that module. So infeedyyou have an object calledfeedparser, which happens to be the modulefeedparser, although you could at any time reassign it to be any other Python object.When you
import feedy, you can refer to any offeedy‘s exported variables asfeedy.name. So in this case,feedy.feedparseris the modulefeedparser.However, if you change the way
feedyis 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.