Pycharm does not see files in relative path with ../
Asked Answered
K

2

14

I am developing a python project using pycharm. However, the problem is that it refuses to load files in which the relative path includes ../. When I try

self.image = pygame.image.load("../resources/img/prey.png").convert_alpha()

I get:

self.image = pygame.image.load("../resources/img/prey.png").convert_alpha()
pygame.error: Couldn't open ../resources/img/prey.png

The thing is that when running my code from the terminal, it works fine, meaning that the problem is with pycharm.

Now, the problem is that while this originally sounds like many of the other questions in which the directory is not part of the working directory,adding:

import os
print os.getcwd()

from pycharm gives me /media/Storage/System and dev/Code/Cross Platform/python/boids/current

which seems correct with my directory structure being:

./current
    ./resources
        ./img
            prey.png
    ./objects
        modules_that_I_import.py
    ./experiments
        code_that_I_actually_run.py

The code that runs self.image=pygame.image.load("../resources/img/prey.png").convert_alpha() is in ./objects

I am using Pycharm Community Edition 2016.1.2 on Linux Mint with Python 2.7.

Any help is appreciated.

EDIT

when running os.getcwd from the command line I get /media/Storage/System and dev/Code/Cross Platform/python/boids/current/experiments

Klotz answered 5/6, 2016 at 0:19 Comment(3)
What is printed when you run code with os.getcwd() in PyCharm?Fransen
the output I gave was when running it in pycharmKlotz
When I run os.getcwd from the commandline I get /media/Storage/System and dev/Code/Cross Platform/python/boids/current/experimentsKlotz
F
22

Go to Run - Edit Configurations, select your configuration

Run Configuration editing

and change Working directory to /media/Storage/System and dev/Code/Cross Platform/python/boids/current/experiments.

Working directory editing

Fransen answered 6/6, 2016 at 10:58 Comment(4)
Your "change .. to" lists two different directories : how can there be two for the single Working Directory ?Graber
@javadba It is one path with names containing spacesFransen
Are you saying that /media/Storage/System and dev/Code/Cross Platform/python/boids/current/experiments is one directory ? I am confusedGraber
@javadba Yes, I took it from the questionFransen
F
0

I had the same problem when I moved the python file to another directory. In my case, it was enough to delete the run configuration (Run -> Edit Configurations -> Remove Configuration).

Featherbedding answered 26/2, 2020 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.