Stick to original output, click on the blue word to follow me

Author: Qing Han blog: OSChina, Cloud + community, Zhihu and other major platforms.

directory

  • 1. Preparation
  • Second, virtual environment
    • 1. Install and apply virtualEnv
    • 2. Pipenv installation and use
    • 3. Export the virtual environment as requirements. TXT
    • 4. Commands for using pipenv
    • 5. You need to start projects in a virtual environment
  • Python code specification
  • 4. Project structure

What does the test platform do?

Test development of the test platform is for people to use, can help us do automated testing, use case management, report generation, improve the efficiency of the test.

For example, some tests done with postman on the market are not in line with the reality of the company’s project. However, the automated test platform can be customized according to the actual project needs of the company.

1. Preparation

Python environment: Python 3.7 is currently the most stable version of Python 3.

Computer: Windows10 home

Second, virtual environment

The main purpose of the Python virtual environment is to create independent running environments for different projects. In a virtual environment, each project has its own dependency package, independent of any other project.

There can be different versions of the same package in different virtual environments. Also, there is no limit to the number of virtual environments, and you can easily create multiple virtual environments using tools such as VirtualEnv or Pipenv.

1. Install and apply virtualEnv

(Windows) Installation Steps:

1. The PIP install virtualenv

pip install virtualenv

2. PIP installs the VIRTUAL environment management package

PIP install Virtualenvwrapper -win

Note: PIP install VirtualenvWrapper for MAC or Linux.

3. Create a directory to save the virtual environment (you are advised to name it. Env or. Virtualenv.

4. Configure the environment variable (variable name: WORKON_HOME, value: directory path created above)

Enter workon to display all virtual environments:

To create a virtual environment, use the following commands:

wokon List all virtual environments
workon[name] The specified virtual environment is displayed
deactivate Exit the current virtual environment
mkvirtualenv[name] Creating a Virtual Environment
rmvirtualenv[name] Deleting a Virtual Environment

Package installation:

In a virtual environment, install the dependency packages and use PIP Install to install the dependency packages.

mkvirtualenv -p python3 qinghan

If only Python3 is installed on your computer, type mkvirtualenv qinghan

After you see this virtual environment name, create a virtual environment will automatically enter the virtual environment inside.

First type exit (); To enter the deactivate

To enter a virtual environment, type: workon Qinghan

To delete a virtual environment, type rmVirtualEnv Tester

Type PIP List to view python-related dependencies currently installed on your computer.

Check how many dependencies exist in the virtual environment:

Enter workon Qinghan and then PIP List

Install a Django package in qinghan:

pip install django

This package is not visible in other virtual environments and is isolated.

PIP uninstall Django

Using virtualEnv as a management tool has a disadvantage:

When installing Django, you install all django-related dependencies. The Pytz and SQLParse packages come with django when you install it.

2. Pipenv installation and use

Pipenv integrates the functionality of both PIP and VirtualEnv, and also addresses some of their drawbacks.

In the past, managing requirements. TXT files with Virtualenv might have been problematic; Pipenv used Pipfile and pipfile.lock.

The Pipfile file is used to record the list of dependencies for the project. The pipfile. lock file records the detailed list of dependencies for the fixed version.

Python2 and python3 are supported. Support MAC, Win, Linux, in each platform command is the same. Hash check is used everywhere, so it is very safe to install and uninstall packages.

PIP install pipenv

Creating a Virtual Environment

Step 1 Create a folder mkdir py3env
Step 2 Enter the folder cd py3env
Step 3 Initialize the virtual environment pipenv install

After the virtual environment is created, a Pipfile file and a pipfile.lock are generated.

Pipfile and pipfile. lock files can be submitted together with the project submission for other development clones to download.

The pipfile. lock file hashes the package name, version, and dependencies using the hash algorithm to ensure package integrity.

Pycharm automatically locates to the path of the current project:

I’ve already installed it here. Once installed, you don’t need to configure environment variables and can use them directly.

To create a virtual environment in the project, type Pipenv install directly into the project

The default is PYTHON37, so use the default PYTHon37 interpreter to create the virtual environment.

Pipenv management module and package

Install the module or package in the virtual environment pipenv install package name (note that the above command is executed in the current project path)

Use — dev to specify that it will only be installed in the development environment (development is your current development environment, production is the actual live deployment environment).

pipenv install requests --dev

Installation packages are recorded in either the [dev-Packages] section or the [Packages] section.

If — dev is specified during installation, only packages under [dev-packages] are installed. If the — dev parameter is not specified during installation, only modules under the [Packages] package are installed.

Uninstall the module package of the virtual environment Pipenv uninstall the package name
View installation packages and dependencies pipenv graph
Generate the requirements.txt file pipenv lock -r –dev > requirements.txt
Install the environment through requirements.txt pipenv install -r requirements.txt

Pipenv install does three things:

1. If no virtual environment exists in the current path, create a virtual environment.

Pipenv install pipenv install requests pipenv install requests

3. The source url = “https://pypi.org/simple” is abroad, need to replace the Python domestic source: tsinghua university source https://pypi.tuna.tsinghua.edu.cn/simple

Now install the package pipenv install Requests

When installing, the installed packages are recorded under [packages] by default:

If you want to install packages only for use in the development environment and not under [packages], you can place them under [dev-packages].

A Pipfile file and a pipfile. lock file are used to record dependencies in the current environment and to restore the environment on the server.

The pipfile. lock file is more complete, such as installing Requests in the virtual environment, but does requests depend on other packages? That’s in pipfile.lock. Pipfile.lock this file cannot be modified at will!

The pipfile. lock file is very accurate and detailed:

For example, if a project is developed under this project, then the project is deployed to the server, and the pipfile. lock file is uploaded to the server along with the project code.

The pipfile. lock file on the server can be used to restore the current development environment, and the corresponding commands can be used to restore the environment.

The default path where the virtual environment is created is the path where the pipenv command is run.

Here in the Pipfile file can be modified:

Installed packages, for use on the server, in [packages], for development environment only in [dev-packages]

= “*” whatever that means.

To create another virtual environment in the class_01day folder, copy the Pipfile and pipfile. lock files. When you upload your project to the server, take these two files to the server as well.

For example, the project under the server is class_01day, which is a path to the project under the server.

Under the server to restore the current code running environment, go to the path below, run the command pipenv install to create a virtual environment.

Note: it’s not just copying these two files. The project code should be sent along with these two files. Through these two files, you can restore all the environment at the time of development to the server.

View the installed dependency packages in the current virtual environment:

pipenv graph

Packages in the development environment will not be restored.

Flask = “*”, which was manually added in Pipfile under test_01.

After copying these two files to the PYTHon_CK01 folder, the packages under [Packages] will be automatically installed at recovery time.

You can also restore packages for [dev-packages] by entering pipenv install –dev

Pipenv is a more powerful tool than VirtualEnv. Pipenv is a wrapper around VirtualEnv.

Uninstall dependencies in the environment, such as uninstall requests. There are four related dependency packages below requests.

Uninstalling Requests here will also unmount the packages that requests depends on. Virtualenv uninstalls requests only, but does not uninstall requests dependencies.

Pipenv uninstall requests

A virtual environment can hold only one requests.

The relationship between Requests and dependency packages that rely on these modules to implement requests:

You can only see it with graph, not json files.

After uninstalling requests, its dependencies are saved, and the packages fall out.

However, if you copy these two files to the server, the package it depends on will not be restored when it is recovered.

Virtualenv can also export virtual environments if you use the virtualenv tool.

For example, if requests is removed from the virtualEnv environment, requests dependencies will be restored to the server.

Export virtualenv dependencies. You can export a requirements. TXT file using virtualEnv.

3. Export the virtual environment as requirements. TXT

Requirements. TXT file, which records all dependency packages and version numbers of the current program.

It is used to rebuild the runtime environment dependencies required by the project on another environment.

Export the current environment to requirements.txt pip freeze > requirements.txt
Use requirements.txt to restore the environment pip install -r requirements.txt

If you get this file, you can restore it to the server.

Pipenv can be restored using Pipfile and pipfile. lock files. Environment restoration can also be done through requirements.txt.

4. Commands for using pipenv

Pipenv – rm Deleting a Virtual Environment
Pipenv – where Lists the local project paths
Pipenv – venv List the virtual environment paths
Pipenv – py Displays Python interpreter information
pipenv graph Viewing package dependencies
pipenv lock Generate lock file
Pipenv – rm Deleting a Virtual Environment
pipenv shell Go directly to the virtual environment
exit Exiting a Virtual Environment

Pipenv is an encapsulation of VirtualEnv. The underlying implementation of Pipenv is virtualEnv and PIP. Pipenv integrates the virtualEnv and PIP tools.

Virtualenv can only be restored using requirements. TXT.

With Virtualenv, you can use commands to operate all virtual environments without looking at the current path.

With Pipenv, this command can only operate virtual environments in the current directory.

After the virtual environment is deleted, the Pipfile and pipfile. lock files and folders will not be deleted, we need to manually delete.

Virtualenv can be deleted like this:

rmvirtualenv fff

Pipenv –where lists the directories where the current virtual environment resides.

Virtual environments are stored under virtualEnv. To check where the current virtual environment resides, type pipenv –venv

After uninstalling some packages, you can use the pipenv lock command to regenerate the lock file.

The lock file is now generated synchronously. If you delete the lock file, you can use this command to generate the lock file.

5. You need to start projects in a virtual environment

If you do not enter the virtual environment, there is no relevant dependency package, the startup project will not start.

Python code specification

1. Pep8 (Python Code Style Specification)

Document Address (Chinese):

Blog.csdn.net/ratsniper/a…

2. Pep257 (Python docstring conventions) :

Document Address:

Github.com/qiuxiang/pe…

3. Pep20 (Python code idea) :

Document Address:

www.python.org/dev/peps/pe…

Key points:

Adjust the code format: Ctrl+Alt+L

Note:

Module and package specifications:

Naming conventions:

4. Project structure

The code needs to be written succinctly, and the structure of the project needs to be so succinctly that people can click on the project file and see what’s in that file.

Structure introduction:

There must be other folders in one project, but having these folders in one project makes it more formal, hahaha.


The public account “QingHAN Software Test” was launched. More original articles: Qinghan software test 105+ original articles, welcome to follow and exchange, and unauthorized reprinting by third parties is prohibited.