Executing .py files is weird

Hello there... just a quick question to those that do python programming...
why is it that when i do:

cd Documents/PyProgramming/Tutorial/snake
python3 main.py

my python file gets executed just fine...
but when I do:

python3 Documents/PyProgramming/Tutorial/snake/main.py

this happens:

╭─johnlamuel@johnlamuel in ~ via  v3.9.7 
 ╰─λ python3 Documents/PyProgramming/Tutorial/snake/main.py
pygame 2.0.2 (SDL 2.0.16, Python 3.9.7)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "/home/johnlamuel/Documents/PyProgramming/Tutorial/snake/main.py", line 15, in <module>
    main()
  File "/home/johnlamuel/Documents/PyProgramming/Tutorial/snake/main.py", line 10, in main
    game.run()
  File "/home/johnlamuel/Documents/PyProgramming/Tutorial/snake/data/engine.py", line 157, in run
    floor = pygame.image.load(
FileNotFoundError: No such file or directory.

...here's some info about my machine:

╭─johnlamuel@johnlamuel in ~ via  v3.9.7 took 2s
 ╰─λ inxi -Faz
System:    Kernel: 5.14.16-zen1-1-zen x86_64 bits: 64 compiler: gcc v: 11.1.0
           parameters: BOOT_IMAGE=/@/boot/vmlinuz-linux-zen root=UUID=a61de297-0025-4727-9c18-aef77fd7414d rw
           rootflags=subvol=@ quiet splash rd.udev.log_priority=3 vt.global_cursor_default=0
           systemd.unified_cgroup_hierarchy=1 loglevel=3
           Desktop: KDE Plasma 5.23.2 tk: Qt 5.15.2 info: latte-dock wm: kwin_x11 vt: 1 dm: SDDM Distro: Garuda Linux
           base: Arch Linux
Machine:   Type: Laptop System: Acer product: Aspire A314-35 v: V1.09 serial: <filter>
           Mobo: JSL model: Rosemary_JL v: V1.09 serial: <filter> UEFI: INSYDE v: 1.09 date: 06/22/2021
Battery:   ID-1: BAT1 charge: 36.4 Wh (92.9%) condition: 39.2/41.6 Wh (94.3%) volts: 12.4 min: 11.2 model: LGC AP19B8K
           type: Li-ion serial: <filter> status: Discharging
CPU:       Info: Quad Core model: Intel Pentium Silver N6000 bits: 64 type: MCP arch: Tremont family: 6 model-id: 9C (156)
           stepping: 0 microcode: 1D cache: L2: 4 MiB
           flags: lm nx pae sse sse2 sse3 sse4_1 sse4_2 ssse3 vmx bogomips: 8908
           Speed: 2132 MHz min/max: 800/3300 MHz Core speeds (MHz): 1: 2132 2: 1065 3: 3164 4: 1230
           Vulnerabilities: Type: itlb_multihit status: Not affected
           Type: l1tf status: Not affected
           Type: mds status: Not affected
           Type: meltdown status: Not affected
           Type: spec_store_bypass mitigation: Speculative Store Bypass disabled via prctl
           Type: spectre_v1 mitigation: usercopy/swapgs barriers and __user pointer sanitization
           Type: spectre_v2 mitigation: Enhanced IBRS, IBPB: conditional, RSB filling
           Type: srbds status: Not affected
           Type: tsx_async_abort status: Not affected
Graphics:  Device-1: Intel JasperLake [UHD Graphics] vendor: Acer Incorporated ALI driver: i915 v: kernel bus-ID: 00:02.0
           chip-ID: 8086:4e71 class-ID: 0300
           Device-2: Quanta VGA WebCam type: USB driver: uvcvideo bus-ID: 1-4:3 chip-ID: 0408:a031 class-ID: 0e02
           serial: <filter>
           Display: x11 server: X.Org 1.20.13 compositor: kwin_x11 driver: loaded: intel unloaded: modesetting
           alternate: fbdev,vesa display-ID: :0 screens: 1
           Screen-1: 0 s-res: 1280x720 s-dpi: 96 s-size: 338x190mm (13.3x7.5") s-diag: 388mm (15.3")
           Monitor-1: eDP1 res: 1280x720 hz: 60 dpi: 105 size: 310x170mm (12.2x6.7") diag: 354mm (13.9")
           OpenGL: renderer: Mesa Intel UHD Graphics (JSL) v: 4.6 Mesa 21.2.4 direct render: Yes
Audio:     Device-1: Intel vendor: Acer Incorporated ALI driver: snd_hda_intel v: kernel alternate: snd_sof_pci_intel_icl
           bus-ID: 00:1f.3 chip-ID: 8086:4dc8 class-ID: 0403
           Device-2: Intel USB PnP Sound Device type: USB driver: hid-generic,snd-usb-audio,usbhid bus-ID: 1-3:2
           chip-ID: 8086:0808 class-ID: 0300
           Sound Server-1: ALSA v: k5.14.16-zen1-1-zen running: yes
           Sound Server-2: sndio v: N/A running: no
           Sound Server-3: JACK v: 1.9.19 running: no
           Sound Server-4: PulseAudio v: 15.0 running: no
           Sound Server-5: PipeWire v: 0.3.39 running: yes
Network:   Device-1: Realtek RTL8111/8168/8411 PCI Express Gigabit Ethernet vendor: Acer Incorporated ALI driver: r8169
           v: kernel port: 3000 bus-ID: 01:00.0 chip-ID: 10ec:8168 class-ID: 0200
           IF: enp1s0 state: down mac: <filter>
           Device-2: Qualcomm Atheros QCA9377 802.11ac Wireless Network Adapter vendor: Lite-On driver: ath10k_pci v: kernel
           bus-ID: 02:00.0 chip-ID: 168c:0042 class-ID: 0280
           IF: wlp2s0 state: up mac: <filter>
Bluetooth: Device-1: Lite-On Qualcomm Atheros QCA9377 Bluetooth type: USB driver: btusb v: 0.8 bus-ID: 1-8:4
           chip-ID: 04ca:3015 class-ID: e001
           Report: bt-adapter ID: hci0 rfk-id: 2 state: down bt-service: enabled,running rfk-block: hardware: no software: yes
           address: <filter>
Drives:    Local Storage: total: 1.03 TiB used: 58.65 GiB (5.6%)
           SMART Message: Unable to run smartctl. Root privileges required.
           ID-1: /dev/nvme0n1 maj-min: 259:0 vendor: SK Hynix model: HFM128GD3JX016N size: 119.24 GiB block-size:
           physical: 512 B logical: 512 B speed: 31.6 Gb/s lanes: 4 type: SSD serial: <filter> rev: 41020C20 temp: 54.9 C
           scheme: GPT
           ID-2: /dev/sda maj-min: 8:0 vendor: Toshiba model: MQ04ABF100 size: 931.51 GiB block-size: physical: 4096 B
           logical: 512 B speed: 6.0 Gb/s type: HDD rpm: 5400 serial: <filter> rev: 2J scheme: GPT
Partition: ID-1: / raw-size: 118.99 GiB size: 118.99 GiB (100.00%) used: 57.75 GiB (48.5%) fs: btrfs dev: /dev/nvme0n1p2
           maj-min: 259:2
           ID-2: /boot/efi raw-size: 260 MiB size: 256 MiB (98.46%) used: 562 KiB (0.2%) fs: vfat dev: /dev/nvme0n1p1
           maj-min: 259:1
           ID-3: /home raw-size: 118.99 GiB size: 118.99 GiB (100.00%) used: 57.75 GiB (48.5%) fs: btrfs dev: /dev/nvme0n1p2
           maj-min: 259:2
           ID-4: /var/log raw-size: 118.99 GiB size: 118.99 GiB (100.00%) used: 57.75 GiB (48.5%) fs: btrfs
           dev: /dev/nvme0n1p2 maj-min: 259:2
           ID-5: /var/tmp raw-size: 118.99 GiB size: 118.99 GiB (100.00%) used: 57.75 GiB (48.5%) fs: btrfs
           dev: /dev/nvme0n1p2 maj-min: 259:2
Swap:      Kernel: swappiness: 133 (default 60) cache-pressure: 100 (default)
           ID-1: swap-1 type: zram size: 7.6 GiB used: 37 MiB (0.5%) priority: 100 dev: /dev/zram0
Sensors:   System Temperatures: cpu: 51.0 C mobo: N/A
           Fan Speeds (RPM): N/A
Info:      Processes: 246 Uptime: 2h 45m wakeups: 1 Memory: 7.6 GiB used: 3.54 GiB (46.6%) Init: systemd v: 249
           tool: systemctl Compilers: gcc: 11.1.0 clang: 12.0.1 Packages: 1968 pacman: 1961 lib: 504 flatpak: 7 Shell: fish
           v: 3.3.1 default: Bash v: 5.1.8 running-in: yakuake inxi: 3.3.08

Hi there, I have a bit of Python programming experience too.
Can you share the code so that I can have a look? Gitlab would be most preferable in case of multiple files.

3 Likes

Python can’t find the file listed here.

You’re setting the working directory to the “snake” directory, so Python will find (by default) files in that directory.

Your working directory is $HOME, and so that’s where Python is looking for the files.

There are various ways of dealing with this (in a wrapper shell script or within the Python script itself), but you’d generally identify the directory the Python script is in then set that as the working directory.

5 Likes

ohhhhh I see... i understand now... thank you

1 Like

Unfortunately I don't have a Gitlab acc so instead i made a github repository just for you my dude...

here's the link:

the code is a little messy cause im not yet done with it and im still just learning

No issue with github either.
In line

See

4 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.