DEV Community

Cover image for Python Project - Packages and Modules
Meir Gabay
Meir Gabay

Posted on • Edited on

2 2

Python Project - Packages and Modules

Goal

Get familiar with the fundamental terms of a Python project.

Project, Packages, Modules, And Scripts

  • Project: A directory, also known as the top-level package, which contains packages and modules
  • Package (in a project): a directory which contains modules and/or packages (sub-directories)
  • Module: a Python script (.py) which can be imported with import and from
  • Script: a Python script (.py) which can be exected from the terminal. This will be the only reference to the word script, since we're not scripting, we're developing a Python package

Packages Which Are Not Part Of The Project

  • Package (built-in): A package that is shipped with Python and can be imported with import and from. For example, os, pathlib, etc.
  • Package (pip): A package that was installed by pip and can be either imported with import and from, or get executed from the command-line (terminal). Think about it, pip stands for Package Installer for Python. For example numpy, pillow, etc.

Importing Project Modules

  • Python project's packages and modules can be (and should be) imported with relative paths from any module which is part of the same project. An example is available in src/appy/core/app.py
  from ..utils import message, img_ascii
Enter fullscreen mode Exit fullscreen mode
  • If you intend to import a package or a module that is not part of the same project, you'll have to use absolute paths. This can be done with importlib, see this StackOverflow Question.

Why do relative imports raise a problem in pylint?

I don't know, all I can say is that it doesn't happen with flake8, I'm referring to the following warning

Attempted relative import beyond top-level-package pylint(relative-beyond-top-level)
Enter fullscreen mode Exit fullscreen mode

This blog post is part of the Python Project series, and is based on this GitHub repository - unfor19/python-project.

The GitHub repo includes an example of a Python project, and Wiki Pages that describe the necessary steps for developing, creating and distributing a Python package.


Originally published at github.com/unfor19/python-project on October 30, 2020

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more