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


Talk Python's Mastodon Michael Kennedy's Mastodon