虚拟机使用技巧_正确使用虚拟环境的建议和技巧

虚拟机使用技巧

In early 2020, I was working on a school project using Keras/Tensorflow. Everything was working great. Then, I shared my code with my teammate so he could start working on it too. However, for some reason, it was not working for him.

在2020年初,我正在使用Keras / Tensorflow进行学校项目。 一切都很好。 然后,我与队友共享了我的代码,以便他也可以开始进行开发。 但是,由于某种原因,它对他不起作用。

We spent hours trying to figure it out. Eventually, we realized that the problem was caused by the fact we had different packages versions installed. Once we managed to make it all work, I decided to find a way to avoid this problem occuring ever again.

我们花了几个小时试图弄清楚。 最终,我们意识到问题是由我们安装了不同的软件包版本引起的。 一旦我们设法使其全部工作,我决定找到一种方法来避免再次发生此问题。

That’s where virtual environments come in.

那就是虚拟环境出现的地方。

If you are already familiar with virtual environments, you can skip to section 4 where I will give some advice and tricks to help you improve and customize your use of virtual environments.

如果您已经熟悉虚拟环境,则可以跳到第4节,在此我将提供一些建议和技巧,以帮助您改善和自定义虚拟环境的使用。

1.为什么要使用虚拟环境? (1. Why use Virtual environments?)

As you get better with Python, you will be using more and more packages and it will get more and more difficult to properly deal with dependencies on earlier versions of packages, hence the need for virtual environments.

随着您对Python的了解越来越好,您将使用越来越多的软件包,并且要正确处理对早期版本软件包的依赖性也变得越来越困难,因此需要虚拟环境。

Think of a virtual environment as a separate folder where you can keep specific versions of packages or even a specific Python version without affecting the rest of your projects.

将虚拟环境视为一个单独的文件夹,您可以在其中保留特定版本的软件包,甚至特定的Python版本,而不会影响其余项目。

It’s an easy way to ensure you (and everybody else) can always run your Python scripts with no dependency issues, i.e. always using the correct versions for your packages.

这是确保您(和其他所有人)始终可以运行Python脚本而没有依赖关系问题的简便方法,即始终为软件包使用正确的版本。

All codes coming up can be executed directly from the Anaconda prompt or the command line.

可以直接从Anaconda提示符或命令行执行所有出现的代码。

2.如何创建虚拟环境 (2. How to create a virtual environment)

It’s super easy. First, make sure you install virtualenv, the library that creates virtual environments: pip install virtualenv .

超级容易。 首先,确保安装了virtualenv,该库可创建虚拟环境: pip install virtualenv

Then, the following lines of code will create a new folder for your project and create/activate a virtual environment inside of it.

然后,以下代码行将为您的项目创建一个新文件夹,并在其中创建/激活虚拟环境。

> mkdir new_folder        #create new folder for project
> cd new_folder #set current directory to folder> virtualenv venv #Create a virtual environment called venv
> cd venv/Scripts #Set working directory to venv
> activate #Activate the environment

From there, you could install all the packages you need with their specific version: pip install beautifulsoup4==4.9.1 .

从那里,您可以安装所有需要的特定版本的软件包: pip install beautifulsoup4==4.9.1

There is however a better to do this.

但是,这样做更好。

3.使用requirements.txt文件 (3. Working with a requirements.txt file)

By creating a requirements file, you can specify which packages and which versions you want to use in your virtual environment. Here is an example of what it should look like:

通过创建需求文件,您可以指定要在虚拟环境中使用的软件包和版本。 这是它应显示的示例:

Image for post

Then, you could install all the packages mentionned in the file using the following line of code, after making sure the file is situated in the current working directory:

然后,在确保文件位于当前工作目录中之后,可以使用以下代码行安装文件中提到的所有软件包:

> pip install -r requirements.txt

That works great but there is an even better way to do this, which I will cover in the following section.

效果很好,但是有一种更好的方法可以做到这一点,我将在下一节中介绍。

4.其他有用的技巧和窍门 (4. Other useful tips and tricks)

Here are a few tips and tricks that I believe will facilitate and even improve your use of virtual environments.

这里有一些技巧和窍门,我相信它们会促进甚至改善您对虚拟环境的使用。

  1. Create a requirements.txt file automatically

    自动创建requirements.txt文件

Instead of having to manually create the requirements file, what you could (and should) do is place your Python script inside the virtual environment and write the following line of code:

您不必(并且应该)要做的是将Python脚本放在虚拟环境中并编写以下代码,而不必手动创建需求文件:

> pip freeze > requirements.txt

This will create it for you automatically.

这将自动为您创建。

Another option would also be to use the pipregs library. All you have to do is make the location of your Python script the current directory and then type pipregs.

另一种选择是使用pipregs库。 您所要做的就是将Python脚本的位置设置为当前目录,然后键入pipregs

> pip install pipreqs
> pipreqs

This will find your Python scripts, detect all the packages (and versions) being used and create a requirements.txt file from it.

这将找到您的Python脚本,检测所有正在使用的软件包(和版本),并从中创建requirements.txt文件。

2. Create virtual environment with a specific Python version

2.使用特定的Python版本创建虚拟环境

If you want your script to be run with a specific Python version, it can be done quite easily. First, make sure you download separetely the version you want to use.

如果您希望脚本使用特定的Python版本运行,则可以很容易地完成。 首先,请确保单独下载要使用的版本。

Then, when creating the virtual environment, change the virtualenv venv bit of code to this (if you want to use Python 3.7 for instance):

然后,在创建虚拟环境时,将virtualenv venv代码更改为此(例如,如果要使用Python 3.7):

> virtualenv venv --python=python3.7

3. Do NOT put your Python script inside the virtual environment

3.请勿将Python脚本放入虚拟环境中

The easiest way to delete your virtual environment is to simply manually delete it’s folder. If you insert your python script and requirements file inside, they will be deleted with it, which might not be what you want to do.

删除虚拟环境的最简单方法是简单地手动删除其文件夹。 如果您将python脚本和需求文件插入其中,它们将随其一起删除,这可能不是您想要的。

You could also want to use the same virtual environment for multiple projects. Leaving everything separate avoids problems.

您可能还希望对多个项目使用相同的虚拟环境。 将所有内容分开可以避免问题。

The best thing to do is to leave those files outside the virtual environment, like below:

最好的办法是将这些文件保留在虚拟环境之外,如下所示:

Image for post

4. Run Python code directly from your virtual environment

4.直接从您的虚拟环境中运行Python代码

If you want to run your code directly from your virtual environment, you simply have to add python in front of the name of your file, like this:

如果要直接从虚拟环境中运行代码,只需在文件名前面添加python ,如下所示:

> python flights_scraping.py

I hope this helped. Thanks a lot for reading!

希望对您有所帮助。 非常感谢您的阅读!

翻译自: https://towardsdatascience.com/advice-and-tips-to-properly-work-with-virtual-environments-67bbad9ba5b6

虚拟机使用技巧

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值