Create python package __init__ py
Show
What is a Python package?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 It provides a mechanism for you to group separate python scripts into a single importable module. Let's run through some examplesThe best way to understand why you would use 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 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 Well, we can only import these files if they are in the current directory that whatever script we are running is running from. 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 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__.pyWhat if we wanted to seperate these scripts into a folder in order to keep them more organized? Well, that is where the First, lets move our scripts into a new subfolder and call it: 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 Let's make a new # 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 Adding imports to init.pyOpen your # __init__.py from .stringLength import stringLength from .stringToLower import stringToLower from .stringToUpper import stringToUpper Note that the And so with that in our # 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
So, that is basically what Debugging import IssuesThere are basically 3 tips I have for debugging import issues:
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! What is __ init __ py in Python package?The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.
How do I create a Python package in Python?Creating Packages
Whenever you want to create a package, then you have to include __init__.py file in the directory. You can write code inside or leave it as blank as your wish. It doesn't bothers Python. Create a directory and include a __init__.py file in it to tell Python that the current directory is a package.
Should I add __ init __ py?Although Python works without an __init__.py file you should still include one. It specifies that the directory should be treated as a package, so therefore include it (even if it is empty).
What is __ init __ py give an example?The __init__.py file lets the Python interpreter know that a directory contains code for a Python module . An __init__.py file can be blank. Without one, you cannot import modules from another folder into your project. The role of the __init__.py file is similar to the __init__ function in a Python class.
|