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.
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:
* The start-up value for sys.path seems to be defined in python311._pth.
It looks that I can add further static paths to it, but I don't know how
to make it add the script path (which can be different for different scripts).
* Uncommenting "import site" in python311._pth doesn't help.
* It seems that I could import something else in python311._pth, but I
don't know how something imported from there could find out the path of
the script that is about to be started.
* I read the (rather short) documentation of the embeddable package and
of the site module several times but couldn't recognize a hint as to how
to solve the issue.
* 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
That works, but it's ugly, executing code between imports is frowned
upon, and it needs to be added to every script.
Does anybody have a better idea?
Any help is appreciated.
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
[...]
I haven't worked with embeddable python, but here are some possibilitiesI didn't think of sys.argv[0] to get at the path; this might be quite
that came to mind, depending on how your system works -
1. If your script is started from the command line, sys.argv[0] gives
the path to the script;
You could use os.path.dirname() to get itsYes, but it requires another import and the embedded package is only
directory. This will end up the same place as your code fragment, but
looks nicer and handles different path separators (e.g., Linux vs Windows);
2. You could write a little module that figures out the script's pathI thought about that, but for that to work all local modules across all
and import that first in all your scripts.
3. If you know all the directories that your scripts will be in, you
could add them all to a xx.pth file (do a search to make sure where to
put .pth files for an embeddable case).
[...}
On 4/20/23 15:47, 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.
This is intended behavior - the question comes up from time to time. The embeddable distribution is intended to be part of an application, not a general-purpose Python you can call for just anything.
There are a bunch of details here, for example:
https://github.com/python/cpython/issues/79022
How are you invoking your script? Presumably you have some code
in your embedding application that takes a script path and runs
it. Instead of putting the code to update sys.path into every
script, the embedding application could do it before running
the script.
Am 22.04.2023 um 03:27 schrieb Greg Ewing via Python-list:
How are you invoking your script? Presumably you have some codeIn principle a good idea, but I don't know how to do that:
in your embedding application that takes a script path and runs
it. Instead of putting the code to update sys.path into every
script, the embedding application could do it before running
the script.
The script is currently invoked by a .cmd file, but that may change to a shortcut (.lnk). This is what the embeddable package documentation calls "Python Application - simple approach".
To update sys.path on start up I would need to do something like
C:\path\to\python.exe --add-path C:\s-path C:\s-path\script.py
but I couldn't find an option like --add-path.
I thought about that, but for that to work all local modules across all script locations must have unique names, otherwise import might get hold
of a module from the wrong directory.
Am 22.04.2023 um 03:27 schrieb Greg Ewing via Python-list:
How are you invoking your script? Presumably you have some codeIn principle a good idea, but I don't know how to do that:
in your embedding application that takes a script path and runs
it. Instead of putting the code to update sys.path into every
script, the embedding application could do it before running
the script.
The script is currently invoked by a .cmd file, but that may change to a shortcut (.lnk). This is what the embeddable package documentation calls "Python Application - simple approach".
To update sys.path on start up I would need to do something like
C:\path\to\python.exe --add-path C:\s-path C:\s-path\script.py
but I couldn't find an option like --add-path.
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
[...]
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 10:55:54 |
| Calls: | 12,100 |
| Files: | 15,003 |
| Messages: | 6,517,990 |