7 Replies - 17496 Views - Last Post: 31 July 2012 - 12:35 PM Rate Topic: -----

#1 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

importing module from another folder

Posted 27 July 2012 - 01:58 AM

Hi, at this point i' m totally blank on how to do this. Here's the problem, i have 'f.py' file inside a folder and i would like to import this file to a 'of.py' file outside the folder.
After a little research, i found the method of

import sys
sys.path.append('the /path')
import mymodule



which did work out, but the thing is that i have images inside the folder that are used by 'f.py', and though the file outside the folder('of.py') successfully imports 'f.py', it shows an error that the image cannot be opened.

Is This A Good Question/Topic? 0
  • +

Replies To: importing module from another folder

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: importing module from another folder

Posted 27 July 2012 - 02:44 AM

Which path exactly have you been using? This may be a case of having to escape backslashes in your path name. Try r'path\name\'

The os module is your friend here, and it makes it cross platform compatible too!
import os

folder = "my-folder"
image = "my-image.jpg"

path = os.path.join(folder, image)




If you are using custom modules (which you seem to be) I would probably prefer this method:

Make a blank file called __init__.py in your subdirectory (this tells Python it is a module)

So:
your-file.py
       your-folder
             __init__.py
             your-module.py



And then you can call the module by name:
from your-folder import your-module

This post has been edited by Simown: 27 July 2012 - 02:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: importing module from another folder

Posted 30 July 2012 - 02:06 PM

View PostSimown, on 27 July 2012 - 03:14 PM, said:

Which path exactly have you been using? This may be a case of having to escape backslashes in your path name. Try r'path\name\'

The os module is your friend here, and it makes it cross platform compatible too!
import os

folder = "my-folder"
image = "my-image.jpg"

path = os.path.join(folder, image)




If you are using custom modules (which you seem to be) I would probably prefer this method:

Make a blank file called __init__.py in your subdirectory (this tells Python it is a module)

So:
your-file.py
       your-folder
             __init__.py
             your-module.py



And then you can call the module by name:
from your-folder import your-module


Actually, the importing does work. But the problem is with the execution of the file imported.
I have a folder/file structure like this:

MY_FOLDER
FILE1.PY
MY_FOLDER2
FILE2.PY
<SOME IMAGES>

I am able to import FILE2.PY into FILE1.PY, but when i try to run the main function of FILE2.PY from FILE1.PY, it gives the error that certain image cannot be found.
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 module from another folder

Posted 30 July 2012 - 07:45 PM

An exact error message might be useful.
Was This Post Helpful? 0
  • +
  • -

#5 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: importing module from another folder

Posted 30 July 2012 - 11:06 PM

this is the error i'm getting

Traceback (most recent call last):
File "G:\pygamer J3F\BASE.py", line 84, in <module>
main()
File "G:\pygamer J3F\BASE.py", line 75, in main
l.main()
File "G:\pygamer J3F\testfolder\level1.py", line 320, in main
background=pygame.image.load("backclothwithclose.jpg")
error: Couldn't open backclothwithclose.jpg


backclothwithclose.jpg is one of the images i am using.
Was This Post Helpful? 0
  • +
  • -

#6 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: importing module from another folder

Posted 31 July 2012 - 05:15 AM

Now it becomes clear:

When you import a module the working directory is still the working directory of the main module:
#say this file is in c:\py_progs
import sys
sys.path.append(r'C:\py_progs\modules')
import my_module


#this is my_module in c:\py_progs\modules
import os
print(os.getcwd)


Even though my_module is in the modules directory and gets correctly imported, the working directory is still C:\py_progs.

So in your program pygame.image.load() looks into the wrong directory. If you want pygame to load from the directory your imported program is in you could do it the following way:

import os
mypath = os.path.dirmane(__file__)

#...
pygame.image.load(os.path.join(mypath, image_name))


Was This Post Helpful? 2
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: importing module from another folder

Posted 31 July 2012 - 08:28 AM

I'd like to point out how swiftly and awesomely Nallo was able to solve your problem once he was able to view your code and see the error message. The more information you give us, the more we're able to do for you.
Was This Post Helpful? 0
  • +
  • -

#8 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: importing module from another folder

Posted 31 July 2012 - 12:35 PM

View PostNallo, on 31 July 2012 - 05:45 PM, said:

Now it becomes clear:

When you import a module the working directory is still the working directory of the main module:
#say this file is in c:\py_progs
import sys
sys.path.append(r'C:\py_progs\modules')
import my_module


#this is my_module in c:\py_progs\modules
import os
print(os.getcwd)


Even though my_module is in the modules directory and gets correctly imported, the working directory is still C:\py_progs.

So in your program pygame.image.load() looks into the wrong directory. If you want pygame to load from the directory your imported program is in you could do it the following way:

import os
mypath = os.path.dirmane(__file__)

#...
pygame.image.load(os.path.join(mypath, image_name))



Thank you very much. That solved the problem. Pardon me for not providing the details of the error before.

View Postatraub, on 31 July 2012 - 08:58 PM, said:

I'd like to point out how swiftly and awesomely Nallo was able to solve your problem once he was able to view your code and see the error message. The more information you give us, the more we're able to do for you.



Yes, i am sorry that i didn't give much information at the beginning. Will be doing that from now now on. Thank you guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1