1

So I have this Python project in PyCharm on MacOS and I moved the .py files into a folder.

This was my folder structure:

project/input/15_example.txt
project/aoc15.py

And I refactored it by moving aoc15.py up into a solutions folder

project/input/15_example.txt
project/solutions/aoc15.py

Now some of the .py files that have been moved are still able to reach their input files, but some are not. It does seem that .py files that don't import anything more often keep working, but also that is not consistent.

File that stopped working after refactor (aoc15.py):

from pprint import pp

lines = [*map(list,open('../input/15_example.txt').read().splitlines())]
pp(lines)

# FileNotFoundError: [Errno 2] No such file or directory: '../input/15_example.txt'

example file that does work (aoc08.py):

from pprint import pp

segs = [[seg.split(' ') for seg in line.split(" | ")] for line in open('../input/08_input.txt').read().splitlines()]

numbers = {}
for i, line in enumerate(segs):
    for j, out in enumerate(line[1]):
        if len(out) == 2: numbers[i,j] = '1'
pp(len(numbers))

I'm running my code from PyCharm, if I run it from the terminal they do work. It's running each file from it's own location, as you can see in this full error message (slightly editted for privacy)

/usr/local/bin/python3.9 ~/project/solutions/aoc15.py
Traceback (most recent call last):
  File "~/project/solutions/aoc15.py", line 4, in <module>
    lines = [*map(list,open('../input/15_example.txt').read().splitlines())]
FileNotFoundError: [Errno 2] No such file or directory: '../input/15_example.txt'

What could be the problem?

Herman
  • 750
  • 1
  • 10
  • 23
  • where are u running the script from? `python project/solutions/aoc15.py` ? – Bijay Regmi Dec 16 '21 at 09:52
  • Yes, I run it from Pycharm. Working example: /usr/local/bin/python3.9 ~/code/persoonlijk/advent-of-code-2021/solutions/aoc08.py File not found example: /usr/local/bin/python3.9 ~/code/persoonlijk/advent-of-code-2021/solutions/aoc15.py – Herman Dec 16 '21 at 10:26
  • There is no `__init__.py` file in the project, I know that this can sometimes lead to troubles with importing functions, though here it seems to work just fine – Herman Dec 16 '21 at 10:40
  • Check what is set as a working directory for every script in the run dialog. – mx0 Dec 16 '21 at 10:45
  • It's the solutions folder every time. Also in the Run/Debug configurations. – Herman Dec 16 '21 at 10:47
  • It runs from the terminal btw. So I guess it's some PyCharm issue? It all seems right though? – Herman Dec 16 '21 at 12:41
  • Damn, that's it! For most files the working directory changed to ~/project/solutions/ but for some it stayed ~/project/. Thanks man! – Herman Dec 17 '21 at 14:51

0 Answers0