Reputation: 29
Beginner question. I am trying to import a python script "feedparser.py" into another python script "ps5.py". Both scripts reside in the same folder "MIT_OCW" on my desktop. When I try to import "feedparser.py" into "ps5.py" I get an import error ("ImportError: No module named feedparser"). What steps should I take to diagnose the error (I am new to programming)? Thanks.
Upvotes: 2
Views: 1133
Reputation: 354
No need to add the file extension.
import feedparser
Or if you are going to refer the functions/classes in the file very frequently and you arenkt going to redefine the functions/classes in the file:
from feedparser import *
Using the second method will allow you to acces the objects defined in the feedparser file without having to add ‘feedparser.’ before the object name. If that doesn’t work try this(replace text in the curly brackets with the appropriate words):
from {name of the directory in which the feedparser file is located}.feedparser import *
Thank You! Hope that works!
Upvotes: 0
Reputation: 36
I agree with @munk, the file extension is not needed when you import a module.
import feedparser
or
import feedparser as f
from feedparser import * #not recommended at all
from feedparser import func1, func2 #where func1 and func2 are functions in your module
Anything will work for Python3 at least.
Upvotes: 0
Reputation: 41
Actually, I agree with munk and Rushy Panchalon this. You should do the following:
import feedparser as feed
(or any other name)
You wouldn't need to import string or time unless needed for your code.
Upvotes: 1
Reputation: 31
This is code from ps5.py
import feedparser
import string
import time
These are the paths of feedparser.py and ps5.py
~/Desktop/MIT_OCW/problem set 5/ps5.py
~/Desktop/MIT_OCW/problem set 5/feedparser.py
Originally when I ran the code I got back the "ImportError: No module named feedparser". However, I just tried running it again (without having changed anything) and it worked. I am happy it works but frustrated that I don't know why it didn't work in the first place. Anyway, thanks for your help.
Upvotes: 3
Reputation: 12983
The name of the module is the filename without the extension. So to import feedparser.py, you would use:
import feedparser
To use something from feedparser, say a function f, you would call it from your module like:
feedparser.f()
Upvotes: 1