Modern Python Projects Transcripts
Chapter: Python versions and packages
Lecture: pipx in action

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