简体   繁体   English

pipenv + pyenv + jupyter

[英]pipenv + pyenv + jupyter

I use pyenv to install and manage both different versions of python and virtual environments for various projects on my system. 我使用pyenv为我的系统上的各种项目安装和管理不同版本的python和虚拟环境。 But pipenv is the recommended tool for managing virtual environments now, and I like a lot of its features. 但pipenv现在是管理虚拟环境的推荐工具,我喜欢它的很多功能。 Everything works for the most part, but pipenv seems to have a leak or something when it comes to Jupyter notebook. 在大多数情况下,一切都有效,但是当涉及到Jupyter笔记本时,pipenv似乎有泄漏。 Here are the steps which replicate the problem on my machine. 以下是在我的机器上复制问题的步骤。

  1. Install pipenv using brew install pipenv . 使用brew install pipenv
  2. Install a new version of Python using, say, pyenv install 3.6.3 , and activate it via pyenv global 3.6.3 . 使用pyenv install 3.6.3安装新版本的Python,并通过pyenv global 3.6.3激活它。
  3. Install Jupyter notebook and another package, eg pip install jupyter requests . 安装Jupyter笔记本和另一个软件包,例如pip install jupyter requests
  4. Create a new directory and navigate into it; 创建一个新目录并导航到它; create a pipenv project using pipenv install 使用pipenv install创建一个pipenv项目
  5. Activate the pipenv virtual environment using pipenv shell . 使用pipenv shell激活pipenv虚拟环境。
  6. Do not install any packages, and confirm that none are installed in the current virtualenv using pip list . 不要安装任何软件包,并使用pip list确认当前virtualenv中没有安装任何软件包。 (I only get pip , setuptools , and wheel .) (我只得到pipsetuptoolswheel 。)
  7. Run jupyter notebook . 运行jupyter notebook My expectation: the console errors out with jupyter: command not found . 我的期望:控制台错误与jupyter: command not found Actual: the notebook server and browser UI launch as normal. 实际:笔记本服务器和浏览器UI正常启动。
  8. Create a new notebook and import the auxiliary package from step 3: import requests . 创建一个新笔记本并从步骤3 import requests辅助包: import requests My expectation: a ModuleNotFoundError is thrown. 我的期望:抛出ModuleNotFoundError Actual: the package is imported and can be used as normal. 实际:包是导入的,可以正常使用。

If I try to import the auxiliary package in the pipenv virtual environment but outside Jupyter notebook (in the repl or in a Python script) , I get a ModuleNotFoundError as expected. 如果我尝试在pipenv虚拟环境中导入辅助包但在Jupyter笔记本之外(在repl或Python脚本中),我会按预期得到ModuleNotFoundError So somehow the problem is with jupyter specifically. 所以问题是jupyter特别是问题。 Note that if I try to recreate this problem using a pyenv virtual environment instead of a pipenv virtual environment then the expected behavior occurs: jupyter notebook produces a jupyter: command not found error in the console. 请注意,如果我尝试使用pyenv虚拟环境而不是pipenv虚拟环境重新创建此问题,则会出现预期的行为: jupyter notebook在控制台中生成jupyter: command not found错误。

My question is: am I using pipenv in a way that was not intended, or is there an alternative way to use pipenv that avoids this problem? 我的问题是:我是否以某种无意的方式使用pipenv,或者是否有其他方法可以使用pipenv来避免这个问题?

Thanks in advance! 提前致谢!

The Problem 问题

In step 1 you install pipenv with brew. 在步骤1中,您使用brew安装pipenv。 The brew installation of pipenv also installs a python environment alongside it which is directly used. pipenv的brew安装pipenv还在其旁边安装了一个直接使用的python环境。 The command itself is on your path, so you can invoke it independent of the virtualenv you have loaded. 命令本身位于您的路径上,因此您可以独立于已加载的virtualenv调用它。

In step 2 you install and load a pyenv virtualenv. 在第2步中,您将安装并加载pyenv virtualenv。 Loading the pyenv virtualenv changes your path. 加载pyenv virtualenv会改变你的路径。 When you use pip now, it is the pyenv 3.6.3 pip and it will install into that pyenv. 当你现在使用pip时,它是pyenv 3.6.3点,它将安装到那个pyenv中。 So when, in step 3, you run pip install jupyter requests they go into the pyenv virtualenv. 因此,当您在第3步中运行pip install jupyter requests他们会进入pyenv virtualenv。

When you run pipenv , in step 4, it will use it's own python, so it is able to load itself. 当您运行pipenv ,在步骤4中,它将使用它自己的python,因此它可以加载自己。 Your load of the pipenv virtualenv in step 5 then adds the bin folder for that to the your path, allowing you to run things you install. 您在步骤5中加载pipenv virtualenv然后将bin文件夹添加到您的路径,允许您运行您安装的内容。 It does not undo the changes pyenv made. 它不会撤消pyenv所做的更改。 When you run pip in step 6 it runs inside this pipenv virtualenv and so prints the limited list of installed packages. 当您在步骤6中运行pip ,它会在此pipenv virtualenv中运行,因此会打印有限的已安装软件包列表。

In step 7 you run jupyter . 在第7步中,您运行jupyter This is not inside the pipenv virtualenv, however it is inside the pyenv one. 这不在pipenv virtualenv中,但它在pyenv内部。 The pyenv path additions still exist so it is runnable. pyenv路径添加仍然存在,因此它是可运行的。 The jupyter command is a bash script and it does magic to reset the python path before running. jupyter命令是一个bash脚本,它可以在运行之前重置python路径。 That is how it can find the requests module in step 8. 这就是它在步骤8中找到requests模块的方式。

You finally run some form of python repl. 你最终运行某种形式的python repl。 This presumably is in your pipenv virtualenv so it is loads modules which are in that environment. 这可能是你的pipenv virtualenv所以它是在那个环境中加载模块。

The Solution 解决方案

You need to unload the global pyenv. 你需要卸载全局pyenv。 Since you have installed packages into your 3.6.3 python install I would also clear any virtualenvs and python versions you have installed. 既然你已经在你的3.6.3 python安装中安装了软件包,我也会清除你安装的所有virtualenvs和python版本。 You can just reinstall them. 你可以重新安装它们。

Your install of pipenv is fine, that can stay. 你安装的pipenv很好,可以留下来。 It is also the primary tool that you should be using. 它也是您应该使用的主要工具。 You should not need to execute pip or pyenv directly. 您不需要直接执行pippyenv

You should install jupyter into the pipenv virtualenv and you should then have an install which only works when the pipenv virtualenv is loaded. 您应该将jupyter安装到pipenv virtualenv中,然后您应该安装一个仅在加载pipenv virtualenv时才能运行的安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM