如何在生产环境中为不同的Python包创建不同的环境

huangapple go评论80阅读模式
英文:

How to create diff environments for different Python packages in production

问题

我是新手,对Python和Azure都不太熟悉,所以我的设计中可能会缺少一些关键信息。
我们有一些库是为用户在他们的Azure ML工作区中使用而创建的。现在,如果我们为用户在简单的Jupyter笔记本中使用而创建库,这些问题仍然存在。

现在,这两个库都有不同的包,可能与用户使用的不同。例如:用户可能在使用numpy x.1的环境,但包A可能使用x.2,包B需要x.3。这是可能的,因为所有这些包都是由不同的团队开发的。

那么,在现实世界中如何处理这个问题呢?到目前为止,我能想到以下几种方法:

  1. 在不同的Docker容器中安装这些文件,其中安装了所需的包。然后在不同的环境中完成所需的输出。
  2. 使用Azure自身提供的自定义环境选项。并在不兼容的情况下在不同的环境中运行它们。
  3. 不确定是否可以创建不同的虚拟环境,并在不同的虚拟环境中运行这些包,但如果可能的话,可以类似于这样做。

所以,我想知道在现实世界中是否有正确的方法来处理这个问题。我认为我们应该为每个项目创建一个不同的环境,但是当我们有不同的包需要不同版本的共同依赖项时,该如何处理这种情况呢?

英文:

I am new to Python as well as Azure, so I might be missing some crucial information with my design.
We have couple of libraries which are created to be used by user while working on their Azure ML workspace. Now, this questions remain same if we are creating libraries for user to be used in a simple Jupyter notebook.

Now, both of these libraries have diff packages which could be different then what a user is using. Eg : User might be using an environment which is using numpy x.1 but package A might be using x.2 and package B needs x.3. This is a possibility since all these packages are developed by different teams.

Now, what could be the best way to handle this problem in real world. So far, I am able to come up with below approaches :

  1. Install these files in different docker container where the needed packages are installed. And get the desired output done in separate environments.
  2. Use Custom Environment options provided by Azure itself. And run the incompatibles ones in different environment.
  3. not sure, if we can create different virtual environments and run the packages in different virtual environments but something similar to this if possible.

So, I wanted to know if there is any right way of doing this in real world. I see that we should create a different environment for each project but what about the case when we have different packages which needs different versions of common dependencies. How to handle such case?

答案1

得分: 1

你说得对,您可以创建Docker容器来为不同的库创建单独的环境,以便用户可以单独管理它们。您还可以利用Azure ML自定义环境,为不同的包创建隔离的环境。

对于虚拟环境,有三种选项。第一种选项是直接使用pip安装包,第二种选项是创建一个requirements.txt文件,将所有包添加到该文件中,然后在虚拟环境中运行pip install requirements.txt。第三种选项是使用setup.py,将所有包添加到setup.py中,然后运行setup.py代码来安装这些包,然后根据用户的需求导入它们。

虚拟环境的创建如下:

创建一个如下所示的requirements.txt文件:

numpy==1.19.5
pandas==1.3.0

在Azure ML Notebook中,创建虚拟环境并安装requirements.txt中的包,如下所示:

!python3 -m venv myenv

!source myenv/bin/activate

!pip install -r requirements.txt

您可以参考我的Stack Overflow答案,在那里我创建了一个名为setup.py的Python文件,其中包含我的GitHub存储库中的包,并在笔记本中安装了它们。

您可以直接创建包含包的不同Python文件,并在笔记本中运行它们,如下所示:

setup.py 来自我上面的答案:

from setuptools import setup, find_packages

setup(

name='pycode',

version='0.1',

packages=find_packages(),

install_requires=[

'numpy',

'pandas',

'scikit-learn'

],

entry_points={

'console_scripts': [

'pycode=pycode.cli:main'

]

}

)

输出如下:

如何在生产环境中为不同的Python包创建不同的环境

英文:

You are right, You can create Docker Containers to create separate environment for different libraries for users to manage them separately. You can also make use of azure ML Custom environment and create isolated environment for different packages.

For virtual environment,One option is to directly pip install the packages and second option is to create a requirements.txt file add all the packages in that file, and pip install the requirements.txt in venv. And third option is to use setup.py > Add all your packages in setup.py and run the setup.py code to install those packages and then import them according to the users requirement.

Virtual environment:-

Created one requirements.txt file like below:-

numpy==1.19.5
pandas==1.3.0

如何在生产环境中为不同的Python包创建不同的环境

In the Azure ML Notebook, Create Virtual environment and install the requirements.txt packages like below:-

!python3 -m venv myenv

!source myenv/bin/activate

!pip install -r requirements.txt

如何在生产环境中为不同的Python包创建不同的环境

You can refer my SO thread answer here where I created a setup.py python file with packages in my github repository and installed them in my notebook.

You can directly create different python files with packages and run it in your Notebook like below:-

setup.py from my answer above:-

from setuptools import setup, find_packages

setup(

name='pycode',

version='0.1',

packages=find_packages(),

install_requires=[

'numpy',

'pandas',

'scikit-learn'

],

entry_points={

'console_scripts': [

'pycode=pycode.cli:main'

]

}

)

如何在生产环境中为不同的Python包创建不同的环境

Output:-

如何在生产环境中为不同的Python包创建不同的环境

huangapple
  • 本文由 发表于 2023年5月28日 23:32:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定