CodeGym /Courses /Python SELF EN /Installing Packages

Installing Packages

Python SELF EN
Level 19 , Lesson 3
Available

4.1 Where to Find Packages

Before you start writing some code, make sure someone hasn't already written it for you. Not only might it already be written, but it could also be tested, documented, and cover all your current and future needs by 200%.

This happens all the time. Python is over 30 years old, and millions of programmers worldwide use it.

For all your needs, there's a fantastic website – pypi.org (The Python Package Index). If you need some library, you just visit the site and enter your query.

For example, I want to write my own client for Telegram. I go to pypi.org, type in telegram client in the search, and see about 10,000 libraries on this topic:

Number one has the description "Python aiohttp telegram client" – that's exactly what we need. Simple and beautiful.

Here, you can find any package for every occasion. So now you're a bit closer to understanding the essence of modern programming work: you need to know where the good packages are, how to use them, and know how to work around their weaknesses.

4.2 Example of Installing a Package

Let's install a package and figure out how to do it correctly using an example.

There's a funny library that draws a cow and text next to it.

It's called cowsay. We'll be using it to learn about importing.

To use the pip manager, you need to go to the Terminal. The easiest way is straight from PyCharm. There's a list of buttons at the bottom left:

Click on the Terminal button, and a window with a prompt will open up. It will look something like this:

There, you need to type the command pip install cowsay

Example:

If the library installs successfully, you'll see a message like:

That's it, now you can import this library (package) into your project and use it however you want.

4.3 Playing with the Cow

For example, you can write code like:


import cowsay

cowsay.cow("Should've learned Python...")

Here's what the program printed in my console:

The cowsay library also supports many other animals, like a dragon:


import cowsay

cowsay.dragon("The dragon says...")

As you can see, installing packages is quick and easy, and using someone else's code is simple and enjoyable.

If you're tired of playing with the cow, you can remove its library by using the command pip uninstall cowsay.

Using the terminal inside an IDE like PyCharm simplifies working with pip commands as it allows executing commands directly in the context of the current project.

2
Task
Python SELF EN, level 19, lesson 3
Locked
Using the requests package.
Using the requests package.
2
Task
Python SELF EN, level 19, lesson 3
Locked
Using the cowsay package
Using the cowsay package
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION