admin管理员组

文章数量:1289529

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.

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.

Share Improve this question asked Feb 20 at 6:33 RimfireRimfire 4001 gold badge4 silver badges14 bronze badges 2
  • 2 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'; OR 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 – Cool Breeze Commented Feb 20 at 6:43
  • Great info, that works. Even under Lazarus and Linux. Make it an answer. – Rimfire Commented Feb 20 at 7:01
Add a comment  | 

1 Answer 1

Reset to default 2

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

本文标签: