16 – Third Party Libraries And Package Managers

Compared to most languages, Python has a large standard library. In fact, people say Python comes with batteries included because it comes with the libraries you need to get right to work. However, the standard library doesn’t come with everything you might want. Some tasks are too specialized to be accommodated by the standard library. Fortunately, there are tens of thousands of third-party libraries written by independent developers. How do we get these packages though, if they aren’t included with Python itself? We can install libraries using pip, a package manager that is included with Python 3. Pip is a standard package manager for Python, but it isn’t the only one. One popular alternative is Anaconda, which is designed specifically for data science. Here, we’ll use pip, which is a general standard. Let’s use pip to install this library, which is used for working with time zones or remarkably complicated task. This command will download and install the package, so that it’s available to import in our programs. Once installed, we can import third-party packages using the same syntax we used to import from the standard library. In this example, I import a time zone package along with a datetime from the standard library. It’s standard practice to put the import statements for third-party libraries after imports from the standard library. This example stores the current time expressed in terms of Coordinated Universal Time or UTC in the variable now. It then translates this time into IST, Indian Standard Time and stores that in the variable ist_now. Larger Python programs might depend on dozens of third-party packages. To make it easier to share these programs, programmers often list of projects dependencies in a file called requirements.txt. This is an example of a requirements.txt file. Each line of the file includes the name of a package and it’s version number. The version number is technically optional, but it usually should be included. Libraries can change subtly or dramatically between versions, so it’s important to use the same library versions that the program’s author used when they wrote the program. You can use pip to install all of the project’s dependencies at once with this command.

Dr. Serendipity에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

Continue reading