Python can't fins file but it's there

I use Eclipse IDE with PyDev. My program reads an xml file which is located in the same folder as the Python script. I tried it on Windows with Python and it works fine. On Linux, when I run my script from Eclipse, it works fine. When I run it from console - it works fine. But when I double click on the executable script, it says it can’t find the file. How so? It’s so strange! Did this happen to you?

Here’s the executable:


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PyQt4.QtGui import QApplication
from MainWindow import MainWindow

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())


MainWindow:


from xml.etree.ElementTree import ElementTree
...
self.xmlFile = "xmlfile.xml"
self.tree = ElementTree()
self.xmlDoc = self.tree.parse(self.xmlFile)
...

Hi,

the environment is different when you are starting the script via double click. You should try to specify the filename not relative and using instead the full path.

Hope this helps

Hi,

When the script file is double-clicked, current working directory will be moved. In my environment(LinuxMint), current working directory was moved to my home directory.

Regards.

jkani4 wrote:

>
> Hi,
>
> When the script file is double-clicked, current working directory will
> be moved. In my environment(LinuxMint), current working directory was
> moved to my home directory.
>
> Regards.
>
>
To get the absolute path where the script is use

import os
print os.sys.path[0]

and use this to create the path for the file.
So you can avoid to use hardcoded paths for the file and the result remains
relocatable.

of course without the “print” use the result.

Hi,martin_helm

Thank you for your reply.

Is os.sys the same as sys module?
If so, are the following scripts same meaning?

[script a]
import os
print os.sys.path[0]

[script b]
import sys
print sys.path[0]

Regards.

jkani4 wrote:

>
> Hi,martin_helm
>
> Thank you for your reply.
>
> Is os.sys the same as sys module?
> If so, are the following scripts same meaning?
>
> [script a]
> import os
> print os.sys.path[0]
>
> [script b]
> import sys
> print sys.path[0]
>
> Regards.
>
>
Use whatever you prefer the result will be the same and work as expected,
because it is the same.

Hi, martin_helm

Thank you !:wink:

martin_helm,

When I do ‘print os.sys.path’ - it prints a lot of paths, when I do ‘os.sys.path[0]’ - it just prints itself, that is ‘------> print (os.sys.path[0])’