Medical Imaging Interaction Toolkit  2025.12.02
Medical Imaging Interaction Toolkit
QmitkSetupVirtualEnvUtil.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.s
10 
11 ============================================================================*/
12 
13 #ifndef QmitkSetupVirtualEnvUtil_h_Included
14 #define QmitkSetupVirtualEnvUtil_h_Included
15 
16 #include "mitkLog.h"
17 #include "mitkProcessExecutor.h"
19 #include <QString>
20 #include <functional>
21 
27 {
28 public:
29  using CallbackType = void (*)(itk::Object *, const itk::EventObject &, void *);
30 
31  QmitkSetupVirtualEnvUtil(const QString& baseDir);
33 
42  bool SetupVirtualEnv(const QString &venvName,
43  const QStringList &packages,
44  std::function<bool()> validator,
45  CallbackType printCallback,
46  const QString &torchVersion = "");
47 
54  virtual QString GetVirtualEnvPath();
55 
66  void PipInstall(const std::string &library,
67  const std::string &workingDir,
68  CallbackType callback,
69  const std::string &command = "pip3");
70 
78  void PipInstall(const std::string &library, CallbackType callback,
79  const std::string &command = "pip3");
80 
81 
91  void ExecutePython(const std::string &args,
92  const std::string &pythonPath,
93  CallbackType callback,
94  const std::string &command = "python");
95 
104  void ExecutePython(const std::string &args, CallbackType callback,
105  const std::string &command = "python");
106 
116  void InstallPytorch(const QString &workingDir, CallbackType callback, const QString &torchVersion = "");
117 
122  void InstallPytorch(const QString& torchVersion = "");
123 
128  void InstallPytorch(CallbackType callback, const QString& torchVersion = "");
129 
130 
136  QString& GetBaseDir();
137 
144 
150  QString& GetPythonPath();
151 
157  QString& GetPipPath();
158 
164  void SetSystemPythonPath(const QString& path);
165 
171  void SetPythonPath(const QString& path);
172 
178  void SetPipPath(const QString& path);
179 
185  void SetVirtualEnvPath(const QString &path);
186 
194  bool IsPythonPath(const QString &pythonPath);
195 
200  static bool IsVenvInstalled(const QString &pythonPath);
201 
206  static QString GetPipPackageVersion(const QString &pythonPath, const QString &packageName);
207 
213  static void PrintProcessEvent(itk::Object *, const itk::EventObject &e, void *);
214 
220  static std::pair<QString, QString> GetExactPythonPath(const QString &pyEnv);
221 
226  static QStringList AutoParsePythonPaths();
227 
228 private:
229  QString m_PythonPath;
230  QString m_PipPath;
231  QString m_BaseDir;
232  QString m_venvPath;
233  QString m_SysPythonPath;
234 };
235 #endif
#define MITKSEGMENTATIONUI_EXPORT
Abstract Class to Setup a python virtual environment and pip install required packages....
void ExecutePython(const std::string &args, const std::string &pythonPath, CallbackType callback, const std::string &command="python")
Function to execute any python code given a python path. Any callback function can be passed to proce...
void ExecutePython(const std::string &args, CallbackType callback, const std::string &command="python")
Overloaded function to Execute Python code. Any callback function can be passed to process the output...
static std::pair< QString, QString > GetExactPythonPath(const QString &pyEnv)
Get the exact Python path and version for any OS from the virtual environment path.
void PipInstall(const std::string &library, CallbackType callback, const std::string &command="pip3")
Overloaded function to Pip install a library function.
void(*)(itk::Object *, const itk::EventObject &, void *) CallbackType
static QStringList AutoParsePythonPaths()
Searches and parses paths of python virtual environments from predefined lookout locations.
static void PrintProcessEvent(itk::Object *, const itk::EventObject &e, void *)
Function can be used as callback to simply print out all the process execution output parsed out from...
void InstallPytorch(const QString &workingDir, CallbackType callback, const QString &torchVersion="")
Installs pytorch using light-the-torch package, correctly identifying cuda version....
void InstallPytorch(CallbackType callback, const QString &torchVersion="")
Overloaded function to install pytorch using light-the-torch package, correctly identifying cuda vers...
void SetPipPath(const QString &path)
Set the Pip Path object.
void SetSystemPythonPath(const QString &path)
Set the System Python Path object.
void InstallPytorch(const QString &torchVersion="")
Overloaded function to install pytorch using light-the-torch package, correctly identifying cuda vers...
QString & GetSystemPythonPath()
Get the System Python Path object.
static QString GetPipPackageVersion(const QString &pythonPath, const QString &packageName)
Returns version of the pip installed package.
void SetVirtualEnvPath(const QString &path)
Set the Virtual Env Path object.
void SetPythonPath(const QString &path)
Set the Python Path object.
QmitkSetupVirtualEnvUtil(const QString &baseDir)
virtual QString GetVirtualEnvPath()
Get the Virtual Env Path object. Override this method in the respective tool installer class.
QString & GetBaseDir()
Get the Base Dir object.
bool IsPythonPath(const QString &pythonPath)
Check if the path provide has python executable or not.
static bool IsVenvInstalled(const QString &pythonPath)
Checks if venv module is available for the python.
QString & GetPipPath()
Get the Pip Path object.
QString & GetPythonPath()
Get the Python Path object.
void PipInstall(const std::string &library, const std::string &workingDir, CallbackType callback, const std::string &command="pip3")
Function to Pip install a library package given the location of pip3 executable. Any callback functio...
bool SetupVirtualEnv(const QString &venvName, const QStringList &packages, std::function< bool()> validator, CallbackType printCallback, const QString &torchVersion="")
Sets up a python virtual environment in the DKFZ directory with given.