简体   繁体   English

Python 虚拟环境混淆

[英]Python Virtual Environments Confusion

I have been learning data science using python for about a year now.我已经使用 python 学习数据科学大约一年了。 I have become quite comfortable with the syntax and model creation.我已经对语法和模型创建非常满意。 I have exclusively used Google Colab just due to how convenient it is and I love the notebook style.我专门使用 Google Colab 只是因为它非常方便,而且我喜欢笔记本风格。 However, one thing I do not understand is the environment stuff.但是,我不明白的一件事是环境的东西。 Although I use Colab, I do have python and anaconda on my machine and have installed various packages using the exact following format: pip install (package name) .虽然我使用 Colab,但我的机器上确实有 python 和 anaconda,并使用以下格式安装了各种软件包: pip install (package name) When I open my terminal, the first line is lead with (base) and when I check the Environments tab in anaconda navigator, it appears as though I installed all of these packages into a base environment named base (root) ?当我打开终端时,第一行以(base)开头,当我检查 anaconda 导航器中的Environments选项卡时,似乎我将所有这些软件包安装到名为base (root)的基本环境中? Is that right?那正确吗? If so, what would my environment's name be then?如果是这样,那么我的环境名称是什么? What is a base environment compared to a venv?与 venv 相比,什么是基础环境?

The reason I am asking is because if I ever decide to use an IDE in the future, I would need to set my environment to be able to run packages, correct?我问的原因是因为如果我将来决定使用 IDE,我需要设置我的环境才能运行包,对吗?

Just for fun I want to try using R and its reticulate package that allows python use in R. As stated in the answer to this question , I need to set my virtual environment before I can use python in R. Would my virtual environment be base (root) ?只是为了好玩,我想尝试使用 R 及其允许在 R 中使用 python 的网状包。正如这个问题的答案中所述,我需要先设置我的虚拟环境,然后才能在 R 中使用 python。我的虚拟环境是base (root)吗? base (root)

I'm a complete noob about all of this environment stuff.我对所有这些环境的东西都是一个完全的菜鸟。 Again, I just opened my terminal and typed pip install (package name) for all packages I've installed.同样,我刚刚打开我的终端并为我安装的所有软件包输入pip install (package name) Thanks for any help in advance.提前感谢您的任何帮助。

So from your description, it sounds like your default Python installation on your computer is through Anaconda.因此,从您的描述来看,您计算机上的默认 Python 安装似乎是通过 Anaconda 安装的。 If that's the case, base is actually going to be the name of the conda virtual environment that you're using.如果是这种情况, base实际上将是您正在使用的 conda 虚拟环境的名称。

Virtual environments can be tricky, so I'll walk you through what I usually do here.虚拟环境可能很棘手,因此我将向您介绍我通常在此处执行的操作。

First, you can always check which Python installation you're currently using by using the which command on Mac/Linux, or if you're using Windows the command will probably be where (if you're on Windows, this answer might be helpful: equivalent of 'which' in Windows .)首先,您始终可以通过在 Mac/Linux 上使用which命令来检查您当前使用的是哪个 Python 安装,或者如果您使用的是 Windows,则该命令可能位于where (如果您使用的是 Windows,此答案可能会有所帮助: 相当于 Windows 中的“which” 。)

(base) ➜  ~ which python
/Users/steven/miniconda3/bin/python

From the above, you can see that my default Python is through Miniconda, which is just a small version of Anaconda.从上面可以看出,我默认的Python是通过Miniconda的,它只是Anaconda的一个小版本。

This means that when you use pip to install packages, those are getting installed into this base conda environment.这意味着当您使用pip安装软件包时,这些软件包将安装到此base conda 环境中。 And, by the way, you can use the which command with pip as well, just to double-check that you're using the version of pip that's in your current environment:而且,顺便说一句,您也可以将which命令与 pip 一起使用,只是为了仔细检查您正在使用当前环境中的pip版本:

(base) ➜  ~ which pip
/Users/steven/miniconda3/bin/pip

If you want to see the list of packages currently installed, you can do pip freeze , or conda env export .如果要查看当前安装的软件包列表,可以执行pip freezeconda env export Both pip and conda are package managers, and if you're using an Anaconda Python installation then you can (generally) use either to install packages into your virtual environment. pipconda都是包管理器,如果您使用的是 Anaconda Python 安装,那么您(通常)可以使用任一方式将包安装到您的虚拟环境中。

(Quick side note: "virtual environments" are a general concept that can be implemented in different ways. Both conda and virtualenv are ways to use virtual environments in Python. I'm also a data scientist, and I use conda for all of my virtual environments.) (快速旁注:“虚拟环境”是一个可以以不同方式实现的一般概念condavirtualenv都是在 Python 中使用虚拟环境的方法。我也是一名数据科学家,我使用conda虚拟环境。)

If you want to create a new virtual environment using conda, it's very straightforward.如果您想使用 conda 创建一个新的虚拟环境,这非常简单。 First, you can create the environment and install some packages right away, like pandas and matplotlib.首先,您可以创建环境并立即安装一些软件包,例如 pandas 和 matplotlib。 Then you can activate that environment, check your version of python, and then deactivate it.然后您可以激活该环境,检查您的 python 版本,然后停用它。

(base) ➜  ~ conda create -n my-new-environment pandas matplotlib
(base) ➜  ~ which python
/Users/steven/miniconda3/bin/python
(base) ➜  ~ conda activate my-new-environment
(my-new-environment) ➜  ~ which python
/Users/steven/miniconda3/envs/my-new-environment/bin/python
(my-new-environment) ➜  ~ conda deactivate
(base) ➜  ~ which python
/Users/steven/miniconda3/bin/python

And, if you want to see which conda virtual environments you currently have available, you can run conda env list .而且,如果您想查看当前可用的 conda 虚拟环境,可以运行conda env list

Here's the documentation for conda environments, which I reference all the time: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html这是 conda 环境的文档,我一直在参考: https : //docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html

I hope this is helpful!我希望这是有帮助的!

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

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