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

Better Python task automation tool: NOx official tutorial

編輯:Python

This tutorial will guide you through the installation of 、 Configure and run Nox.

install
Nox Can pass pip Easy installation :

python3 -m pip install nox
You may want to use user sites (user site) To avoid the whole situation Python install Cause confusion :

python3 -m pip install --user nox
perhaps , You can also be more refined , Use pipx:

pipx install nox
Either way ,Nox Usually to be installed globally , Be similar to tox、pip And other similar tools .

If you are interested in docker Internal operation nox, have access to DockerHub Upper thekevjames/nox Mirror image , It contains all nox Build and all supported Python edition .

If you want to GitHub Actions Run in nox , You can use Activatedleigh/setup-nox action, It will install the latest nox, And order GitHub Actions All that the environment provides Python Version available .

Writing configuration files
Nox Through a project directory named noxfile.py Configuration of the file . This is a Python file , Defined a set of conversations (sessions). A session is an environment and a set of commands that run in that environment . If you are familiar with tox, A conversation is like its environment . If you are familiar with GNU Make, Conversation is similar to its target.

Conversation use @nox.session The decorator made a statement . This way is similar to Flask Use @app.route.

Here is a basic Nox file , Yes example.py function flake8( You can create your own example.py):

import nox
@nox.session
def lint(session):
session.install("flake8")
session.run("flake8", "example.py")

First run Nox
Now? , You have installed Nox And have a profile , Then it can run Nox 了 ! Open the project directory in the terminal , And then run nox . You should see something like this :

$ nox
nox > Running session lint
nox > Creating virtualenv using python3.7 in .nox/lint
nox > pip install flake8
nox > flake8 example.py
nox > Session lint was successful.

Now you have successfully used... For the first time Nox La !

The rest of this tutorial will take you through other available Nox Common operations completed . If necessary , You can also skip to command line usage and configuration &API file .

Install dependencies
Nox Basically, I will session.install Pass to pip , So you can install things in the usual way . Here are some examples :

(1) Install one or more packages at a time :

@nox.session
def tests(session):
# same as pip install pytest protobuf>3.0.0
session.install("pytest", "protobuf>3.0.0")
...

(2) according to requirements.txt Files installed :

@nox.session
def tests(session):
# same as pip install -r -requirements.txt
session.install("-r", "requirements.txt")
...

(3) If your project is a Python package , And you want to install it :

@nox.session
def tests(session):
# same as pip install .
session.install(".")
...

Run the command
session.run Function allows you to run commands in the context of the session's virtual environment . Here are some examples :

(1) You can install and run Python Tools :

@nox.session
def tests(session):
session.install("pytest")
session.run("pytest")

(2) If you want to pass more parameters to a program , Just give run Just add more parameters :

@nox.session
def tests(session):
session.install("pytest")
session.run("pytest", "-v", "tests")

(3) You can also pass environment variables :

@nox.session
def tests(session):
session.install("black")
session.run(
"pytest",
env={
"FLASK_DEBUG": "1"
}
)

More options and examples for running programs , Please see the nox.sessions.Session.run().

Select the session to run
Once your Noxfile There are multiple conversations in , You'll notice Nox All sessions will be run by default . Although it's useful , But usually you only need to run one or two at a time .

You can use --sessions Parameters ( or -s) To select the session to run . You can use --list Parameters show which sessions are available , Which will run . Here are some examples :

This is a three conversation Noxfile:

import nox
@nox.session
def test(session):
...
@nox.session
def lint(session):
...
@nox.session
def docs(session):
...

If you only run nox --list , You will see that all sessions are selected :

Sessions defined in noxfile.py:
* test
* lint
* docs
sessions marked with * are selected,
sessions marked with - are skipped.

If you run nox --list --sessions lint,Nox Will only run lint conversation :

nox > Running session lint
nox > Creating virtualenv using python3 in .nox/lint
nox > ...
nox > Session lint was successful.

There are more ways to choose and run sessions ! You can read more about calling... In command line usage Nox Information about .

For many different Python To test
Many projects need to support a specific Python Version or multiple Python edition . You can give @nox.session Appoint Python, To make Nox Run sessions against multiple interpreters . Here are some examples :

(1) If you want the conversation to be about Python Running a single version of :

@nox.session(python="3.7")
def test(session):
...

(2) If you want the conversation to be in Python Running on multiple versions of :

@nox.session(python=["2.7", "3.5", "3.7"])
def test(session):
...

You'll notice , function nox --list It will be shown that this session has been extended to three different sessions :

Sessions defined in noxfile.py:
* test-2.7
* test-3.5
* test-3.7

You can use nox --sessions test Run all test conversation , You can also use the full name shown in the list to run a single test conversation , for example ,nox --sessions test-3.5. More details about choosing a session , See the command line usage documentation .

You can talk virtualenv In the configuration , Read more about the virtual environment used to configure sessions .

And conda Test together
Some projects , Especially in the data science community , Need to be in conda Test its usage in the environment . If you want the conversation to be in conda Running in the environment :

@nox.session(venv_backend="conda")
def test(session):
...

Use conda Install package :

session.conda_install("pytest")

It can be used pip Install the software package into conda Environment , But the best practice is to use only --no-deps Option installation . This can be avoided pip Install the package with conda The installed package is not compatible , prevent pip damage conda Environmental Science .

session.install("contexter", "--no-deps")
session.install("-e", ".", "--no-deps")

A parameterized
It's like Nox Can control the operation of multiple interpreters as well , It can also be used nox.parametrize() Decorator , To process a session with a series of different parameters .

This is a short example , Use parameterization on two different versions of Django To test :

@nox.session
@nox.parametrize("django", ["1.9", "2.0"])
def test(session, django):
session.install(f"django=={django}")
session.run("pytest")

If you run nox --list , You will see Nox Expand a session to multiple sessions . Each session will get a parameter value that you want to pass to it :

Sessions defined in noxfile.py:
* test(django='1.9')
* test(django='2.0')

nox.parametrize() The interface and usage of is specifically similar to pytest Of parametrize. This is a Nox One of the most powerful features of . You can do this in a parameterized session , Read more about parameterization and examples .

The above is all the content shared this time , Want to know more python Welcome to official account :Python Programming learning circle , send out “J” Free access to , Daily dry goods sharing


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