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

Retaining original path when using PYAPP_EXEC_SCRIPT #47

Closed
mortenee opened this issue Jun 26, 2023 · 2 comments
Closed

Retaining original path when using PYAPP_EXEC_SCRIPT #47

mortenee opened this issue Jun 26, 2023 · 2 comments

Comments

@mortenee
Copy link

mortenee commented Jun 26, 2023

I did some testing of the new PYAPP_EXEC_SCRIPT option(with PYAPP_EXEC_SCRIPT=~/bin/PWXtract/bin/main.py --force --root ~/bin/PWXtract) . It works really well except for one thing.
When running a script like this the path is changed and in my test case gives /home/pwb/.cache/pyapp/scripts/11235863200305506371/main.py rather than /home/pwb/bin/PWXtract/bin/main.py
Is there any way of disabling the caching (or otherwise) so that the original path is retained? Which would also work after moving the full directory of code files and pyapp binary to a new path?
Exporting the path of the pyapp binary to an environmental path on execution would also work as a workaround.

Thanks a lot for making this by the way :)

@ofek
Copy link
Owner

ofek commented Jun 26, 2023

For the workaround you mentioned, does this suffice? https://ofek.dev/pyapp/latest/config/#installation-indicator

@mortenee
Copy link
Author

mortenee commented Jun 27, 2023

It does! Having this as the script file launching the actual script worked rather nicely :)

import os
import sys
import runpy
import site
import subprocess
from pathlib import Path

bin_dir = Path(os.environ["PYAPP"]).parent.absolute()
project_dir = bin_dir.parent
libs_dir = Path(bin_dir, "libs")
req_file = Path(project_dir, "requirements.txt")

libs_dir.mkdir(parents=True, exist_ok=True)
if len(os.listdir(libs_dir)) == 0:
    print("Installing missing dependencies...")
    cmd = [sys.executable, "-m", "pip", "install", "--target", libs_dir, "-r", req_file]
    proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, universal_newlines=True)
    result = proc.communicate()[1]
    if "ERROR:" in result:
        print(result)
        sys.exit()

site.ENABLE_USER_SITE = True
site.addsitedir(libs_dir)
site.addsitedir(bin_dir)

runpy.run_path("main.py", run_name="__main__")

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

2 participants