Search code examples
pythondelphilazarus

Using a python library installed in virtual environment from script running in standalone application


There is a python library that only wants to be installed in a virtual environment, but how to import the library into scripts running in my standalone application that does not run in a virtual environment?

I'm writing a a Delphi/Lazarus application using the component Python4Delphi and Python4Lazarus to run python scripts.


Solution

  • There are two ways you can try to fix this:

    1. In your Delphi/Lazarus application, you could configure Python4Delphi to use the Python interpreter from your virtual environment instead of the system Python. This way, it will automatically have access to all packages installed in that virtual environment.
    PythonEngine1.DllPath := 'PATH';
    
    1. You can add the venv's package directory into your standalone application like:
    import sys import os 
    venv_path = r"C:\path\to\your\venv\Lib\site-packages" 
    
    if venv_path not in sys.path: 
        sys.path.append(venv_path) # Now you can import your library import your_library