Home > Mobile >  A project's three module rely on three different virtual environment
A project's three module rely on three different virtual environment

Time:05-10

Now I have a project and I think it can be divided into three different modules like below.

project
│   README.md
│ 
└───flask_web
│   └───app_folder
│   |   └───  ...
|   |   └───  ...
|   |   |   __init__.py
|   |
│   │   app_run.py
│   └───venv
│       └──bin        
|   
│      
└───Fate
|   └─── ...
|   └───env
|       └───python36
|           └───venv
|               └───bin
|
└───fake_data_algorithm
    └─── ...
    └───venv
        └───bin

The first part 'web' is a flask application providing web service, it requires a virtual environment.

The second part 'fate' is a open source project for federal learning and also needs a virtual environment.If I want to start the system for federal learning process, I have to source 'fate/bin/init_env.sh' to activate the providing environment.

The third part 'fake_data_algorithm' is used to generate fake data based on the 'data' folder, which also requires virtual environment for some python packages.

Because the after the 'web' application getting the request from browser, it will call 'fate' or 'fake_data_algorithm', but both of them are in a different virtual environment, so I should source the environment again.

Something really puzzles me:Is there a mechanism that can enable the whole project keep running without switching the virtual environment.And because the "fate" is a relatively close part, I want to keep the virtual environment of 'fate' as far as possible(try to avoid put all the python packages in a single environment under project).

CodePudding user response:

Something really puzzles me:Is there a mechanism that can enable the whole project keep running without switching the virtual environment.And because the "fate" is a relatively close part, I want to keep the virtual environment of 'fate' as far as possible(try to avoid put all the python packages in a single environment under project).

Switching virtual environment is just a comfort feature when you are inputting commands into your CLI. You do not have to switch virtual environments manually.

You can write a small batch script to execute your projects:

#!/bin/sh

cd /home/username/project/flask_web &&
./venv/bin/python ./app_run.py

This will set the working directory to /home/username/project/flask_web (important...) and use the working directory's virtual environment to execute your project.

Depending on your IDE you can also select in the project settings which Python Interpreter/Virtual Environment to use. So in your IDE you can have multiple projects opened but they all use different Python Interpreters and libraries.

  • Related