Python packages
Also called modules in Python
The purpose of modules (or package) is to divide, manage and reuse code. A module is a file that contains definitions and code.
A module contains code related to a function, and there can be multiple classes, functions and even scripts that need to be pre-executed in a module.
Unlike in Java where one class is one file (regardless of internal classes), a .py file in python can contain multiple classes.
Related course: Complete Python Programming Course & Exercises
Explore module
To load a module simply type
import module_name
The dir()
function is a built-in command in python that can be used to view the module. To view a module call dir(module_name), it returns the declaration information for the current file.
Executed as a script
The module itself can be executed as a python script.
The module can be executed directly from the command line using python module.py
, but if there is code in the module introduced during import it will also be executed.
To prevent that, put your code in a main function:
if __name__ == '__main__':
print('your code.')
pip
PIP is the tool used to install the package listed in PyPI. PyPI is the Python Package Index, an online repository of libs provided by python similar to Java's maven.
The functionality of PIP includes:
- installation
- uninstallation
- upgrade
- viewing
- local installation, configuration.
First, you need to check if the pip tool is available before using it, open cmd and enter pip .
If the pip tool doesn't work, check the python installation directory, find the file pip.exe
in the installation directory, add it to the system environment variable path, restart cmd and try again.
If this still doesn't work, we recommend reinstalling python, there is an automatic add to environment variable when installing python3, just select it.
Some computers open CMD directly, use the PIP tool to install or other operations failed, please open CMD as administrator.
Second, use the PIP tool to install third-party libraries, using the package numpy
as an example.
Install package
The command install for package is used to complete the installation using the command pip install somepackage. To install, type the command directly in cmd.
pip install numpy
Usually pip selects the latest version to install and can specify the desired package version via the parameter version.
In addition to the installation of a single package, the pip can be installed in bulk using the request file: pip install -r requirement.txt
.
The request file itself is a text file containing multiple package lists and can accept parameters such as version.
PIP supports installation from local files, eliminating the overhead of each network transfer for repeated installation scenarios, and automatically downloading the packaged installation file and installing it with the following command.
Download.
pip install --download <DIR> -r requirements.txt
Installation
pip install --no-index --find-links=[file://]<DIR> -r requirements.txt
Uninstall package
Uninstall third-party libraries.
pip uninstall numpy
PIP supports installation from local files, eliminating the overhead of each network transfer for repeated installation scenarios, and automatically downloading the packaged installation file and installing it with the following command.
View installed package
The command below shows installed packages:
pip list
View expired packages
pip list --outdated
View the details of a certain package
pip show Django
Search package
pip provides the search command to find package information in PyPI, such as
pip search django.
Uninstall package
To delete a package, use the command
pip uninstall package_name
Upgrade package
You can upgrade a package with the command pip upgrade package_name
.
Note that the upgrade command for pip is recursive, which means that the sub_package on which the new version of the package depends will be updated at the same time if a new version is available, even if the existing version already meets the requirements and you do not want to use the command.
pip install --upgrade --no-deps SomePackage
pip install SomePackage
The first line of commands updates the package but does not install dependencies, while the second line of commands follows the package that SomePackage needs but does not install.