diff --git a/src/shiv/bootstrap/__init__.py b/src/shiv/bootstrap/__init__.py index b6ad546..84a3b76 100644 --- a/src/shiv/bootstrap/__init__.py +++ b/src/shiv/bootstrap/__init__.py @@ -191,12 +191,17 @@ def bootstrap(): # pragma: no cover # get a handle of the currently executing zip file with current_zipfile() as archive: + if archive: + # create an environment object (a combination of env vars and json metadata) + env = Environment.from_json(archive.read("environment.json").decode()) - # create an environment object (a combination of env vars and json metadata) - env = Environment.from_json(archive.read("environment.json").decode()) - - # get a site-packages directory (from env var or via build id) - site_packages = cache_path(archive, env.root, env.build_id) / "site-packages" + # get a site-packages directory (from env var or via build id) + site_packages = cache_path(archive, env.root, env.build_id) / "site-packages" + else: + # not in zip file, now it is in a directory + parent_dir = Path(os.path.abspath(sys.argv[0])).parent + env = Environment.from_json(open(parent_dir / 'environment.json').read()) + site_packages = Path(parent_dir / 'site-packages') # determine if first run or forcing extract if not site_packages.exists() or env.force_extract: diff --git a/src/shiv/builder.py b/src/shiv/builder.py index 58cf855..c7cf602 100644 --- a/src/shiv/builder.py +++ b/src/shiv/builder.py @@ -48,6 +48,10 @@ def iter_package_files(package: Union[str, ModuleType]) -> Iterator[Tuple[Path, # zipapp __main__.py template MAIN_TEMPLATE = """\ # -*- coding: utf-8 -*- +# add current path to sys.path for module search +import sys +sys.path.insert(0, '.') + import {module} {module}.{fn}() """