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_())
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.
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.
>
> 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.
>
> 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.