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

标签 python dependencies pip python-module

我使用 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’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using 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 needs version 1.0 of a particular module but application B needs version 2.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 installation for a particular version of Python, plus a number of additional packages.

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/

相关文章:

python - 如何解决 python3 的此 pip 错误?

python - 只找到没有 child 的轮廓

Python - 将 2 个类导入主文件

visual-studio - Xamarin-如何更新Mono.Android版本以解决依赖关系?

powershell - Pip.exe已安装。如何使用?

python - 无法使用pip在Ubuntu 20.04 LTS上安装Tensorflow 1.x

Python:将字段值提取到新列中,写入Excel

python - 按经纬度选择数据

java - 用于解决与云 API 的依赖关系的 Maven

C++ 依赖管理器