2

I am building a plugin in QGIS 3.24 using Python. I want to create a button in the plugin which will in turn download the dependencies such as Pandas, NumPy, GeoPandas, Networkx etc. in the system if its not available.

Is it possible with Python?

2
  • 1
    Where do you want to install these libraries? a separate virtual environment, QGIS Python environment i.e. py3_env, Anaconda environment, or directly in the system ?
    – Taras
    Commented Nov 30, 2022 at 9:30
  • QGIS python env or virtual env
    – BenStone
    Commented Nov 30, 2022 at 9:34

1 Answer 1

6

You can wrap import package line in try-except. If it throws ModuleNorFoundError, then install the package.

import subprocess

try:
    import numpy
except ModuleNotFoundError:
    subprocess.call(['pip', 'install', 'numpy'])
3
  • Could you tell me how to install the library in the virtual environment I have created in vs code using Python? @Kadir Şahbaz
    – BenStone
    Commented Nov 30, 2022 at 11:05
  • 1
    This is a different question which should be another post. For a quick solution, I guess, when you run VsCode inside a virtual environment or you switch your Python interpreter to point to the one within your venv, pip installs packages into virtual environment. I have not tried so far. Commented Nov 30, 2022 at 11:28
  • okay , fine i will check it out
    – BenStone
    Commented Nov 30, 2022 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.