Using Python #external packages

Packages and modules

Packages and modules correspond to the relationship between files and directories. A module can contain multiple modules, whereas a module points to a single file. The program can be structured on a file-by-file basis.

There are many external packages in Python in addition to the standard packages. This external package is imported and used.

Module import

def printmsg():
    print('Hello, World')

By using the ʻimport` statement, the above file can be read as a module.

import file_test1

file_test1.printmsg()

Variables in the module

When a global variable is defined in a module, the variable in the module will not be rewritten even if a variable with the same name is defined in the import destination. That is, the variables defined in the module are valid for each module.

msg = 'Hello, World'
def printmsg:
    print(msg)

The above file defines the global variable msg.

import file_test2

msg = 'Goodbye, Python'
file_test2.printmsg()

Execution result

Hello, World

The variable msg is also defined in the import destination, but it is distinguished from the variable msg in the module.

Test the module

Files used as modules can also be written as one Python program, so each can be executed individually. When executing a Python program individually, __main__ is assigned to the variable __name__, so you can write the test code as follows.

def printmsg:
    print('Hello, World')

if __name__ == '__main__':
    print('test')

Creating a package

To package the directory, you need to create a file named __init__.py. In __init__.py, describe the names of the modules included in the variable __all__ as a list. For example, if you want to include modules named test3.py and test4.py in the same directory as __init__.py in one package, __init__.py is written as follows ..

__all__ = ['test3.py', 'test4.py']

List the elements in the package

If you want to list the elements included in the package, you can use the function dir (). Import the package and specify the package name as an argument.

Recommended Posts

Using Python #external packages
Start using Python
Scraping using Python
Operate Redmine using Python Redmine
Python packages and modules
Data cleaning using Python
Install python external library
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
python external file reading
Try using Tweepy [Python2.7]
Study from the beginning of Python Hour8: Using packages
Python notes using perl-ternary operator
Flatten using Python yield from
Scraping using Python 3.5 async / await
Save images using python3 requests
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Try using Kubernetes Client -Python-
Understand Python packages and modules
Execute external command in python
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
Scraping using Python 3.5 Async syntax
Post to Twitter using Python
Install external libraries with Python
Start to Selenium using python
Search algorithm using word2vec [python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Create JIRA tickets using Python
Instrument control using Python [pyvisa]
Roadmap for publishing Python packages
Manipulate spreadsheets locally using Python
Python memo using perl --join
Web scraping using Selenium (Python)
ambisonics simulation (external problem) Python
[Python] I tried using OpenPose
[Python] JSON validation using Voluptuous
External command execution in Python
Broadcast on LINE using python
Data analysis using python pandas
Translate using googletrans in Python
Using Python mode in Processing
Using OpenCV with Python @Mac
[Python] Shooting game using pyxel
Send using Python with Gmail
Complement python with emacs using company-jedi
How to install python using anaconda
Initializing global variables using Python decorators
[Python] Loading csv files using pandas
[Package cloud] Manage python packages with package cloud
Retry post request using python requests
Python Note: About comparison using is
[Ubuntu] [Python] Object tracking using dlib
MIDI packages in Python midi and pretty_midi
Image capture of firefox using python