gpt4 book ai didi

python - 如何在项目内添加python项目的所有依赖?

转载 作者:太空宇宙 更新时间:2023-11-03 11:43:51 26 4
gpt4 key购买 nike

我使用 pip 安装项目依赖项。 Pip 将其所有依赖项安装到此位置

位置:/home/tara/taraproject/myvenv/lib/python2.7/site-packages

What I want to accomplish:

我想把所有的依赖放在项目中,而不是让用户在操作过程中设置它。只是想减轻操作团队的安装负担。

我尝试了什么?

我在我的 ProjectFolder 中尝试了这个,我的工作项目就在那里

/home/tara/taraprject/ProjectFolder

我创建了一个名为 dependencies 的目录,并尝试在其中添加所有依赖模块。然后导入为。

from dependencies.yapsy.PluginManager import PluginManager

我还通过添加 init.py 文件将目录作为模块。

Traceback (most recent call last):
File "main.py", line 16, in <module>
from dependencies.yapsy.PluginManager import PluginManager
File "/home/tara/taraproject/myvenv/checkaccess/dependencies/yapsy/__init__.py", line 73, in <module>
from yapsy.compat import is_py2, str
ImportError: No module named yapsy.compat

看起来导入工作正常,但模块的内部代码在导入时出现错误。

为什么要这样做?

如果我能做到,运维团队就可以拿到我的项目并轻松运行它,而不需要下载依赖项和进行必要的设置。只是想让运维部分更简单

如何解决此类问题或解决方法是什么?

最佳答案

根据所提供的描述,我相信虚拟环境可以为您的问题提供解决方案。来自虚拟环境的介绍(见下面的链接):

Python applications will often use packages and modules that don’tcome as part of the standard library. Applications will sometimes needa specific version of a library, because the application may requirethat a particular bug has been fixed or the application may be writtenusing an obsolete version of the library’s interface.

This means it may not be possible for one Python installation to meet the requirements of every application. If application A needsversion 1.0 of a particular module but application B needs version2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installationfor a particular version of Python, plus a number of additionalpackages.

Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.

有关虚拟环境的更多信息,请访问(快速互联网搜索):

希望这对您有所帮助。

编辑:在评论之后。使用 relocatable 选项可以使虚拟环境变得可重定位。来自用户指南 ( https://virtualenv.pypa.io/en/latest/userguide/#making-environments-relocatable ):

Normally environments are tied to a specific path. That means that you cannot move an environment around or copy it to another computer. You can fix up an environment to make it relocatable

另请参阅:Python3 venv: Can env directory be renamed?

希望这能解决您的问题。

关于python - 如何在项目内添加python项目的所有依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43993946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com