Modern Python Projects Transcripts
Chapter: Python versions and packages
Lecture: pipx in action
0:00 Now that we have pipx installed. Each time we want to install a Python package that should be available globally instead of
0:08 running, pip install and the name of a package, we're gonna run pipx install and the name of the package.
0:14 so,there is just one letter of a difference in your commands. Let's say I want to install black.
0:21 I can do this with pipx install black and now I can use black. You can see that after installing black,
0:31 I have three new global commands black, black primer and blackd. so,now if I run black Command,
0:39 it doesn't matter, if I'm inside the virtual environment or not. I will always use this global black package.
0:45 And if I want to install a different version of black, let's say inside of my virtual environment,
0:49 I can still do this. Let's create a temporary virtual environment and let's install a different version of black inside of it.
0:57 so,first I will activate virtualenvwrapper. Then I can run mktmpenv.
1:04 This will create a temporary virtual environment that will be deleted when I deactivated. so,we are inside of it. We have no pip packages installed.
1:14 Let's install black version 19. so,there is No 19. There is 19.3b0. Oh, right. so,now if we try to run black --version to
1:30 see which black we're using, you can see that we're still using the global one
1:35 so,it's important to remember that if you have a problem with using a version of a package inside of a virtual environment,
1:42 always run pyenv rehash. so,now we will be using black version installed inside of this virtual environment. we deactivate it.
1:56 We are back to our global black version. so,what else can I do with pipx?
2:02 Let's see the list of commands, we have installed that we just used to install the package, to uninstall a package. There is uninstall,
2:10 and if you want to uninstall all packages or install them, then there is uninstall-all, install all, upgrade is a useful command.
2:18 If you want to upgrade a single package or if you want to upgrade-all the packages installed with pipx, you can just run,
2:24 upgrade-all, another really cool command is pipx run. It will run a command from a pip package in a temporary virtual environment,
2:35 and it will immediately destroy this environment after the command exits. This is very useful when you know that you want to run a given command only
2:44 once in your life. so,instead of doing pip install, then running the command and doing pip uninstall,
2:50 you just do pipx run and you specify command from that package. Let me show you an example.
2:57 This is a silly example, but let's say we want to run command from this
3:01 cow say package that prints this ASCII character of a cow and some text you specify As you can see, we just ran a command from the cowsay package
3:11 But if we check the list of installed packages, cowsay is not installed anywhere. As I said, this is a very useful command.
3:21 If you want to run and one of command without cluttering your computer with redundant packages another very useful command is inject.
3:33 This lets you install a pip package inside of an existing virtual environment. If it sounds confusing, don't worry.
3:39 For a long time I didn't understand why would I install a package inside of an existing environment, if I actually want to isolate my packages,
3:47 right? Well, this is useful when you're using pip packages that have plugins, but those plugins are separated pip packages.
3:55 A great example here is pytest the most common testing library for Python that I will
4:00 talk more in Chapter eight. But right now you need to know that pytest has many plugins, and to use those plugins you have to first,
4:08 install them. so,let me show you. First, we need to install pytest. And now let's say we want to install the pytest-cov pluggin that will display
4:18 the test coverage of your code. How do we install this plugin? Well, we can't run pipx install pytest-cov.
4:25 Because this will install our plugin in a separate virtual environment. so,we need to install it in the same environment where we installed pytest.
4:34 We could figure out where this virtual environment is located, manually activated and then run pip inside. But there is no need for that.
4:43 Since we have the inject command, we can run pipx inject pytest pytest-cov,
4:49 where the first argument in our case pytest is the name of the existing package and the second argument is the name of the package that you want to add
4:57 to that environment. As you can see, it said, injected package pytest-cov into venvpytest.
5:04 so,now we can run pytest with --cov parameter that comes from this new plugin that we just installed.