Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYTHONPATH module location #188

Open
jamesstidard opened this issue Aug 11, 2023 · 1 comment
Open

PYTHONPATH module location #188

jamesstidard opened this issue Aug 11, 2023 · 1 comment

Comments

@jamesstidard
Copy link

jamesstidard commented Aug 11, 2023

Hi,

I'm a little unsure the best place to put this, as I'm using py2exe and nsis to freeze and install a project. I'm not to sure which side of the chain I should/can resolve this from.

I'm able to py2exe.freeze my project, and launch it via the created *.exe on my Windows machine. I'm also able to create an installer which install the application to Program Files, and creates a desktop shortcut, all from my development machine.

However, when I go to move that workflow over to a GitHub Action, when I install the application from there I get a ModuleNotFoundError for my applications namespace/module/package (unsure the technically correct term there).

For example, I have a project with the structure like:

scripts/
    build.py
myapp/
    __init__.py
    utils.py
    gui.py
Pipfile
Pipfile.lock
README.md

And the errors are for the import of my package e.g. from myapp.utils import *.

To get around this I tried to programatically add the module/package to python sys.path using sys.path.append(os.path.dirname(__file__)) as a kind of hacky solution, but actually that ends up with an error saying __file__ doesn't exist, and if I meant to say __name__.

I'm unsure either the correct way to solve this, or a hack way to solve this at the moment; open to either.

The differences in the freezing are that when ive done it on a local development machine (that works), I've done this in a virtual environment. When I've py2exe.freeze in the GitHub action, that's been from the system install or python. Not really sure any other differences that would cause problems.

Ideally there's a way of fixing the this so the myapp module is appropriately added to the PYTHONPATH. But otherwise, if I need to hard code in a sys.path.append(<path>), I'm not sure where that path would be in a post-frozen project.

Would appreciate any pointers if anyone has experience with this. Thanks.

Here's the build script I use, if it helps give any more context:

# scripts/build.py
import os
import shutil
import tomllib
import pathlib

import py2exe


NAME = "My App"

HERE = pathlib.Path(__file__)
ROOT = HERE.parent.parent
DEST = ROOT / "build"

shutil.rmtree(DEST, ignore_errors=True)


with open("pyproject.toml", "rb") as fp:
    META = tomllib.load(fp)


py2exe.freeze(
    console=[],
    windows=[
        {
            "dest_base": NAME,
            "script": "myapp/gui.py",
        },
    ],
    data_files=None,
    zipfile=None,
    options={
        "dist_dir": DEST,
        "includes": [
            "charset_normalizer.md__mypyc",
        ],
    },
    version_info={
        "project_name": NAME,
        "version": META["project"]["version"],
        "description": META["project"]["description"],
    },
)

nsis_config = f"""\
!define exe '{NAME} Installer.exe'

; Comment out the "SetCompress Off" line and uncomment
; the next line to enable compression. Startup times
; will be a little slower but the executable will be
; quite a bit smaller
SetCompress Off
;SetCompressor lzma

Name '{NAME}'
OutFile '${{exe}}'
;SilentInstall silent
AutoCloseWindow false
ShowInstDetails show
;Icon 'icon.ico'

Section '{NAME}'
    SetOutPath '$PROGRAMFILES64\{NAME}'
    File /r "{os.path.abspath(DEST)}\\*"
    CreateShortCut '$DESKTOP\{NAME}.lnk' '$PROGRAMFILES64\{NAME}\{NAME}.exe'
SectionEnd
"""

with open(ROOT / "install.nsi", "w+") as fp:
    fp.write(nsis_config)

Locally I would run (in a pipenv virtual environment):

$ pipenv run python scripts/build.py

And then use NSIS GUI application to make an installer from the generated install.nsi file. This works on that and other windows machines.

However, this is the Github action that has the path issue (cut down for brevity):

...
    steps:
      - name: Get sources
        uses: actions/checkout@v3
        with:
          submodules: true
          ssh-key: ${{ secrets.BUILD_KEY }}

      - name: Install Windows Dep
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - run: pip install pipenv

      - run: pipenv sync --dev --system

      - run: pip freeze

      - run: python scripts/build.py

      - name: Create installer
        uses: joncloud/makensis-action@v4
        with:
          script-file: "install.nsi"
          arguments: "/V3"
...
@jamesstidard
Copy link
Author

I ended up solving this by switching the GitHub action to instead build a virtual environment instead of installing the dependencies to the --system.

This is odd to me, as I actually thought a virtual environment was discouraged by the documentation - though I might be thinking of pyinstaller.

Anyway, I'm leaving this open as seems to be an issue, and above can hopefully be used to reproduce the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant