程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python virtual environment practical guide

編輯:Python

The project we are working on is likely to have many dependencies that need to be installed . These dependencies facilitate many tasks in the project . However, especially when dealing with multiple projects , We need to be very careful .

Like any other technology , Software packages or programming languages are also improving . Therefore, a new version is being launched .

Different projects may require different versions of packages or software . for example , We may have a project that needs Python2.7, Another project requires Python3.6. As the number of projects and dependencies increases , It is difficult to track and deal with these differences .

One way to overcome this problem is to use virtual environments . They can be regarded as the bounding box of the software package . We can develop a project in a virtual environment , And install all dependencies specific to the project . What we have in the virtual environment is not affected by the global changes of the machine .

Python There are many virtual environment tools , Such as pipenv、virtualenv、venv etc. . In this paper , We will discuss some uses virtualenv and pipenv An example of , To be familiar with the concept of virtual environment and its working mode .

Let's start with virtualenv Start . use first python Package installer (pip) Install it from the terminal .

$ pip install virtualenv

Create a sample project file as the working directory .

$ mkdir demoproject
$ cd demoproject

Now in demoproject Directory . We will use the following command to create a virtual environment .

$ virtualenv venv_demo

It was created . We can run ls Command to view the files in the current working directory .

$ ls
venv_demo

The next step is to activate the virtual environment .

$ source venv_demo/bin/activate

Once the virtual environment is activated , Its name will be displayed in the terminal , As shown below :


Now you can install the package .

$ python -m pip install pandas

We have now installed pandas.freeze The command displays a list of installed packages .

$ python -m pip freeze
numpy==1.19.4
pandas==1.1.5
python-dateutil==2.8.1
pytz==2020.5
six==1.15.0

NumPy Also installed , Because it is pandas Dependence .pandas The installed version of is 1.1.5. We can specify the required version when installing the software package .

$ python -m pip install pandas==1.0.5

If you only want to check the installed version of a specific package , Please put freeze Command and grep Use it together :

$ pip freeze | grep pandas
pandas==1.0.5

We can also install several software packages saved in text files . This is better than installing dependencies one by one , Especially when there are multiple dependencies .

$ python -m pip install -r requirements.txt

In order to exit the virtual environment , We use deactivate command .

$ deactivate

The next tool we will find is pipenv, It can be used pip install :

$ pip install pipenv

Use pipenv Create a new virtual environment .

$ pipenv install --python=/usr/bin/python3.6

Pipenv Allow dependencies to be installed when creating virtual environments . for example , I can add pandas, Then you can create the installation pandas Virtual environment for .

function shell Command to activate the virtual environment .

$ pipenv shell


We are now in a virtual environment . Also install this pandas Well .

$ pipenv install pandas

graph The command displays a detailed overview of the installed packages .

$ pipenv graph
pandas==1.1.5
- numpy [required: >=1.15.4, installed: 1.19.4]
- python-dateutil [required: >=2.7.3, installed: 2.8.1]
- six [required: >=1.5, installed: 1.15.0]
- pytz [required: >=2017.2, installed: 2020.5]

We can use uninstall Command to uninstall a specific package or all packages in the virtual environment .

$ pipenv uninstall pandas

The following command will uninstall all packages .

$ pipenv uninstall -all

type “exit” Command to exit the virtual environment .

Conclusion

Virtual environment is a good tool for managing multiple projects at the same time . There are many software packages and libraries that can be updated quickly . therefore , Trying to update manually is inefficient .

About Python Technology reserve

Learn from good examples Python Whether it's employment or sideline, it's good to make money , But learn to Python Still have a learning plan . Finally, let's share a complete set of Python Learning materials , For those who want to learn Python Let's have a little help !

One 、Python Learning routes in all directions

Python The technical points in all directions are sorted out , Form a summary of knowledge points in various fields , The use of it is , You can find the corresponding learning resources according to the above knowledge points , Make sure you learn more comprehensively .

Two 、Python Essential development tools

3、 ... and 、 The high-quality goods Python Learning books

When I learn a certain foundation , When you have your own understanding , I will read some books compiled by my predecessors or handwritten notes , These notes detail their understanding of some technical points , These understandings are quite original , You can learn different ideas .

Four 、Python Video collection

Watch the zero basics learning video , Watching video learning is the quickest and most effective way , Follow the teacher's ideas in the video , From foundation to depth , It's still easy to get started .

5、 ... and 、 Practical cases

Optical theory is useless , Learn to knock together , Do it , Can you apply what you have learned to practice , At this time, we can make some practical cases to learn .

6、 ... and 、Python Exercises

Check the learning results .

7、 ... and 、 Interview information

We learn Python Must be to find a well paid job , The following interview questions are from Ali 、 tencent 、 The latest interview materials of big Internet companies such as byte , And the leader Ali gave an authoritative answer , After brushing this set of interview materials, I believe everyone can find a satisfactory job .

This full version of Python A full set of learning materials has been uploaded CSDN, Friends can scan the bottom of wechat if necessary CSDN The official two-dimensional code is free 【 Guarantee 100% free

Python Information 、 technology 、 Course 、 answer 、 For consultation, you can also directly click on the business card below , Add official customer service Qi


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved