2

I have a directory with two .py files in my C:\Python27\word_data called main.py and gethtml.py.

I want to import gethtml.py in my main.py, print def from that file, and I tried to do like this:

import gethtml

print gethtml.getHtmlText()

When I run this in a Python shell I get an error:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    execfile("word_rank/main.py")
 File "word_rank/main.py", line 3, in <module>
    import gethtml
ImportError: No module named gethtml

What am I missing?

1
  • If you run this from the word_rank directory, it will work. Commented Aug 1, 2013 at 11:18

1 Answer 1

4

You could check that the working directory for your python session is the directory containing your two python files. You can get Python to report the location of the current working directory as follows:

import os
print(os.getcwd())

Python will look in the current working directory (and in some directories in PATH) for the file you are trying to import. Not being able to find the file would give the error above.

Sign up to request clarification or add additional context in comments.

3 Comments

I've added you suggestion, but I'm not quite sure how to use it... It gives me the same error. should I add something in brackets of getcwd or? my file looks like this now: import os os.getcwd() import gethtml print gethtml.getHtmlText()
yea I've managed to get it work with your suggestion and adding from word_rank before import gethtml, thanks
Glad to hear it's working. Note that adding import os and os.getcwd() does not change the working directory, only prints it out for you.

Your Answer

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.