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,

2
  • 1
    is your current working directory inside your python paths? also you'd better share your code for more clarification. Commented May 25, 2016 at 7:58
  • please share directory structure Commented May 25, 2016 at 8:05

1 Answer 1

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" 
Sign up to request clarification or add additional context in comments.

10 Comments

thanks for this clarification, I think that I also have to add the path of my project to the PYTHONPATH ?
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 :)
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'))
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')
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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.