如何在项目中添加python项目的所有依赖项?
作者:互联网
我用pip安装项目依赖项. Pip将其所有依赖项安装到此位置
位置:/home/tara/taraproject/myvenv/lib/python2.7/site-packages
What I want to accomplish:
我希望将所有依赖项放在项目中,而不是让用户在操作期间进行设置.只是试图将安装负担移除给操作团队.
我尝试了什么?
我在我的ProjectFolder中尝试了这个,我的工作项目存在于其中
/家庭/塔拉/ 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.
有关虚拟环境的更多信息,请访问(快速互联网搜索):
> https://docs.python.org/3/tutorial/venv.html(Python 3)
> http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/(Python 2)
> https://realpython.com/blog/python/python-virtual-environments-a-primer/
希望这可以帮助.
编辑:跟随评论.使用可重定位选项可以使虚拟环境可重定位.来自用户指南(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,pip,python-module,dependencies 来源: https://codeday.me/bug/20190705/1391468.html