如何在Ubuntu 20.04上设置Python虚拟环境
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:
要列出所有可用的虚拟环境,请在终端中使用以下命令: workon或lsvirtualenv (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/
相关文章:

getURLParameters - 网址参数
返回包含当前URL参数的对象。 通过适当的正则表达式,使用 String.match() 来获得所有的键值对, Array.reduce() 来映射和组合成一个单一的对象。 将 location.search 作为参数传递给当前 url。 const getURLParameters url >url.match(/([^?&])…

block的使用
#import "ViewController.h" /* 使用Block最大的一个好处就是可以在代码块中随时访问外部变量 比如你在A.class类中的某个方法中声明了一段代码块.你可以在代码块中直接对A.class所拥有的成员变量进行调用,并且,通过一定的条件(__block),还可以随时的修改这…

关于二级菜单的问题
大家在做二级菜单的时候经常会碰到鼠标移出一级菜单,二级菜单瞬间消失,根本不给你机会移到二级菜单上,今天分享下怎样解决这个问题。 ①第一种介绍一种简单粗暴的方法,二级菜单的元素放入一级菜单中。 代码地址:http:h…

python打印换行符_Python换行符以及如何在不使用换行符的情况下进行Python打印
python打印换行符Welcome! The new line character in Python is used to mark the end of a line and the beginning of a new line. Knowing how to use it is essential if you want to print output to the console and work with files.欢迎! Python中的新行字…

tomcat启动后 项目运行缓慢,要几十到几百秒不等 怎么样./startup.sh 运行加快
修改 linux系统中 /usr/local/jdk1.8.0_11/jre/lib/security/java.security 借力 好文章。我们新的Linux系统,部署了多个 Tomca,同时重启后t, 每次都阻塞差不多260秒左右。修改之后总的启动时间下降到6-8秒左右。另外,不确定为什么࿰…

转载 C#中使用结构来传递多个参数
C#中当参数超过5个时,建议用结构来传递多个参数。 示例代码如下: 1 public struct MyStruct2 {3 public string str;4 public int number;5 }6 7 class Program8 {9 static void Main(string[] args) 10 { 11 MyStruct myStruct…

xmpp 开源项目选择_如何选择和维护安全的开源项目
xmpp 开源项目选择评估开源项目安全性的一些技巧。 (A few tricks for assessing the security of an open source project.) There is a rather progressive sect of the software development world called the open source community. 在软件开发领域,有一个相当…

【2018-01-22】HTML-表单及表单元素
<body><!--表单--><form action"" method"post"><!--文本类--><input type"text" value"" placeholder"请输入用户名"/><!--文本框--><input type"password" value"…

取消tableView多余的横线
- (void)setExtraCellLineHidden: (UITableView *)tableView{UIView *view [UIView new];view.backgroundColor [UIColor clearColor];[tableView setTableFooterView:view];}

iOS 自定义UITabBar
推荐一篇非常好的集成各种UITabBar的三方库 《点击这里直取demo》 另外一篇根据runtime定制了一款可以出轨的UITarBar 《Runtime实战之定制TabBarItem大小》 点击view的触发机制《iOS事件分发机制(一) hit-Testing》 摘自: UIView中提供两个…

react入门代码_如何在React中构建温度控制应用程序-包括提示和入门代码
react入门代码我们正在建立的 (What were building) In this beginner React project, were going to learn how to use state hooks, handle events, apply CSS based on state, and more! Check it out: 在这个初学者的React项目中,我们将学习如何使用状态挂钩&am…

决策树(chap3)Machine Learning In Action学习笔记
优点:计算复杂度不高,输出结果易于理解,对中间值的缺失不敏感,可以处理不相关特征数据。缺点:可能会产生过度匹配问题。适用数据类型:数值型(必须离散化)和标称型。决策树创建分支的…

BigdCIMAL类型数据的使用选择
现在常用的数值类型有Integer , Double , Float , BigDecimal几种 , 常用的当然要数前两种 了 , Integer代表的是整数类型的数据 , double则是代表的是浮点型 , 双精度 ,double的计算精度相对于float来讲要 高 , BigDecimal的计算精度则是最高的 . 可是BigDecimal的一些计算方…

数字字符串转化为时间字符串
(NSString *)dateStringFromNumberTimer:(NSString *)timerStr {//转化为Doubledouble t [timerStr doubleValue];//计算出距离1970的NSDateNSDate *date [NSDate dateWithTimeIntervalSince1970:t];//转化为 时间格式化字符串NSDateFormatter *df [[NSDateFormatter alloc]…

git 覆盖本地修改_Git拉力–如何使用Git覆盖本地更改
git 覆盖本地修改When you learn to code, sooner or later youll also learn about Version Control Systems. And while there are many competing tools in this space, one of them is the de facto standard used by almost everyone in the industry. Its so popular tha…

云计算大会记录
一、要点及主要技术内容记录消费金融刘志军 马上消费大额 分散 小额 短期OpenStack OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目。 OpenStack是一个开源的云计算管…

获取iOS版本号
(double)getCurrentIOS {return [[[UIDevice currentDevice] systemVersion] doubleValue];}

spring boot 服务 正确关闭方式
引言 Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行、产品级别的基于Spring框架的应用,大部分Spring Boot应用只需要非常少的配置就可以快速运行…

如何在5美元的Raspberry Pi上构建个人开发服务器
In this article, youll learn how to build a personal dev server by installing Git, Node.js, Rust, and Docker on a Raspberry Pi. The cheapest option costs just $5. You can get a starter kit ($25) for free here.在本文中,您将学习如何通过在Raspberry…

Eclipse:xml文件中添加.xsd约束文件
今天在使用dubbo的时候,XML文件一直报错。找不到dubbo的xsd约束文件。 cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element dubbo:reference 解决方法: 找到dubbo的jar包,然后在META-…

029 浏览器不能访问虚拟机的问题解决
1.在搭建分布式时 ssh一直不能进行scp,后来发现一个问题。 windows中的hosts配置了三台虚拟机的映射,但是在虚拟机中的hosts没有配置。 做法是在每台虚拟机上都配置三台虚拟机的映射。 2.端口访问与防火墙 最近帮别人解决问题时才注意的。 以前安装好虚拟…

获取 一个文件 在沙盒Library/Caches/ 目录下的路径
(NSString *)getFullPathWithFile:(NSString *)urlName {//先获取 沙盒中的Library/Caches/路径NSString *docPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString *myCacheDirectory [docPath stringByAppendingPat…

如何有效使用每一点脑力总结_如何更有效地节省脑力和编码
如何有效使用每一点脑力总结如果您知道这些工具的存在,那么您现在可能会使用它们。 (If you knew these tools existed, youd probably be using them by now.) This article isn’t going to tell you about saving your neck with a Roost stand, or your wrists …

C语言程序设计50例(一)(经典收藏)
【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。 1 #include…

python多线程执行类中的静态方法
在python 中如果通过多线程的方式执行某个方法很简单,只需要把同步函数的第一个参数为该函数对象即可。但是如果函数对象是某个类的静态方法,这时候如果直接使用类的该函数对象会报错。此时需要构造一个代理的方法来实现。 如:上一个博文中的…

检测缓存文件是否超时
(BOOL)isTimeOutWithFile:(NSString *)filePath timeOut:(double)timeOut {//获取文件的属性NSDictionary *fileDict [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];//获取文件的上次的修改时间NSDate *lastModfyDate fileDict.fileModificat…

JavaScript创建对象–如何在JS中定义对象
Objects are the main unit of encapsulation in Object-Oriented Programming. In this article, I will describe several ways to build objects in JavaScript. They are:对象是面向对象编程中封装的主要单元。 在本文中,我将介绍几种使用JavaScript构建对象的方…

MyBatis中#{}和${}的区别
------------------------siwuxie095 MyBatis 中 #{} 和 ${} 的区别 1、在 MyBatis 的映射配置文件中,动态传递参数有两种方式: (1)#{} 占位符 (2)${} 拼接符 2、#{} 和 ${} 的区别 (1ÿ…

十进制字符串转十六进制字符串
NSString *colorStr[self.model.sclass_color substringFromIndex:1]; unsigned long cor strtoul([colorStr UTF8String],0,16);

gi克隆github文件_如何构建GitHub文件搜索功能的克隆
gi克隆github文件In this article, we will build a project that mimics the lesser known but awesome file search functionality provided by GitHub.在本文中,我们将构建一个项目,该项目模仿GitHub提供的鲜为人知但功能强大的文件搜索功能。 To se…