This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Embedded 3.6.4 distribution does not add script parent as sys.path[0]
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Cong Monkey, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2018-03-06 11:12 by Cong Monkey, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg313316 - (view) Author: Cong Monkey (Cong Monkey) Date: 2018-03-06 11:12
Embedded 3.6.0 distribution does not insert script parent in sys.path[0], but the normal python do it.

this make some failed, like when I try to do pip install future, it will failed as import src.future failed, which works for normal python.

The root source maybe when python36._pth exist, python rewrite the flag and mark as isolate mode automate, which will not update sys.path when pymain_init_sys_path.

I use a trick which is really bad as a work around(and even when site .main the sys.argv is not ready!), and hope upstream will fix the root source.

===============begin in my usercustomize.py:================
import sys

import pathlib



class DummyImportHook(object):

    def __init__(self, *args):

        self.is_script_path_to_sys_path_be_done = False

        pass


    def find_module(self, fullname, path=None):

        # print(f'{DummyImportHook.__name__} trigger {sys.argv if hasattr(sys, "argv") else ""} ')

        if not self.is_script_path_to_sys_path_be_done and hasattr(sys, 'argv'):

            if sys.argv[0] is not None:

                # print(f'{DummyImportHook.__name__}:argv is {sys.argv}')

                path_obj = pathlib.Path(sys.argv[0])

                # #if path_obj.exists():

                # print(f'{DummyImportHook.__name__}:I am try to add {str(path_obj.parent)} to sys.path')

                sys.path.insert(0, str(path_obj.parent))

                print(f'{DummyImportHook.__name__}:current sys.path is {sys.path}')

                pass

            self.is_script_path_to_sys_path_be_done = True

            pass

        return None

        pass



print(f'{DummyImportHook.__name__}:auto script path to sys.path hook load!')



#sys.meta_path = [DummyImportHook()]

sys.meta_path.insert(0,DummyImportHook())


===============end in my usercustomize.py:================
===============begin in my python36._pth========================
python36
.
# Uncomment to run site.main() automatically
import site
===============end in my python36._pth========================

BTW, where is Embedded distribution package script in python git repo?
msg313341 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-03-06 17:25
This is the intentional design of the embedded distribution. You need to update the ._pth file to include the directories that have files that are part of your app - it's not meant for running random scripts, that's the job of the normal distribution.

The build script files are in Tools/msi with the other installer files, primarily make_zip.py, though that also gets used for the Nuget packages.
History
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77192
2018-03-06 17:25:59steve.dowersetstatus: open -> closed
resolution: not a bug
messages: + msg313341

stage: resolved
2018-03-06 11:13:40Cong Monkeysetnosy: + paul.moore, tim.golden, zach.ware, steve.dower
type: behavior
components: + Windows
2018-03-06 11:12:50Cong Monkeycreate