qwertylpc
qwertylpc

Reputation: 2126

Running a flask app that uses multiple conda environments

As the title says, I want to either run multiple conda environements from 1 flask app such that certain pages use 1 version of packages and the others use a different version of packages.

Alternatively, I could do something where I run 2 apps concurrently and then would need to be able to properly redirect from one to another.

I scoured the internet and didn't find anything. Any ideas/documentation on where to get started?

EDIT I was told this was a bad idea and to elaborate on the problem rather than my attempted solution

The problem is that I have certain packages that I am trying to interact with 2 different ML models that were done in different versions of scikit. I can't recreate the model because it was given to me by a coworker. Additionally I am doing some name matching using fuzzywuzzy which is causing issues with other packages I need.

Upvotes: 1

Views: 535

Answers (1)

d_kennetz
d_kennetz

Reputation: 5359

You can do what you are asking by installing both versions to different locations (so they don't overwrite each other), and then renaming the package as this seems to be your only option.

Take the following example, I am going to setup 2 virtual environments, in the first I'll install scitkit-learn 0.22.2 and in the second I'll install 0.20.4, then move the name of the package so python can differentiate them and print the version ($ denotes something to enter on the command line):

$ python3 -m venv sk1
$ source sk1/bin/activate
$ pip3 install scikit-learn==0.22.2 # install to venv 1
$ deactivate # leave

$ python3 -m venv sk2
$ source sk2/bin/activate
$ pip3 install scikit-learn==0.20.4 # install to venv 2
$ deactivate

# move the package names
$ mv ./sk1/lib/python3.7/site-packages/sklearn ./sk1/lib/python3.7/site-packages/sklearn0222

$ mv ./sk2/lib/python3.7/site-packages/sklearn ./sk2/libpython3.7/site-packages/sklearn0204

# add both of them to your PYTHONPATH
$ export PYTHONPATH=$PYTHONPATH:$(pwd)/sk1/lib/python3.7/site-packages/sklearn0222
$ export PYTHONPATH=$PYTHONPATH:$(pwd)/sk2/lib/python3.7/site-packages/sklearn0204

Now let's go into the python interpreter, import them:

$ python3
>>> import sklearn0222 as sk0222
>>> import sklearn0204 as sk0204
>>> sk0222.__version__
'0.22.2'
>>> sk0204.__version__
'0.20.4'

This will use the packages version specific code to run, but you have to be SUPER CAREFUL when referencing each and you cannot use both packages within the same module. so in mymodule1.py you can import sklearn0222 and use its submodules and in mymodule2.py you can import sklearn0204 and use its submodules, but if you try to use both in the same module in your program the second will not be recognized.

Again, this is a bad idea but this is a way to get what you are looking for.

Upvotes: 2

Related Questions