如何在Ubuntu 20.04上设置Python虚拟环境

本文介绍了如何在Ubuntu 20.04上利用pip3和virtualenvwrapper轻松创建和管理Python 3虚拟环境。首先确保系统已安装Python 3和pip3,然后安装virtualenvwrapper。接着,编辑.bashrc文件以配置虚拟环境使用Python 3,并创建一个用于存放虚拟环境的隐藏目录。最后,通过工作在终端中执行相关命令即可创建、激活、停用和管理虚拟环境。
摘要由CSDN通过智能技术生成

I recently got myself a “new” laptop – a Lenovo x270 (yay)! And once again I needed to set up a Python virtual environment. So of course I Googled for a solution, just to find my previously written article on the same topic!

我最近给自己买了一台“新”笔记本电脑-联想x270(是)! 再一次,我需要设置一个Python虚拟环境。 所以,当然,我用Google搜索了一个解决方案,只是找到了我以前写的关于同一主题的文章

So in this article, I'll update the instructions based on my newly acquired knowledge.

因此,在本文中,我将基于我新获得的知识来更新说明。

And let me tell you, it’s easier than before because we are going to do only two things:

让我告诉你,这比以前容易,因为我们只要做两件事:

  • Install virtualenvwrapper

    安装virtualenvwrapper
  • Edit the .bashrc file

    编辑.bashrc文件

先决条件 (Prerequisites)

In this article I will show you how to set up virtualenvwrapper with pip3 (pip for Python 3). We are not going to use Python 2 because it's no longer supported.

在本文中,我将向您展示如何使用pip3(适用于Python 3的pip)设置virtualenvwrapper。 我们将不再使用Python 2,因为它不再受支持

To complete this tutorial, you will need a computer with Ubuntu 20.04 installed and an internet connection. Also, some knowledge of the terminal and Vim editor would be useful.

要完成本教程,您将需要一台安装了Ubuntu 20.04并具有Internet连接的计算机。 另外,有关终端和Vim编辑器的一些知识将很有用。

设置虚拟环境 (Setting up a Virtual Environment)

Now open your terminal in the home directory by right clicking and choosing the option “Open in Terminal”. You can also press the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.

现在,通过右键单击并选择“在终端中打开”选项,在主目录中打开终端。 您也可以同时按键盘上的CTRL,ALT和T键以自动打开“终端”应用程序。

You first need to create a special directory that will hold all of your virtual environments. So go ahead and create a new hidden directory called virtualenv:

首先,您需要创建一个包含所有虚拟环境的特殊目录。 因此,继续创建一个名为virtualenv的新隐藏目录:

mkdir .virtualenv

点3 (pip3)

Now you should install pip for Python3:

现在您应该为Python3安装pip:

sudo apt install python3-pip

Confirm the pip3 installation:

确认pip3安装:

pip3 -V

虚拟环境包装器 (virtualenvwrapper)

virtualenvwrapper is a set of extensions for virtualenv. It provides commands like mkvirtualenv, lssitepackages, and especially workon for switching between different virtualenv environments.

virtualenvwrapper是virtualenv的一组扩展。 它提供了mkvirtualenv,lssitepackages等命令,尤其是在不同的virtualenv环境之间切换的workon。

Install virtualenvwrapper via pip3:

通过pip3安装virtualenvwrapper:

pip3 install virtualenvwrapper

bashrc文件 (bashrc file)

We are going to modify your .bashrc file by adding a row that will adjust every new virtual environment to use Python 3. We will point virtual environments to the directory we created above (.virtualenv) and we will also point to the locations of the virtualenv and virtualenvwrapper.

我们将通过添加一行来调整每个新的虚拟环境以使用Python 3来修改.bashrc文件。我们将虚拟环境指向我们在上面创建的目录(.virtualenv),并且还将指向该目录。 virtualenv和virtualenvwrapper。

Now open the .bashrc file using the Vim editor:

现在,使用Vim编辑器打开.bashrc文件:

vim .bashrc

If you still haven’t used Vim before or you don’t have it installed on your computer, you should install it now. It is one of the most widely used Linux editors and for good reason.

如果您以前从未使用过Vim,或者未在计算机上安装过Vim,则应立即安装。 它是使用最广泛的Linux编辑器之一,这有充分的理由。

sudo apt install vim

After you've installed Vim open the .bashrc file by typing in the vim .bashrc command in your terminal. Navigate to the bottom of the .bashrc file, press the letter i to enter insert mode in Vim, and add these rows:

安装Vim后,通过输入vim .bashrc打开.bashrc文件。 终端中的命令。 导航到.bashrc文件的底部,按字母i进入Vim中的插入模式,然后添加以下行:

#Virtualenvwrapper settings:
export WORKON_HOME=$HOME/.virtualenvs
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
. /usr/local/bin/virtualenvwrapper.sh

After you are done, press the esc key, then type :wq and press enter. This command will save the file and exit Vim.

完成后,按Esc键,然后输入wq并按Enter。 该命令将保存文件并退出Vim。

Now you need to reload the bashrc script. There are two ways to do it – close and reopen your terminal, or execute this command in the terminal:

现在,您需要重新加载bashrc脚本。 有两种方法可以执行此操作–关闭并重新打开终端,或者在终端中执行以下命令:

source ~/.bashrc

To create a virtual environment in Python3 and activate it immediately use this command in your terminal:

要在Python3中创建虚拟环境并立即将其激活,请在终端中使用以下命令:

mkvirtualenv name_of_your_env

To deactivate the environment use the deactivate command.

要停用环境,请使用deactivate命令。

To list all available virtual environments use the command workon or lsvirtualenv (lsvirtualenv will show the same result as workon but in a fancier way) in your terminal:

要列出所有可用的虚拟环境,请在终端中使用以下命令: workonlsvirtualenv (lsvirtualenv将显示与workon相同的结果,但显示方式更为简洁):

workon
lsvirtualenv

To activate one specific environment use workon + name of your environment:

要激活一个特定的环境,请使用workon +您的环境名称:

workon name_of_your_env

There are several useful command you might need to use someday:

有一天您可能需要使用几个有用的命令:

Rmvirtualenv will remove a specific virtual environment located in your .virtualenv directory.

Rmvirtualenv将删除.virtualenv目录中的特定虚拟环境。

rmvirtualenv name_of_your_env

Cpvirtualenv will copy the existing virtual environment to a new virtual environment and activate it.

Cpvirtualenv会将现有的虚拟环境复制到新的虚拟环境并激活它。

cpvirtualenv old_virtual_env new_virtual_env

Well done! You have now created your first isolated Python 3 environment.

做得好! 现在,您已经创建了第一个隔离的Python 3环境。

Thank you for reading!

感谢您的阅读!

Check out more articles like this on my freeCodeCamp profile, Medium profile, and other fun stuff I build on my GitHub page.

在我的freeCodeCamp配置文件中型配置文件以及我在GitHub页面上构建的其他有趣的东西查看更多类似的文章。

翻译自: https://www.freecodecamp.org/news/how-to-set-up-python-virtual-environment-on-ubuntu-20-04/

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值