1

I have a package with the following structure:

package
├── LICENSE
├── README.md
├── MANIFEST.in
├── my_pkg
│   └── __init__.py
│   └── main.py
│   └── style.qss
├── setup.py

When I install it from github using pip, the stylesheet file style.qss is cloned, but when I launch the script typing foopkg it is not loaded. This is my setup.py file:

setup.py

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="my_pkg",
    version="0.0.1",
    author="foo",
    author_email="[email protected]",
    description="A small gui",
    long_description=long_description,
    long_description_content_type="text/markdown",
    # url="https://github.com/foo/pkg",
    include_package_data=True,
    packages=setuptools.find_packages(),
    install_requires=[
        'PyQt5',
    ],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.7',
    entry_points={
        "gui_scripts": [
            "foopkg = my_pkg.main:main",
        ]
    }
)

And this is my MANIFEST.in file:

MANIFEST.in

recursive-include my_pkg *.qss

The main.py is

main.py

import sys
    ...
def main():
    app = QApplication(sys.argv)
    style_path = sys.path[0] + '/style.qss'

    file = QFile(style_path)
    file.open(QFile.ReadOnly)
    stream = QTextStream(file.readAll())
    app.setStyleSheet(stream.readAll())

    gui = GuiWindow()
    gui.show()

    app.exec_()


if __name__ == '__main__':
    main()

What am I missing? What should I modify to be able to call my script using stylesheet?

5
  • What makes you think that style.qss should be automatically included? Commented Dec 18, 2020 at 11:06
  • I naively thought that all the files in the package folder would be cloned Commented Dec 18, 2020 at 11:10
  • I would like to improve the answer because now, even though the stylesheet is cloned, the script which you can launch by typing "foopkg" doesn't find it. Any help is veeery appreciated. Commented Dec 18, 2020 at 11:11
  • 1
    1. Only files directly related to the main package script(s) are added (those explicitly declared in the imports of each script, recursively), everything else is not, and that's for good: a project directory could contain hundreds or thousands of files, even if only a few of them are actually used by the program. 2. If the answer doesn't actually solve the issue, you can delete it, then you can restore and edit it or create a new one as soon as you find the correct approach. Commented Dec 18, 2020 at 11:30
  • @Gigioz show main.py Commented Dec 18, 2020 at 14:50

1 Answer 1

1

When you build routes you must take into account what information each part contains, for example sys.path[0] contains the information from where the application is launched, for example when launching the entrypoint in Linux in my case it is /usr/bin. Instead you should use the __file__ attribute:

import os


CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

def main():
    app = QApplication(sys.argv)
    style_path = os.path.join(CURRENT_DIR, 'style.qss')

    file = QFile(style_path)
    # ...
Sign up to request clarification or add additional context in comments.

8 Comments

Please, do not recommend the unreliable use of __file__ for accessing package data. There is now importlib.metadata in the standard library (+ the importlib_metadata backport). And before that, we already had setuptools' pkg_resources.
@sinoroc You have just pointed out an option, but my question is because my solution is not correct so that I can analyze if my answer is incorrect and then correct it or at least give a warning. That there is an alternative does not imply that the other options are not valid.
@sinoroc I will understand you if another application wants to access the files of the library, but this is not the case since the library will access its own files
Relying on the value of __file__ will break in some cases. For example when executing directly from a zipped archive. Think zipapp for example. This does not matter if it is the library accessing its own files or not. This is a serious concern, not just about a choice. Relying on __file__ will break.
@sinoroc It is clearly a duplicate that I would not have found so I closed the question.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.