如何使用pip将Python软件包安装到Divio Docker项目中? [英] How can I use pip to install Python packages into my Divio Docker project?

查看:94
本文介绍了如何使用pip将Python软件包安装到Divio Docker项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于使用 pip 将Python软件包安装到Django项目的虚拟环境中.

I'm used to using pip to install Python packages into my Django projects' virtual environments.

当我在本地处理Divio Docker项目时,这不起作用.

When I am working with a Divio Docker project locally, this does not work.

推荐答案

将Python软件包安装到Docker项目中时,需要注意两点:

There are two things you need to be aware of when installing Python packages into a Docker project:

  • 该软件包必须安装在正确的环境中
  • 如果您以后想使用已安装的软件包,则需要以更永久的方式进行安装

下面的详细信息描述了使用Divio项目,但是其原理对于其他Docker安装将相似.

The details below describe using a Divio project, but the principle will be similar for other Docker installations.

要在命令行上使用 pip 将Python软件包安装到Dockerized项目中,您需要在Docker内部使用 pip 环境,即在容器内部.

To use pip on the command line to install a Python package into a Dockerised project, you need to be using pip inside the Docker environment, that is, inside the container.

仅位于您可以访问项目文件的目录中是不够的.在这方面,这类似于使用虚拟环境-您需要激活virtualenv .(否则,您的软件包将不会安装在虚拟环境中,而是会安装在您自己的主机环境中.)

It's not enough to be in the directory where you have access to the project's files. In this respect, it's similar to using a virtual environment - you need to have the virtualenv activated. (Otherwise, your package will be installed not in the virtual environment, but on your own host environment.)

要激活虚拟环境,您可以在其上运行类似 source bin/activate 的东西.

To activate a virtual environment, you'd run something like source bin/activate on it.

要在Divio web 容器中安装软件包,请执行以下操作:

To install a package within a Divio web container:

# start a bash prompt inside the project
docker-compose run --rm web bash

# install the package in the usual way
pip install rsa

rsa 现在已安装并可以使用.

rsa is now installed and available to use.

但是,到目前为止,该软件包仅会在该特定容器中安装并可用 .退出bash外壳后,该容器将消失.下次启动 web 容器时,将不会在其中找到 rsa 包.这是因为容器每次都是从其 image 启动的.

So far however, the package will only be installed and available in that particular container. As soon as you exit the bash shell, the container will disappear. The next time you launch a web container, you will not find the rsa package there. That's because the container is launched each time from its image.

要保持该软件包的安装状态,您需要将其包含在映像中.

In order to have the package remain installed, you will need to include it in the image.

Divio项目包含一个 requirements.in 文件,其中列出了将包含在图像中的Python软件包.

A Divio project includes a requirements.in file, listing Python packages that will be included in the image.

在该文件的末尾添加一个包含 rsa 的新行.然后运行:

Add a new line containing rsa to the end of that file. Then run:

docker-compose build web

这将重建Docker映像.下次使用(例如) docker-compose run --rm web bash 启动容器时,它将包含该Python包.

This will rebuild the Docker image. Next time you launch a container with (for example) docker-compose run --rm web bash, it will include that Python package.

(《 Divio开发人员手册》提供了一些其他指导关于使用pip .)

注意:我是Divio团队的成员.这个问题是我们经常通过支持渠道看到的.

Note: I am a member of the Divio team. This question is one that we see quite regularly via our support channels.

这篇关于如何使用pip将Python软件包安装到Divio Docker项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆