Modules, Packages, Pypi in Python.
Python Module is a singular file with a collection of methods, classes, local variables and functions. A module can be imported and re-used into another python file. Python Package is a collection of modules in a directory.
List of modules in a directory is not enough to be called a python package. A python package also needs an
The __init__.py file tells python a particular folder contains a package. __init__ file runs everytime a python package is imported into an environment or file. Inside the __init__.py file, you may need to import your other modules into it.
When doing import, you may need “.” in front of the file name. For example. To import a file/module into another module/file in python, you use this syntax.
from .file_name import function, method, this_class # OR from file_name import function, method, this_class
Either one will work. If one doesn’t work, try the other installation method. Starting in python 3, if we didnt’ add “.” in front of the file_name, then importing things will be a bit more complicated and indirect because you have to identify the folder level. For example, to import something, you will have to do
from folder.file_name import function, method, this_class
There is a file called
setup.py file that is on the same folder level as our parent folder that has the python modules. This file contains information like
package name, version, description, packages, and other information.
To install a local python package into your environment, just navigate to the folder where the package
setup.py file is located and type in
pip install . this command tells python to look for the setup file and install everything accordingly.
To find where your package is installed on your computer, type in
package_name.__file__ that should give you information about where it is installed.
After changing a package, you can re-install the changes and receive the upgrade by doing
pip install --upgrade . (this command should be run from the folder directory). This will uninstall the old package and re-install it.
To run unit tests as you develope your package, run
python -m unittest unit_test_name
To upload your packages using the command line….
cd into package_folder_files python setup.py sdist #or python3 setup.py sdist bdist_wheel (See below for explaination) pip install twine # commands to upload to the pypi test repository twine upload --repository-url https://test.pypi.org/legacy/ dist/* # install package after upload pip install --index-url https://test.pypi.org/simple/ package_name # command to upload to the pypi repository twine upload dist/* # install package after upload pip install package_name
Don’t forget to add files like
setup.cfg (name of readme file, doesn’t negate setup.py file. Both exists) and
readme.md (describes how your package works) files.
# example setup.cfg file. [metadata] description-file = README.md
Example of setup.py file
setup(name='test', version = '1.2'), description "My package I just made. HELLO", packages=[same_as package_name] #(example - ['test'], author='me, me, me', author_email = "absolutely_not@nobodycom", zip_safe = false = # makes sur the package won't be installed using zip file.
Running this command
python setup.py sdist generates a .tar.gz file with is a source archive which contains files needed to compile and install the package. while this command
python3 setup.py sdist bdist_wheel will generate .tar.gz file as well as .whl file which is a build distribution.
Every package on pypi has to have a unique name. A package name can have hypen in it, but when using it locally, hypen is replaced with underscore.
More information on MIT License — Used to define copyright information. Open source.