0

When i double click the bios.pyw it runs good but when I run this command from firm.pyw os.system("pythonw ..\\system_pbl\\bios\\bios.pyw") it gives me an error

I've tried running it with the start command but it doesn't do anything then

I've tried to run it in the same directory and with a .cmd and a .ps1 and a .bat file but no luck

firm.pyw-

import os
from tkinter import *


os.system("python ..\\system_pbl\\bios\\bios.pyw")

bios.pyw-

sil = "..\\..\\system_res files\\sil.sif"
 si = PhotoImage(file=_sil)#-error
 s_label = Label(app,image = si,borderwidth=0).place(y=0,x=0)

The error i get is

File "C:\Users\gavin\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3498, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "..\..\system_res files\sil.sif": no such file or directory

This is the file system:

file system:

  1. fireware

    2.S^

    3.Fire-system ->firm.pyw 3.system_pbl -> bios->bios.pyw, data_change.pyw, local_update.py -> boot->boot.pyw 3.system_res files -> sil.sif, usr_data.dde

tripleee
  • 175,061
  • 34
  • 275
  • 318
Potoo
  • 13
  • 5
  • 1
    If the relative path works some of the time, you run it from a different directory than when it fails. You should probably understand the concept of current directory, or at least add this information to the question. How exactly do you run these commands and where in the filesystem are these files actually? – tripleee Jul 20 '19 at 19:43
  • 1
    Why are you running one Python script from another by calling out to the system? Why don't you just import the code and call it? – Daniel Roseman Jul 20 '19 at 19:47
  • @DanielRoseman because my script has an update function and also it for organization – Potoo Jul 20 '19 at 19:50
  • @tripleee I added the file system sorry about the mis-labeling – Potoo Jul 20 '19 at 19:57
  • It's not just the file layout but which directory is current. That can change without you even knowing it. – Mark Ransom Jul 20 '19 at 20:02
  • I'm afraid your update doesn't make much sense. Could you use a more standard notation, such as that produced by the `tree` command? (And do you really have a directory called `S^`?) – tripleee Jul 21 '19 at 07:06

1 Answers1

0

Your script hardcodes the path to the data file so that you always have to run it in a subdirectory two levels below the directory with the data file.

If

python ./bios.pyw

works then

python ../firm.pyw

should work too, whereas

cd ..
python ./firm.pyw

will fail, because cd switches your current directory to one which isn't precisely two levels below the data file directory (you are now only one level below). Perhaps see also https://stackoverflow.com/a/55342466/874188

If you are running the scripts in a different manner (such as by double-clicking), you have to figure out how to control the current working directory within that mechanism. But a better fix is to not hard-code a relative path. You could

  • change the script so it accepts the path to the data file directory as a command-line argument or perhaps a configuration parameter;
  • change the script so it requires the data file subdirectory in the current directory. This is cumbersome to use, but at the very least simple and predictable behavior;
  • change the script so it figures out the absolute path to the data file directory based on something in the environment, such as, for example, the absolute path to the module you import; see How to retrieve a module's path?

More broadly, you should almost certainly not run Python as a subprocess of Python. You will probably need to reorganize your code slightly to avoid os.system() but you should find that the reorganized code is both easier to work with and much faster.

tripleee
  • 175,061
  • 34
  • 275
  • 318