4 ms·
What Is a Python Package?
- JohnLockwood 5y agoTL;DR; here are the contents: What’s the Difference Between a Python Package and a Module? How do I use the Import Statement in Python? Understanding ModuleNotFoundError How to Install Python Packages Making Sure Your Pip Version Is Correct How to Install Packages (The Right Way) 1. Creating the Virtual Environment: 2. Activating the Virtual Environment: 3. Using the Virtual Environment: 4. Deactivating the Virtual Environment: Dependency Hell — Why We Set Up Virtual Environments How Python Finds Packages Changing the Python Path and Controlling Where Pip Installs Packages (Case Study) How Do I Publish a Package in Python? Final Thoughts and Source Code
- ryan-duve 5y agoI often see empty `__init__.py` files in package folders at work despite nobody being able to demonstrate any difference from when they're removed. For example, this works with an `__init__.py`: $ docker container run --rm -it --entrypoint bash python:3-slim root@bf9368a0ccd6:/# mkdir folder root@bf9368a0ccd6:/# echo "x = 7" > folder/source.py root@bf9368a0ccd6:/# python Python 3.10.2 (main, Jan 29 2022, 02:55:36) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from folder.source import x >>> print(x) 7 Research on the need for empty `__init__.py` files always surfaces outdated articles back from when Python2 required them and/or what I can only describe as sentiment such as "because that's how it's always been done". Meanwhile, valid reasons to not include empty `__init__.py` files are: 1. Removing unneeded things simplifies the package code base. 2. When tracking down a package bug, if there is an `__init__.py` file in every folder, they have to be checked for logic potentially causing the issue. Maybe someone on HN can give a clear answer relevant in 2022: what is the benefit of putting empty `__init__.py` files in Python3 package folders?
- JohnLockwood 5y agoThat's a good point. It was only when reading about namespace packages in PEP 420 that I realized they were no longer required. Honestly, I think adding them now is just a matter of folks not knowing they're no longer required.