Last entry we learned how to set up our work environment, a minimal project structure, we installed the required packages and also we learned how to make our tests pass.
In this chapter we’ll learn how to create and import a class from a different module in different folders, also how to initialize a class into an object and manipulate our object’s representation.
- Open the file
ipinfo.pywhere you will create a class called
IPInfo, you can write an empty class for this challenge.
- After you create the class, go to
__init__.pyimport the Class itself (not the module containing the class).
- Define a
__all__to include your class in the
This challenge requires that we create a class called
IPInfo inside our module
ipinfo.py which is in the
modules directory. You don’t have to create a class with methods and initializing values just yet, it is only asking for a class which can be empty right now.
ipinfo.py and create a class
Now that we have defined our class let’s import it and initialize it in our module, let’s open
from .ipinfo import IPInfo
__all__ = ['IPInfo']
Now we re-run our test and everything should be O.K
Ran 2 tests in 0.000s
__init__.py is necessary to treat our
modules directory as a package, this means that we can import it in our python programs. Usually when you import a python file you can do so by typing:
To import a python file inside another folder you can do so by typing:
Now, if we create an empty file called
myfolder then python will treat it as a package instead of a regular folder and you can import all the modules that the package
Or we can import only one module if we wanted to:
from myfolder import mymodule
This is what is happening to our package
modules that we initialized as a package with the
<module 'modules' from '/home/franccesco/workspace/ipchallenge/modules/__init__.py'>
<module 'modules.ipinfo' from '/home/franccesco/workspace/ipchallenge/modules/ipinfo.py'>
The initialization file also let us do some other stuff, for example, I don’t want to use my module like
modules.ipinfo.IPInfo(), I would like to write only
IPInfo() instead, we can import our
IPInfo class directly into our initialization file, let’s open it.
What about that
ipinfo? We are saying that we want to import the module from the current folder instead of a module or package loaded in our
PYTHONPATH, you can leave the dot behind if you want, but for the sake of being explicit we will leave it there.
Now we can import our class directly without having to write the package name and the module name:
from modules import IPInfo
But there’s one thing, if we run flake8 to check if our code is PEP8 compliant it will find an issue with our initialization file:
$ pipenv run flake8 modules
modules/__init__.py:1:1: F401 'ipinfo.IPInfo' imported but unused
It complains that we’re not using our class
IPInfo, here’s were
__all__ can help us out. We define
__all__ to explicitly define a list of the modules that we are importing, this will fix the flake8 problem.
That is enough for our first challenge, we learned how to import our modules correctly, from other folders and the current folders, also we learned how to respect the PEP8 in our initialization file. Let’s see what the next challenge will bring to our table.