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/

如何在Ubuntu 20.04上设置Python虚拟环境相关推荐

  1. linux 修改时区_如何在 Ubuntu 20.04 上设置或者修改时区

    本文最先发布在: 如何在 Ubuntu 20.04 上设置或者修改时区​www.itcoder.tech 使用正确的时区,对于系统相关的任务和进程来说,是最基本的.例如,cron 守护进程,使用系统时 ...

  2. ubuntu安装python百度经验_如何在Ubuntu 20.04上安装Python 3.9(含python编译安装和使用Apt命令安装)...

    在本文中,我们将向您展示在Ubuntu 20.04上安装Python 3.9的两种方法.第一种使用APT命令安装Python3.9,第二种是在Ubuntu20.04上编译安装Python 3.9.本教 ...

  3. 如何在Ubuntu 20.04上设置Mattermost

    介绍 (Introduction) Mattermost is an open source collaboration and messaging platform created with sec ...

  4. linux桌面时区设置,如何在Ubuntu 20.04上设置或更改时区

    对于许多与系统相关的任务和进程,使用正确的时区至关重要. 例如,cron守护程序使用系统的时区执行cron作业,而日志文件中的时间戳基于系统的同一时区. 在Ubuntu上,系统的时区是在安装过程中设置 ...

  5. 如何在Ubuntu 20.04上设置和配置证书颁发机构(CA)

    介绍 (Introduction) A Certificate Authority (CA) is an entity responsible for issuing digital certific ...

  6. ubuntu使用fail2ban_如何在Ubuntu 20.04上安装和配置Fail2ban

    暴露给Internet的任何服务都有遭受恶意软件攻击的风险. 例如,如果您在可公开访问的网络上运行服务,则攻击者可以使用暴力手段尝试登录您的帐户. Fail2ban是一种工具,可通过监视服务日志中的恶 ...

  7. ubuntu19 安装git_如何在Ubuntu 20.04上安装Git

    Git是世界上最受欢迎的分布式版本控制系统,被许多开源和商业项目使用.它使您可以与其他开发人员在项目上进行协作,跟踪代码更改,还原到上一阶段,创建分支等. Git最初是由Linux内核的创建者Linu ...

  8. go 写文件_如何在 Ubuntu 20.04 上安装 Go

    本文最先发布在: 如何在 Ubuntu 20.04 上安装 Go​www.itcoder.tech Go,通常被称为 golang,它是一门由 Google 创建的现代化的开源编程语言,它允许你构建实 ...

  9. 如何在Ubuntu 20.04 上安装 Xrdp 服务器(远程桌面)

    本文最先发布在: https://www.itcoder.tech/posts/how-to-install-xrdp-on-ubuntu-20-04/ Xrdp 是一个微软远程桌面协议(RDP)的开 ...

最新文章

  1. 什么时候使用临时表?
  2. 注解@Slf4j的使用
  3. Springmvc文件上传(servlet3.0)/下载(ssm)以及坑点
  4. c 语言 指针 指向数组,C 指向数组的指针
  5. 最新Angular2案例rebirth开源
  6. 使用python 下载_使用python下载大量文件
  7. 工业以太网交换机的接口知识详解
  8. 大疆地理围栏系统预防无人机闯入机场
  9. python vs golang_Ruby vs Golang:四个维度对比,谁更胜一筹?
  10. svn增量打包部署_实现Jenkins+svn+bat批处理构建svn版本差异增量的自动化打zip包
  11. Java后台生成小程序二维码
  12. python正则表达式 身份证_正则表达式实现身份证信息验证
  13. 使用Python写一个定时锁屏软件
  14. 房友中介管理系统服务器地址查询,房友中介连接服务器设置
  15. python-numpy常用知识汇总
  16. WEB交互设计方法中”页面表达原则”
  17. 求两个数的最大公约数(C++)
  18. 自动打卡php,使用腾讯云实现网易云自动打卡签到 | 小七呀w
  19. 在IE地址栏显示自己的小图标
  20. 赵小楼《天道》《遥远的救世主》深度解析(49)丁元英的“不执著出人头地”和主流价值观的“执著出人头地”

热门文章

  1. 【css】如何使页面压缩时文本内容不换行
  2. AttributeError: Cant get attribute SPPF on module models
  3. JavaScript中substr()和substring的区别
  4. 在线考试系统html模板,请问谁有在线考试系统的网页模板?
  5. python数据库建表_mysql数据表如何创建
  6. HTML超出部分滚动效果 HTML滚动 HTML下拉 附效果图
  7. 聚焦OA品牌:OA产品影响力是选型关键
  8. Wink发布Wink Hub2家庭物联网控制中心
  9. Linux下des对称性加密
  10. 开源监控解决方案Nagios+Cacti+PNP4Nagios+NConf+NDOUtils+Nagvis(六)ndoutils安装