0

folders

I have a python script that uses classes from a file next to my main.py named 'src'. These classes use images from the file 'img' next to main.py as well.

I'm writting the path as follows :

"img/myImage.gif"

When I run it from my python shell it works fine but I just can't run it using the command prompt (path not found).

I tried

"/img/myImage.gif" and "./img/myImage.gif"

and also moving my 'img' file to src, and to main.py's parent file but it doesn't work..

I'm out of ideas.. any assistance would be appreciated, Thanks,

vbuzze
  • 930
  • 1
  • 11
  • 25

1 Answers1

1

This is what you need to do for you folder structure,

program
   main.py
   src
      __init__.py //empty py file
      classA
           __init__.py
           classA.py

      classB
           __init__.py
           classB.py

"../../img/myimage.gif"          // to use in classA.py file

More Explanation:

if your main.py is in

    src
       main.py
    img
       myimage.gif

use this

"../img/myimage.gif"

if your folder structure is (moving img inside src)

-src
    main.py
   -img
       myimage.gif

you need to use like below

"img/myimage.gif"
Arun G
  • 1,678
  • 15
  • 17
  • thanks for this clarification, I think that I also have to add the path of my project to the PYTHONPATH ? – vbuzze May 25 '16 at 08:28
  • wel, i think so we should. main.py and classA.py are not in same level, and Python cant able to figure out if main.py importing classA.py module!! let me know if i m wrong. do accept this ans if this resolved your problem :) – Arun G May 25 '16 at 08:30
  • try doing this in main.py, import os, sys PROJECT_PATH = os.path.abspath(os.path.dirname(__file__)) sys.path.append(PROJECT_PATH) sys.path.append(os.path.join(PROJECT_PATH, 'classA')) – Arun G May 25 '16 at 08:39
  • my classes are found because they are imported in my main as follows: from src.classA.ClassA import ClassA but within these classes I can't load my images (from 'img') – vbuzze May 25 '16 at 08:42
  • I tried to add all this in main.py (changing 'file' with the path of my project and changing 'classA' to 'img') but it doesn't solve the problem – vbuzze May 25 '16 at 08:51
  • you have two options,1. in classA module refer "myimage" with absolute path. 2. under folder "src" create (__init__.py) and under classA and classB folder create "__init__.py" (this might solve ur problem) not able mention "_" underscore inthe comment ( underscore-underscore-init-underscore-underscore-.py) – Arun G May 25 '16 at 09:00
  • I do have __init__.py within every folders – vbuzze May 25 '16 at 09:03
  • create file (init empty file) reference http://stackoverflow.com/questions/448271/what-is-init-py-for – Arun G May 25 '16 at 09:04
  • It seems to work with absolute paths but I'd like to avoid it if possible – vbuzze May 25 '16 at 09:08
  • Relative paths are relative to the user's current directory, not your script's (though there are ways to achieve the latter, too). – tripleee May 25 '16 at 17:04