3 Replies - 929 Views - Last Post: 16 July 2012 - 06:28 AM Rate Topic: -----

#1 neojiphre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 27-October 11

Importing modules from different folders

Posted 15 July 2012 - 07:44 PM

Hello, I have a script file, and inside this script file I would like to import a couple of modules. The two modules are located in different folders from my script file. Say my directory is like this:

script.py
folder1
---1.py
folder2
---2.py

I would like to import the scripts 1.py and 2.py for use in script.py. I tried
import folder1/1
import folder2/2


But a syntax error is produced. Any help to fix this problem would be greatly appreciated. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Importing modules from different folders

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: Importing modules from different folders

Posted 15 July 2012 - 07:56 PM

You need to rename the files to something line one.py and two.py because 1 and 2 aren't valid module names. Then you need to create __init__.py files in folder1 and folder2. This turns the folders into packages. Now you can import the modules with import folder1.one and import folder2.two.

For more information see the Python documentation on modules.

This post has been edited by sepp2k: 15 July 2012 - 07:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 neojiphre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 27-October 11

Re: Importing modules from different folders

Posted 16 July 2012 - 02:12 AM

First of all, thanks for your reply. Ok, so I tried your solution. I have renamed the modules to correct names. Now, inside script.py I am trying to run a couple of functions inside one.py and two.py as:

import folder1.one
import folder2.two

a = func1('someparam') #func1 is a function inside one.py
b = func2('someotherparam') #func2 is a function inside two.py


the ff error appears (it stops at line 4 in the code above)

NameError: name 'func1' is not defined


The same error appears even if I try:

import folder1.one
import folder2.two

a = one.func1('someparam') #func1 is a function inside one.py
b = two.func2('someotherparam') #func2 is a function inside two.py

Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Importing modules from different folders

Posted 16 July 2012 - 06:28 AM

So close! But the syntax would be:
import folder1.one
import folder2.two

a = folder1.one.func1('someparam') #func1 is a function inside one.py
b = folder2.two.func2('someotherparam') #func2 is a function inside two.py



however, that's pretty ugly, amIright? So we can clean that up a little like so:
import folder1.one as one
import folder2.two as two

a = one.func1('someparam') #func1 is a function inside one.py
b = two.func2('someotherparam') #func2 is a function inside two.py



That's better. However, I'm guessing that you wouldn't mind just saying func1('abc') rather than one.func1('abc'). This is possible too. Here's that syntax:
from folder1.one import func1
from folder2.two import func2

a = func1('someparam') #func1 is a function inside one.py
b = func2('someotherparam') #func2 is a function inside two.py



Technically, you can do from folder1.one import *. This will make it so that you can call any function from folder1.one directly, in the same way you could with the above snippet. However, you shouldn't do this; it's considered poor form for several reasons. First of all, there's no way to know what module a given function came from, thus making your programs harder to debug. Secondly, you might accidentally make a function with the same name as a function from one of those modules, which could be problematic later on.

If you start using the from module import function someone is bound to tell you about from module import * but not you know enough to explain to them why they're giving bad advice. I prefer the second style that I showed you. With that syntax, you can tell which module a given function came from at a glance. If you need to debug that module, you check the top of the script to find out the file path and you're on your way!

This post has been edited by atraub: 16 July 2012 - 09:42 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1