PDA

View Full Version : [PYTHON] How do I import file in different folder?



regomodo
November 11th, 2008, 09:34 PM
#

LaRoza
November 11th, 2008, 09:39 PM
I'm a bit perplexed by a certain section of a tutorial (http://www.die-offenbachs.de/eric/tutorials/LogParser/chap5.html) in where a root file imports a file "./ui/MainWindow.py" by using:


from ui.MainWindow import MainWindow

I've tried to do this as well but the interpreter spits error about not being able to do the same. Has this person making the tutorial got this wrong? How would I go about doing this?

Put a file named "__init__.py" in ui/.

Zootropo
November 11th, 2008, 09:39 PM
I'm a bit perplexed by a certain section of a tutorial (http://www.die-offenbachs.de/eric/tutorials/LogParser/chap5.html) in where a root file imports a file "./ui/MainWindow.py" by using:


from ui.MainWindow import MainWindow

I've tried to do this as well but the interpreter spits error about not being able to do the same. Has this person making the tutorial got this wrong? How would I go about doing this?
This is because ui is a folder, but it's also a package. A package in Python is just a folder with an __init__.py file in it.

regomodo
November 11th, 2008, 10:01 PM
#

nvteighen
November 11th, 2008, 10:13 PM
Ah. So that rather crucial step is omitted in that tutorial. Is the file blank?
Usually yes, unless you need some variables global to the package and that kind of things.

regomodo
November 12th, 2008, 01:04 AM
#