2

I have a Python package which is organized like this:

package
|- subpackage
|  |- code.py
|  |- window.ui
| ...

In code.py I want to access the file window.ui via

PyQt4.uic.loadUi('window.ui', self)

This works well, if I just run code.py with subpackage as the working directory. But if I import the package from another working directory, this file cannot be found:

IOError: [Errno 2] No such file or directory: 'window.ui'

My question: How can I get the path name of the directory the file code.py is placed in, in order to create the absolute path name of window.ui. Or, how can I most efficiently access the file window.ui.

I tried os.path.abspath('.') from here, but it only returns the absolute path of the current working directory.

Community
  • 1
  • 1
erik
  • 619
  • 1
  • 11
  • 20

1 Answers1

5

Use absolute path of the file instead of relative path.

abspath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "window.ui")

PyQt4.uic.loadUi(abspath, self)
Mayank Jaiswal
  • 12,338
  • 7
  • 39
  • 41