Rolf Fankhauser 1 Posted March 22, 2021 Hi all I would like to deploy python with my Delphi application to my customers. So, I downloaded and installed a Windows embeddable package of Python in a subdirectory \python of my project folder. I set the following two lines in the OnCreate event handler of the main form: PythonEngine1.SetPythonHome('\python'); PythonEngine1.LoadDLL; With this setting I can execute Python scripts but I cannot import numpy (ImportError: No module named 'numpy') I can import numpy from the command line, so it seems to be correctly installed: C:\Data\Delphi10-3\TestPythonPerformance\python>python Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> I also tested if P4D uses the correct Python distribution with: import sys print (sys.version) and I get: 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] The structure of the Python installation is as follows (under \python): Scripts share Lib site-packages ... matplotlib ... numpy ... pandas ... Do I need to set a module path that P4D finds the modules? Another problem is that importing matplotlib (even on the command line) rises an error that tkinter is not installed. If I try to install tkinter I get an error: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter Thanks, Rolf Share this post Link to post
Rolf Fankhauser 1 Posted March 22, 2021 I used the following script (from "deploying p4d.pdf") to list the existing modules: import sys import os print("Python path =", sys.path) print() print("Python modules already imported:") for m in sys.modules.values(): if m: print(" ", m) print() print("PYTHONHOME =", os.getenv('PYTHONHOME')) print("PYTHONPATH =", os.getenv('PYTHONPATH')) Output: Python modules already imported: <module '_datetime' (built-in)> <module 'codecs' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\codecs.pyc'> <module 'builtins' (built-in)> <module 'encodings.aliases' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\encodings\\aliases.pyc'> <module '_frozen_importlib_external' (frozen)> <module 'io' (built-in)> <module 'time' (built-in)> <module 'encodings.mbcs' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\encodings\\mbcs.pyc'> <module 'genericpath' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\genericpath.pyc'> <module '_collections_abc' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\_collections_abc.pyc'> <module 'marshal' (built-in)> <module 'sys' (built-in)> <module '_weakrefset' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\_weakrefset.pyc'> <module 'zlib' (built-in)> <module 'os' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\os.pyc'> <module 'ntpath' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\ntpath.pyc'> <module 'stat' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\stat.pyc'> <module 'encodings' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\encodings\\__init__.pyc'> <module '_bootlocale' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\_bootlocale.pyc'> <module 'ntpath' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\ntpath.pyc'> <module '_weakref' (built-in)> <module 'encodings.latin_1' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\encodings\\latin_1.pyc'> <module 'abc' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\abc.pyc'> <module 'errno' (built-in)> <module 'site' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\site.pyc'> <module '_codecs' (built-in)> <module '_sitebuiltins' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\_sitebuiltins.pyc'> <module 'zipimport' (built-in)> <module '_thread' (built-in)> <module '_frozen_importlib' (frozen)> <module '_signal' (built-in)> <module '_locale' (built-in)> <module 'encodings.utf_8' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\encodings\\utf_8.pyc'> <module 'winreg' (built-in)> <module 'sysconfig' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\sysconfig.pyc'> <module 'math' (built-in)> <module 'pyio'> <module 'nt' (built-in)> <module '_stat' (built-in)> <module '_imp' (built-in)> <module '_warnings' (built-in)> <module 'datetime' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\datetime.pyc'> <module '__main__' (built-in)> <module 'io' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\io.pyc'> <module 'encodings.cp1252' from 'C:\\Data\\Delphi10-3\\TestPythonPerformance\\python\\python35.zip\\encodings\\cp1252.pyc'> PYTHONHOME = None PYTHONPATH = None It seems that only modules in python35.zip are imported Share this post Link to post
SwiftExpat 65 Posted April 4, 2021 Hello Rolf, There is a file in the directory with the suffix, should be python35._pth that needs to be modified to import site. Here are the references I used to get past the issue: https://dev.to/fpim/setting-up-python-s-windows-embeddable-distribution-properly-1081 https://www.christhoung.com/2018/07/15/embedded-python-windows/ 1 Share this post Link to post
Rolf Fankhauser 1 Posted April 6, 2021 Hello SwiftExpat Thanks for the links. I already know the first link. The problem is that I have no such file python35._pth. I will check the other link and possibly download and install the embeddable distribution again. Share this post Link to post
Rolf Fankhauser 1 Posted April 6, 2021 I now installed the newest version with python 3.9.4. There is a file python39._pth (I checked it: Indeed, the python35 package didn't have a file python35._pth) So, I changed all according to the instructions. I installed numpy and matplotlib but when I try matplotlib from the command prompt and want to plot I get the following warning: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. Only this backend "agg" is installed, no others. If I try to install Tkinter I get the same error as before with the python35 package: ERROR: Could not find a version that satisfies the requirement Tkinter ERROR: No matching distribution found for Tkinter Can I use a Delphi window as backend? Saving the plot as png or svg and display it in Delphi would be a workaround but rather complicated. Share this post Link to post
Rolf Fankhauser 1 Posted April 6, 2021 I installed the PyQt5 package: ...\python> cd Scripts ...\python\Scripts> pip install PyQt5 Now matplotlib is working with GUI because there is now Qt as a GUI backend. The window doesn't look really native, Tk looks nicer. Share this post Link to post