Recent

Author Topic: Python for Lazarus and pip  (Read 1034 times)

superc

  • Full Member
  • ***
  • Posts: 235
Python for Lazarus and pip
« on: December 20, 2022, 03:58:13 pm »
Hello,

I'm tried to run demo of Python for Lazarus and all fine to go; in the project directory must reside python.dll and python.zip and msvcr100.dll and the interpreter accept command as REPL style. Now i want to use an external librarys and I want to understand if it is possible to use Python for Lazarus with downloaded libraries (with  pip or not).

Thanks in advance.

Thaddy

  • Hero Member
  • *****
  • Posts: 12971
Re: Python for Lazarus and pip
« Reply #1 on: December 20, 2022, 04:04:34 pm »
pip's are not a problem: that is a dependency on Python, not Freepascal. ( Of course they need to be called from python, not freepascal).
I actually get compliments for being rude... (well, Dutch, but that is the same)

superc

  • Full Member
  • ***
  • Posts: 235
Re: Python for Lazarus and pip
« Reply #2 on: December 20, 2022, 04:11:22 pm »
pip's are not a problem: that is a dependency on Python, not Freepascal. ( Of course they need to be called from python, not freepascal).

For explain me well, I have installed on my Windows os a standard installation of python 3.10' and setted on path, on my Lazarus program I settet and python3.dll ecc... ecc.. Component Python for Lazarus use local directory dll; is it possible in this condition to download libraries for python and use them in the program programmed in Lazarus?

AlexTP

  • Hero Member
  • *****
  • Posts: 2113
    • UVviewsoft
Re: Python for Lazarus and pip
« Reply #3 on: December 20, 2022, 04:12:23 pm »
1. Add needed dirs to Python's sys.path.(in module 'sys'). You can do it in the REPL demo.
2. Just do 'import modulename' in the REPL.

superc

  • Full Member
  • ***
  • Posts: 235
Re: Python for Lazarus and pip
« Reply #4 on: December 21, 2022, 11:06:05 am »
1. Add needed dirs to Python's sys.path.(in module 'sys'). You can do it in the REPL demo.
2. Just do 'import modulename' in the REPL.

I want use mathplotlib to my project and i added from python console of test program a path of library: I downloaded library matplotlib with command:

"pip install matplotlib --target=c:\pl --no-user"

when launch from command console "import matplotlib.pyplot as plt" i receive an error like the photo; How can I fix it (if possible)?

Thanks in advance.
« Last Edit: December 21, 2022, 11:13:35 am by superc »

AlexTP

  • Hero Member
  • *****
  • Posts: 2113
    • UVviewsoft
Re: Python for Lazarus and pip
« Reply #5 on: December 21, 2022, 11:32:14 am »
That is not simple question for me. Maybe ask the same at this GitHub repo:
https://github.com/pyscripter/python4delphi/issues

Maybe, 'os' is not in the sys.path ? Did you add matplotlib to sys.path not breaking old value?
« Last Edit: December 21, 2022, 11:34:24 am by AlexTP »

superc

  • Full Member
  • ***
  • Posts: 235
Re: Python for Lazarus and pip
« Reply #6 on: December 21, 2022, 11:45:58 am »
Did you add matplotlib to sys.path not breaking old value?

what do you mean?

AlexTP

  • Hero Member
  • *****
  • Posts: 2113
    • UVviewsoft
Re: Python for Lazarus and pip
« Reply #7 on: December 21, 2022, 12:00:14 pm »
I mean the sys.path must be modified carefully: like this

import sys
sys.path.append(new_dir)

superc

  • Full Member
  • ***
  • Posts: 235
Re: Python for Lazarus and pip
« Reply #8 on: December 21, 2022, 01:04:55 pm »
I saw that in delphi the components used for python are:

https://github.com/pyscripter/python4delphi

I installed and tested demo and setted the same directory into program for new path of library: now I have problem with numpy library....

AlexTP

  • Hero Member
  • *****
  • Posts: 2113
    • UVviewsoft
Re: Python for Lazarus and pip
« Reply #9 on: December 21, 2022, 01:08:18 pm »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1204
Re: Python for Lazarus and pip
« Reply #10 on: December 21, 2022, 03:54:00 pm »
Hello,
This works for me with Lazarus 2.2 32 bits on windows 11.
1 - Install a complete version of Python (same type that Lazarus ( for me python 3.10 32 bits)) with options pip and tcl/tk. Embedded version has no pip script.

2 - To install a pip python package open a terminal console and change the directory to where is python.
3 - For example to install matplotlib pip package :
Quote
D:\Logiciels\Python\Python310-32>Scripts\pip install matplotlib
Collecting matplotlib
  Downloading matplotlib-3.6.2-cp310-cp310-win32.whl (7.1 MB)
in attachment a python4Lazarus project to plot a figure.
Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

alaa123456789

  • Full Member
  • ***
  • Posts: 237
  • Try your Best to learn & help others
    • youtube:
Re: Python for Lazarus and pip
« Reply #11 on: December 22, 2022, 01:36:33 pm »

[/quote]
in attachment a python4Lazarus project to plot a figure.
Friendly, J.P
[/quote]
hi Jp
the question how to distribute my application to others if i am using different packages of python installed by Pip

thanks

Bogen85

  • Hero Member
  • *****
  • Posts: 572
Re: Python for Lazarus and pip
« Reply #12 on: December 22, 2022, 01:52:40 pm »
the question how to distribute my application to others if i am using different packages of python installed by Pip

Which is more of python question.... (regarding python virtual environments).

You could use a wrapper "script" (or some other mechanism) to create a python venv and activate it, then launch your app in that activated venv.

Unless python4delphi already has direct support for python venv you would need to use your own wrapper mechanism.

superc

  • Full Member
  • ***
  • Posts: 235
Re: Python for Lazarus and pip
« Reply #13 on: January 03, 2023, 02:38:12 pm »
the question how to distribute my application to others if i am using different packages of python installed by Pip

my problem is exactly this: I want to distribute both the contents of the python embeddable package, and also the numpy and matplotlib package; if I download them in the same directory as python.dll and add with sys.path.append the path downloaded librarys obtain this error :

Code: Pascal  [Select][+][-]
  1. Traceback (most recent call last):
  2.   File "<string>", line 1, in <module>
  3.   File "C:\sviluppo_software\Python_Console\libPython\numpy\__init__.py", line 139, in <module>
  4.     from . import _distributor_init
  5.   File "C:\sviluppo_software\Python_Console\libPython\numpy\_distributor_init.py", line 9, in <module>
  6.     import glob
  7. ModuleNotFoundError: No module named 'glob'
  8.  

I find it strange that there isn't a way to make this mechanism work without using venv.


//update 14:48

I realized now that in the TPthonEngine component there is a property called 'VenvPythonExe': it seems interesting.

« Last Edit: January 03, 2023, 02:50:54 pm by superc »

AlexTP

  • Hero Member
  • *****
  • Posts: 2113
    • UVviewsoft
Re: Python for Lazarus and pip
« Reply #14 on: January 03, 2023, 02:52:13 pm »
Then the 'glob' module cannot be found - can you find it's folder and add it to sys.path?

 

TinyPortal © 2005-2018