Incomplete sys.path with embeddable python (Windows)!?
Mats Wichmann
mats at wichmann.us
Sun Apr 23 12:17:26 EDT 2023
On 4/22/23 16:04, Ralf M. wrote:
> Am 21.04.2023 um 18:07 schrieb Thomas Passin:
>> On 4/20/2023 5:47 PM, Ralf M. wrote:
>>> Hello,
>>>
>>> when I run a script with a "normally" installed python, the directory
>>> the script resides in is automatically added as first element to
>>> sys.path, so that "import my_local_module" finds my_local_module.py
>>> in the directory of the script.
>>>
>>> However, when I run the same script with embeddable python ("Windows
>>> embeddable package (64-bit)", download link
>>> https://www.python.org/ftp/python/3.11.3/python-3.11.3-embed-amd64.zip) the script directory is *not* prepended to the path, thus "import my_local_module" gives an ImportError.
>>>
>>> I couldn't find an option to get the "normal" behaviour. Any ideas
>>> how to do that?
>>>
>>> What I tried so far:
>>> [...]
>>> * I can add the following lines to every script:
>>> import sys
>>> script_path = __file__.rsplit("\\", 1)[0]
>>> if script_path not in sys.path:
>>> sys.path[0:0] = [script_path]
>>> import my_local_modul
>>> [...]
Have used this stanza (actually, for the inverse purpose, to remove that
script's dir from sys.path, but whatever...):
script_dir = os.path.dirname(os.path.realpath(__file__))
you can then insert it:
sys.path.insert(0, script_dir)
or just add:
sys.path = [script_dir] + sys.path
assuming you want it at the front...
but it doesn't really solve your problem of needing it *everywhere*...
More information about the Python-list
mailing list