pip安装环境。

发布于 2025-02-12 14:24:01 字数 379 浏览 0 评论 0原文

我有一个emoverial.yml文件,但不想使用conda:

name: foo
channels:
  - defaults
dependencies:
  - matplotlib=2.2.2

是否可以在pip内安装依赖项emoveration.yml 文件好像是unignts.txt文件?

我尝试了pip install -r emoverition.yml,它与pip == 22.1.2不起作用。

I have an environment.yml file, but don't want to use Conda:

name: foo
channels:
  - defaults
dependencies:
  - matplotlib=2.2.2

Is it possible to have pip install the dependencies inside an environment.yml file as if it's a requirements.txt file?

I tried pip install -r environment.yml and it doesn't work with pip==22.1.2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤芳又自赏 2025-02-19 14:24:02

基于 beni 实现,我只是想调整代码,因为它有很多错误;

import os
import yaml

with open("environment.yaml") as file_handle:
    environment_data = yaml.safe_load(file_handle)

for dependency in environment_data["dependencies"]:
    if isinstance(dependency, dict):
      for lib in dependency['pip']:
        os.system(f"pip install {lib}")

Based on Beni implementation, I just wanted to adjust the code since it has lots of errors;

import os
import yaml

with open("environment.yaml") as file_handle:
    environment_data = yaml.safe_load(file_handle)

for dependency in environment_data["dependencies"]:
    if isinstance(dependency, dict):
      for lib in dependency['pip']:
        os.system(f"pip install {lib}")
我的鱼塘能养鲲 2025-02-19 14:24:02

我已经实现了什么 brian 在他的评论中建议。

这将emoverition.yaml转换为insirase.txt

import yaml

with open("environment.yaml") as file_handle:
    environment_data = yaml.load(file_handle)

with open("requirements.txt", "w") as file_handle:
    for dependency in environment_data["dependencies"]:
        package_name, package_version = dependency.split("=")
        file_handle.write("{} == {}".format(package_name, package_version))

这是用pip

import os
import yaml

with open("environment.yaml") as file_handle:
    environment_data = yaml.load(file_handle)

for dependency in environment_data["dependencies"]:
    package_name, package_version = dependency.split("=")
    os.system("pip install {}=={}".format(package_name, package_version))

注意:我省略了错误处理和包装定义的任何其他变化(例如,包装版本的规范大于或等于某个版本),以使其简单。

I've implemented what Brian suggests in his comment.

This converts the environment.yaml to requirements.txt:

import yaml

with open("environment.yaml") as file_handle:
    environment_data = yaml.load(file_handle)

with open("requirements.txt", "w") as file_handle:
    for dependency in environment_data["dependencies"]:
        package_name, package_version = dependency.split("=")
        file_handle.write("{} == {}".format(package_name, package_version))

And this installs the dependencies directly with pip:

import os
import yaml

with open("environment.yaml") as file_handle:
    environment_data = yaml.load(file_handle)

for dependency in environment_data["dependencies"]:
    package_name, package_version = dependency.split("=")
    os.system("pip install {}=={}".format(package_name, package_version))

NOTE: I've omitted error handling and any other variations of package definitions (e.g., specification of a package version greater than or equal to a certain version) to keep it simple.

匿名。 2025-02-19 14:24:02

不,pip不支持此格式。要求文件的格式记录了在这里。您必须将Environment.yml文件转换为sumplliance.txt手动或通过自动化此过程的脚本。但是,请记住,PYPI上并非所有conda上的包装都可以使用。

No, pip does not support this format. The format it expects for a requirements file is documented here. You'll have to convert the environment.yml file to a requirements.txt format either manually or via a script that automates this process. However, keep in mind that not all packages on Conda will be available on PyPI.

甲如呢乙后呢 2025-02-19 14:24:02

第一个答案提出重要的观点:没有直接的转换,因为conda是一般软件包管理器,因此包括其他包装。此外,Conda软件包通常可以用不同的名称使用。拟议的解析解决方案均未涵盖这种情况。

我个人认为,最有效的完整方法是使用Mamba重新创建环境,然后在环境中使用pip抛弃合法的要求.txt 。

# use mamba, not conda
mamba env create -n foo -f environment.yaml
mamba install -yn foo pip
mamba run -n foo pip list --format freeze > requirements.txt
mamba env remove -n foo

也就是说,不要过度思考并使用可靠的工具。

The first answer makes important points: there is not direct conversion because Conda is a general package manager and so includes additional packages. Furthermore, Conda packages can often go by different names. None of the proposed parsing solutions cover this situation.

Personally, I think the most efficacious complete approach is to recreate the environment with Mamba, then use pip in the environment to dump out a legitimate requirements.txt.

# use mamba, not conda
mamba env create -n foo -f environment.yaml
mamba install -yn foo pip
mamba run -n foo pip list --format freeze > requirements.txt
mamba env remove -n foo

That is, don't overthink it and use the reliable tools at hand.

旧夏天 2025-02-19 14:24:02

如果您不想使用脚本,我使用以下单元字节来生成需求。从env.yml文件中txt文件:

❯ grep -n "pip:" env.yml | cut -d":" -f1 | xargs -I{} expr {} + 1 | xargs -I{} tail -n +{} env.yml | sed 's/^[[:space:]]*- //' | cut -d"=" -f 1 > requirements.txt

这只是将所有依赖项都取决于pip YML的依赖性并删除了固定版本以单独处理PIP。

If you don't want to use a script I've used the following one-liner to generate the requirements.txt file from an env.yml file:

❯ grep -n "pip:" env.yml | cut -d":" -f1 | xargs -I{} expr {} + 1 | xargs -I{} tail -n +{} env.yml | sed 's/^[[:space:]]*- //' | cut -d"=" -f 1 > requirements.txt

This just takes all of the dependencies under the pip dependency in the yml and removes the pinned version for pip to handle on its own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文