Python site packages init py

How to Use Python Packages

Gigi Sayfan

Gigi Sayfan Last updated Jun 29, 2023

Python packages allow you to break down large systems and organize their modules in a consistent way that you and other people can use and reuse efficiently. Python’s motto of «Batteries Included» means that it comes preloaded with lots of useful packages in the standard library.

But there are also many amazing third-party packages you can take advantage of. In this tutorial, you’ll learn all you need to know about what packages are exactly, how to import modules from packages, exploring the built-in packages in Python’s standard library, and installing third-party packages.

What Are Packages?

Before we can talk about packages, let’s talk about modules. Modules are the source files with *.py extension where you (and everyone else) put the functions and classes that comprise your program.

A package in Python is simply a folder with multiple Python files and should have an __init__.py file. The __init__.py file indicates that the directory is a package. The __init__.py file can be empty or contain some executable code.

Packages are the manifestation of Python’s hierarchical namespaces concept. To quote from the Zen of Python:

To view the whole Zen of Python, type import this in a Python interactive session:

The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Special cases aren't special enough to break the rules. 
Although practicality beats purity. 
Errors should never pass silently. 
Unless explicitly silenced. 
In the face of ambiguity, refuse the temptation to guess. 
There should be one-- and preferably only one --obvious way to do it. 
Although that way may not be obvious at first unless you're Dutch.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea. 
If the implementation is easy to explain, it may be a good idea. 
Namespaces are one honking great idea -- let's do more of those!

Namespaces help with organizing code and preventing naming conflicts. This is critical when multiple people work together or when using packages developed by other people.

While packages represent a hierarchy of sub-packages and modules, which are files, the hierarchy doesn’t have to file-system-based, where packages and sub-packages are directories and sub-directories. It is much more flexible than that.

Create a Python Package

Let’s start with a simple example. Below we have a package called simple_package with two Python modules.

  • __init__.py : indicates that it’s a package
  • tasks.py and views.py are modules

Third-Party Packages

Let’s take a look at a package called ansible . It is not a package from the standard library. You’ll see later how to find and install third-party packages. Now, let’s just check out the directory file structure.

Читайте также:  Writing string to file in java

The packages will typically be installed into the Python interpreter’s site-packages directory, located somewhere (depending on version, OS, and distribution) under lib.

On the Mac, for example, Python 3.10 will be located in /lib/python3.10/site-packages. Here is how the ansible package is organized:

There are 6 modules and 18 directories. Each directory is a sub-package of the main ansible package. Looking inside the ansible/utils directory, we can see it contains additional modules and even one more sub-package:

The Search Path

When you import a module, Python will go through a search algorithm based on the search path, which is a list of directories to start the search. The search path is a list of directories available through sys.path , and you can manipulate it dynamically (add, remove or move around items in the search path). The site-packages directory is always there.

To import the path.py module from ansible/utils, you’ll need to use the following command:

To import both path and encrypt modules, you use the following commands:

import ansible.utils.encrypt

If you also want to use the standard os.path module, you’ll use the following command:

Now you can use either or both path modules with no conflicts due to the different namespaces they belong to.

Exploring the Standard Library

The standard library has a lot of packages. It’s worth exploring it whenever you need to accomplish a task and you’re not sure how. There is a very high likelihood that for any general-purpose task like math, shell integration, OS integration, string manipulation, networking and common file formats, there is a well-designed, well-performing and well-tested package in the standard library.

You can really trust standard library packages because it is a big deal to get into the standard library. Either the package was designed by Python’s core developers or it was heavily reviewed and often heavily used in the field as a third-party library before making it into the standard library.

Here are all the packages in the standard library organized by topic.

PyPI

The standard library is awesome, but there’ll often be some special functionality you need that is not standard. It doesn’t mean you have to write it from scratch. Python has a vibrant and active community that develops and freely shares a lot of code. Enter PyPI: the Python Package Index. PyPI hosts all publicly available packages and provides a one-stop shop for browsing through them.

Browsing PyPI

PyPI organizes the packages in a browsable index. You can browse and search by topic, environment, framework, development, status, intended audience, license, natural language, programming language (yes, there are Python packages that support many programming languages), and operating system.

As of 2021, PyPI does not display download statistics for packages since it’s ineffective due to the resources required to maintain the statistics.

Installing Packages

There are two ways to install packages from PyPI. You can download the package and then run python setup.py install . But the modern way is to use pip, setuptools, and wheel.

Pip and setuptools are included by default starting from Python 3.4 and Python 2.79, but you will need to upgrade to the latest version:

  • Linux/macOS: pip install -U pip setuptools
  • Windows: python -m pip install -U pip setuptools
Читайте также:  IT SourceCode

Python 2 is, however, no longer supported, so you should already be using Python 3.0 or higher for improved performance.

To install a package with pip, you issue this command.

Where package_name is the name of the package. For example, to install Ansible, the command would look like this:

If you need a specific version, you can also specify it as follows:

Python packages are always installed into an environment. A common practice I will not cover here is to use virtual environments to manage multiple independent installations of Python with different interpreters and/or different sets of installed packages. You can read more about virtual environments here.

Best Practices

The Python packaging authority provides a lot of guidance on the best practices around packaging. This is important because it is an area of active development and recommendations evolve quickly.

Also, if you want to do something special like installing packages from alternative repositories instead of PyPI or using pip in a more sophisticated way, you’ll find great discussions and practical advice.

Conclusion

When you’re a Python beginner, you learn the core language and have fun playing with it. Pretty soon you discover the standard library, and as you gain more experience you benefit more and more from its richness.

The next stage in your evolution as a Pythonista is to incorporate the vast awesomeness the Python community has put on PyPI into your systems. Packages as the deployment unit of reusable Python code enable this ecosystem.

Источник

How to create a Python Package with __init__.py

Banner

A Python package is simply an organized collection of python modules. A python module is simply a single python file.

Why would I want to create a package using __init__.py ?

Creating a package with __init__.py is all about making it easier to develop larger Python projects.

It provides a mechanism for you to group separate python scripts into a single importable module.

Let’s run through some examples

The best way to understand why you would use __init__.py and to learn how to use it to create a package is to run through some quick examples! The best way to learn is by doing!

The code in this tutorial should work for Python 2 or 3. Just remember, if you are using 2 then you will need to use the from __future__ import print_function functionality.

Say we have three modules we have created:

someFolder |-- stringLength.py |-- stringToUpper.py `-- stringToLower.py

Remember a module is just another name for any single python file

For our example, the content of these files is the following:

# stringLength.py def stringLength(inStr): return len(inStr) 
# stringToUpper.py def stringToUpper(inStr): return inStr.upper() 
# stringToLower.py def stringToLower(inStr): return inStr.lower() 

Obviously, these functions are useless, but it helps to serve as a model for the basic concept that we have some python modules that we have already written that are somehow related.

So, without creating a package and using __init__.py , how do we use the functions in these files?

Well, we can only import these files if they are in the current directory that whatever script we are running is running from.

Читайте также:  Php ignore all notices

Well, we can use these files in a new Python script but with one key caveat:

To illustrate that, let’s create a file called example1.py that leverages our modules:

# example1.py import stringLength import stringToLower import stringToUpper some_string = "Hello, Universe!" print(stringLength.stringLength(some_string)) print(stringToLower.stringToLower(some_string)) print(stringToUpper.stringToUpper(some_string)) 

Adding a blank __init__.py

What if we wanted to seperate these scripts into a folder in order to keep them more organized?

Well, that is where the __init__.py file comes into play.

First, lets move our scripts into a new subfolder and call it: string_func . Then create an empty file in that folder called __init__.py

Here is our new file/folder structure:

someFolder |-- string_func | |-- __init__.py | |-- stringToUpper.py | |-- stringToLower.py | `-- strengthLength.py `-- example1.py

So, now let’s test out exactly what __init__.py allows us to do:

Let’s make a new example2.py file.

# example2.py import string_func.stringLength import string_func.stringToLower import string_func.stringToUpper some_string = "Hello, Universe!" print(string_func.stringLength.stringLength(some_string)) print(string_func.stringToLower.stringToLower(some_string)) print(string_func.stringToUpper.stringToUpper(some_string)) 

So, now we can access our string functions in this manner. This is great, because they are all in a seperate folder, but the syntax is definitely not very succinct. Let’s see if we can clean things up a bit by editing our __init__.py file.

Adding imports to init.py

Open your __init__.py file and make the following changes:

# __init__.py from .stringLength import stringLength from .stringToLower import stringToLower from .stringToUpper import stringToUpper 

And so with that in our __init__.py we can now shorten our code to:

# example3.py import string_func some_string = "Hello, Universe!" print(string_func.stringLength(some_string)) print(string_func.stringToLower(some_string)) print(string_func.stringToUpper(some_string)) 

Now the syntax is a lot shorter and you can see that string_func is behaving like its own module.

So, that is basically what __init__.py does! It allows you to treat a directory as if it was a python module. Then you can further define imports inside your __init__.py file to make imports more succinct, or you can just leave the file blank.

Debugging import Issues

There are basically 3 tips I have for debugging import issues:

  1. Use the interactive interpreter (The REPL) to import the modules and see if you are getting what you expect.
  2. Start your script with python -v -m my_scriptname.py and then check the output to see exactly where your modules are getting imported from.
  3. Use Pycharm. Pycharm’s fantastic introspection abilities mean that you will immeadiately know whether or not your module is being properly imported as it will indicate an error if not. It will sometimes also suggest the proper correction. The community edition is free and if you’re a student you can get a free subscription to ALL of their products!

For more information about python modules and packages you can see check the python documentation on it.

You can also check out this great Talk Python To Me podcast with David Beazley where he discusses the subject, as well as David’s talk on the same subject.

As usual, feel free to contact me if you come across any errors in this post!

Источник

Оцените статью