Modern Python Projects Transcripts
Chapter: CI
Lecture: tox in action

Login or purchase this course to watch this video and the rest of the course contents.
0:00 First, we need to create a Tox any file and let's go to the documentation
0:06 and copy the basic example, what this code does.
0:11 It will create Python 2.7 and Python 3.6 virtual environment installed pytest and then run
0:18 pytest command. Since I don't want to use Python 2.7,
0:22 let's remove it and let's use some more up to date version of Python.
0:28 Let's see if this works. So first we have to actually install Tox.
0:35 I am inside of virtual environment,
0:37 but you might as well use pipx to install it globally.
0:42 I will use pip, and let's run Tox command and we have an error
0:50 The thing with Tox is that it requires you to have either a pyproject.toml
0:55 or file.
0:58 If you're not building a python package,
1:00 then you probably don't have a
1:03 And when you manage your dependencies with a simple requirements file,
1:07 you might also not have the pyproject.toml.
1:10 So, instead of creating a dummy file just to make this error go away,
1:14 we can set a configuration option, to tell Tox that we are not going to use
1:18 any of those files, and I don't actually remember what's the name of this option
1:22 So, let's search for it.
1:26 Yeah, here it is.
1:32 It Skip this and we have to set it to true.
1:39 Let's try one more time. Cool.
1:43 So, now we have a different error.
1:46 So, the thing with Tox is that you have to first install different versions of,
1:50 python if you want to use them.
1:52 And my computer doesn't have a command Python 3.6 or Python 3.8,
1:57 because I'm using pyenv. The easiest way to make Tox work with pyenv is to
2:02 enable multiple local python versions. But before we do that,
2:07 I need to make sure that I actually have some Python 3.6 and Python 3.8 version
2:12 installed. So let's see the list of all python versions installed with pyenv.
2:20 So I have Python 3.8, but I don't have Python 3.6,
2:23 so let's quickly install that one.
2:37 Let's use the latest version. This is going to take some time.
2:44 You can really go grab a coffee in the meantime,
2:48 so now we have both version 3.6 and 3.8.
2:51 If we switch to version 3.8,
2:55 we have the command Python 3.8.
2:59 If we switch to version 3.6,
3:01 we have the command python 3.6.
3:07 But, for example, we don't have a command python 3.8 when we are using
3:11 Python 3.6. You can solve this problem by using some plugins like Tox-pyenv.
3:17 But there is a much easier solution.
3:19 Let me go back to my Global Python 3.9.
3:24 We cannot pyenv local and specify two versions of Python,
3:28 and both versions will be available locally.
3:35 And now Tox should work.
3:41 As you can see, First Tox is creating a python 3.6 virtual environment,
3:46 installing pytest and running pytests.
3:50 Then it creates a python 3.8 environment and again it install pytest inside and run
3:55 our tests. And we got a success message,
3:58 meaning that tests are passing in both Python 3.6 and Python 3.8.
4:03 Let's add python 3.5 to this mix,
4:07 and that way we will see that our test will fail because the F strings should
4:11 not work. So, let me go back to the tox.ini.
4:14 here we at py35,
4:20 and again I have to install python 3.5.
4:22 Let's add python 3.5.
4:33 To our local list of pythons.
4:36 We could run pyenv local command again,
4:38 but what it does is it just creates this .python version file, so we can
4:44 go ahead and modify it directly.
4:49 And here we just add our new version of Python.
4:53 If we run Tox again this time,
4:55 you should run with 3 Python versions.
5:03 So a few things happen. First,
5:05 Pyenv had to create the new environment for Python 3.5,
5:10 and that we need tried to run test.
5:12 We have the syntax error because F strings are not supported.
5:16 So, that's good. And then for both Python 3.6 and 3.8,
5:20 we already had the environment created,
5:22 so it was much faster to run tests again.
5:26 So we know how to run tests.
5:27 But I told you that you can actually run any command that you want with Tox
5:31 So, let's also at the Black and Flake 8.
5:35 To our code to make sure that it looks nice and doesn't have any errors.
5:45 And we also need to add black and flake 8 to the list of our requirements
5:48 We could select a specific version of each of those plugins,
5:54 but I want to use the latest one,
5:56 so I will leave it without the version.
5:58 specifier. And now let's run our Tox.
6:01 Command, Ah, one more thing.
6:05 I forgot to remove. python 3.5 I removed python 3.5 so we can
6:13 keep using the F strings. All right,
6:18 so pytest, Black and Flake 8,
6:20 are all happy about our code.
6:23 Let's just go back for a moment and make sure that if I make some violations
6:27 to flake 8 rules, they will be picked up by Tox.
6:34 I'm going to import the module and then not use it, flake 8 should complain about
6:38 that. And as you can see,
6:45 it's complaining that choice was important but not used.
6:49 So, that's how you would use Tox to automate some mundane tasks on your computer,like running tests or running static analyzer tools.