如何在码头工人中运行pipenv?

时间:2017-09-30 15:02:32

标签: docker pipenv

我在我的码头工具中安装pipenv:

RUN pip install pipenv
RUN cd /my/app/path/ && pipenv install
RUN cd /my/app/path/ && pipenv shell

我得到错误:

Traceback (most recent call last):
  File "/usr/local/bin/pipenv", line 11, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pipenv/cli.py", line 2057, in shell
    do_shell(three=three, python=python, fancy=fancy, shell_args=shell_args)
  File "/usr/local/lib/python2.7/dist-packages/pipenv/cli.py", line 1952, in do_shell
    shell = os.path.abspath(PIPENV_SHELL)
  File "/usr/lib/python2.7/posixpath.py", line 360, in abspath
    if not isabs(path):
  File "/usr/lib/python2.7/posixpath.py", line 54, in isabs
    return s.startswith('/')
AttributeError: 'NoneType' object has no attribute 'startswith'

如果我跑

RUN cd /my/app/path/ && pipenv install --system 

相反,我得到另一个错误:

build   30-Sep-2017 16:50:45    Step 5/9 : RUN cd /my/app/path &&     pipenv install --system
build   30-Sep-2017 16:50:45     ---> Running in cffd31633074
build   30-Sep-2017 16:50:46    [91mPipfile.lock not found, creating…
build   30-Sep-2017 16:50:46    [0m[91mLocking [dev-packages] dependencies…
build   30-Sep-2017 16:50:46    [0m[91mLocking [packages] dependencies…
build   30-Sep-2017 16:50:49    [0m[91mCRITICAL:pip.utils:Error [Errno 2] No such file or directory while executing command python setup.py egg_info
build   30-Sep-2017 16:50:49    [0m[91mTraceback (most recent call last):
build   30-Sep-2017 16:50:49      File "/usr/local/bin/pipenv", line 11, in <module>
build   30-Sep-2017 16:50:49        sys.exit(cli())
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 722, in __call__
build   30-Sep-2017 16:50:49    [0m[91m    return self.main(*args, **kwargs)
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 697, in main
build   30-Sep-2017 16:50:49    [0m[91m    rv = self.invoke(ctx)
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 1066, in invoke
build   30-Sep-2017 16:50:49    [0m[91m    return _process_result(sub_ctx.command.invoke(sub_ctx))
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 895, in invoke
build   30-Sep-2017 16:50:49    [0m[91m    return ctx.invoke(self.callback, **ctx.params)
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/vendor/click/core.py", line 535, in invoke
build   30-Sep-2017 16:50:49        return callback(*args, **kwargs)
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/cli.py", line 1782, in install
build   30-Sep-2017 16:50:49    [0m[91m    do_init(dev=dev, allow_global=system, ignore_pipfile=ignore_pipfile, system=system, skip_lock=skip_lock, verbose=verbose, concurrent=concurrent, deploy=deploy)
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/cli.py", line 1290, in do_init
build   30-Sep-2017 16:50:49    [0m[91m    do_lock(system=system)
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/cli.py", line 1080, in do_lock
build   30-Sep-2017 16:50:49    [0m[91m    pre=pre
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/utils.py", line 421, in resolve_deps
build   30-Sep-2017 16:50:49    [0m[91m    resolved_tree.update(resolver.resolve())
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/patched/piptools/resolver.py", line 101, in resolve
build   30-Sep-2017 16:50:49    [0m[91m    has_changed, best_matches = self._resolve_one_round()
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/patched/piptools/resolver.py", line 199, in _resolve_one_round
build   30-Sep-2017 16:50:49    [0m[91m    for dep in self._iter_dependencies(best_match):
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/patched/piptools/resolver.py", line 293, in _iter_dependencies
build   30-Sep-2017 16:50:49        dependencies = self.repository.get_dependencies(ireq)
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/patched/piptools/repositories/pypi.py", line 171, in get_dependencies
build   30-Sep-2017 16:50:49        result = reqset._prepare_file(self.finder, ireq)
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/patched/pip/req/req_set.py", line 639, in _prepare_file
build   30-Sep-2017 16:50:49    [0m[91m    abstract_dist.prep_for_dist()
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/patched/pip/req/req_set.py", line 134, in prep_for_dist
build   30-Sep-2017 16:50:49    [0m[91m    self.req_to_install.run_egg_info()
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/patched/pip/req/req_install.py", line 438, in run_egg_info
build   30-Sep-2017 16:50:49    [0m[91m    command_desc='python setup.py egg_info')
build   30-Sep-2017 16:50:49      File "/usr/local/lib/python2.7/dist-packages/pipenv/patched/pip/utils/__init__.py", line 667, in call_subprocess
build   30-Sep-2017 16:50:49    [0m[91m    cwd=cwd, env=env)
build   30-Sep-2017 16:50:49      File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
build   30-Sep-2017 16:50:49    [0m[91m    errread, errwrite)
build   30-Sep-2017 16:50:49      File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
build   30-Sep-2017 16:50:49    [0m[91m    raise child_exception
build   30-Sep-2017 16:50:49    OSError: [Errno 2] No such file or directory
error   30-Sep-2017 16:50:49    The command '/bin/sh -c cd /opt/supercrunch/function-service/lib &&     pipenv install --system' returned a non-zero code: 1
build   30-Sep-2017 16:50:49    [0mSending build context to Docker daemon 40.96 kB

但是,当我改为做以下事情时:

RUN pip install pipenv
RUN cd /my/app/path && pipenv install
RUN cd /my/app/path && pipenv install --system

它正在运作......

所以有两个问题:第一:为什么pipenv shell给我这个错误?第二:为什么我必须在pipenv install之前pipenv install --system才能让它工作?

我想使用pipenv创建具有不同python版本和不同依赖版本的虚拟环境。

4 个答案:

答案 0 :(得分:40)

考虑到您的问题,我知道最简单的方法是更新到最新版本的pipenv。它仍处于开发阶段,所以很快就会解决问题。

我们正在pipenv使用docker进行制作。我们真的很喜欢它。 有几点需要注意:

  1. 您需要使用--system标志,因此它会将所有软件包安装到系统python中,而不是virtualenv。由于docker容器不需要virtualenv s
  2. 您需要使用--deploy标记,因此如果Pipfile.lock已过期,您的构建将失败
  3. 您需要使用--ignore-pipfile,因此不会弄乱我们的设置
  4. 检查the official docs以确保此信息是最新的。

    总而言之:

    pipenv install --system --deploy --ignore-pipfile
    

    还有一件事。如果您在开发和生产中使用相同的Dockerfile,那么仅对开发环境使用--dev标志也是非常好的。

    另外,请查看我们的django项目模板,查看完整示例:wemake-django-template

答案 1 :(得分:5)

pipenv文档不再正式建议在docker实例中使用--system标志。相反,他们建议使用虚拟环境,因为“它是用于部署到成熟的OS上”。警告:

  

...我相信你们都注意到,大多数容器都是在没有virtualenvs的情况下部署的,我相信目的是保持苗条,并通过尽可能少地安装来减少攻击表面积

https://github.com/pypa/pipenv/pull/2762中所述。

相反,解决方案是运行(如此处的其他答案所述):

RUN pipenv install --deploy --ignore-pipfile

,然后在所有对python的调用前加上pipenv run,例如CMD ["pipenv", "run", "python", "hello.py"]

Ps。我本想将此评论作为已接受答案的注释,但我没有声誉。

答案 2 :(得分:3)

此问题的直接答案是不使用shell,而是使用run

CMD ["pipenv", "run", "python", "my/app.py"]

如果需要更大的灵活性,也可以使用pipenv run sh init.sh,它会创建一个用所有pipenv环境变量初始化的外壳程序。

我实际上更喜欢C. Sweet提到的方法。如果您可以放弃预先构建虚拟环境并简单地复制它(设置PIPENV_VENV_IN_PROJECT,然后使用嵌套的FROM,然后再加上COPY --from=builder-image),则不需要python您最终容器中的pipenvpipenv依赖性也没有。 极大地减小了最终图像的大小。

Dockerfile

WORKDIR /etc/service/
CMD ["sh", "/etc/service/init.sh"]

init.sh

source /etc/service/my/.venv/bin/activate
python my/app.py

答案 3 :(得分:0)

最好不要直接使用 pipenv,因为您的容器中通常不需要 virtualenv。

有一个名为 micropipenv 的工具,您可以在容器中使用它来代替 pipenv。

https://github.com/thoth-station/micropipenv

micropipenv 用例

为什么要使用 micropipenv 而不是 PipenvPoetry

  • 我想要一个“统治一切”的工具——一个轻量级的工具 支持所有 Python 依赖锁文件管理器 (pip-tools, Poetry, Pipenv) 并让用户在部署 Python 应用程序时决定他们想要使用什么 在容器化环境中(例如 Kubernetes、OpenShift 等)。

  • 我想要一个快速且简约的工具来安装软件 CI 中的包。

  • 我希望容器化 Python 应用程序尽可能小 提供构建和运行 Python 所需的最少软件 在生产中的应用。

  • 我想将 Pipenv/Poetry 生成的文件转换为 pip-tools 兼容输出。

  • 我不想安装 Pipenv/Poetry,但我想运行一个项目 使用 Pipenv/Poetry 进行依赖管理(例如受限环境)。

  • 我的 Pipenv 安装坏了,Pipenv upstream did not issue any new Pipenv release <https://github.com/pypa/pipenv/issues/4058>_。

  • 我想将我的应用程序部署到生产环境中 应用程序依赖项由 Pipenv/Poetry 管理(依赖项是 已经解决),但我不想在生产中运行 Pipenv/Poetry(例如 OpenShift 的 s2i 构建过程)。