Module import and exception handling in python

Creating an environment for importing Python modules

  1. Make a Derrek Lee package in the main folder
  2. Create a directory folder and files in the directory package. Be sure to create init.py. Because Python recognizes this file and calls the module
  3. Make utils.py in it Create two types of files, a.py b.py, in 4 and 3. To call a function defined later
  4. Next, create another directory folder in the directory package.
  5. Similarly, create init.py. Exception handling is to handle errors even if old packages or new packages are mixed.

Code that calls the module

from directly_package.directlyfolder1 import a.py //a.py is in directly folder1
from directly_package.directlyfolder2 import b.py
print(folder1.function()) //Function execution
print(folder2.function()) 

Below is the contents of the files in the directory folder and the path specification. It is necessary to change the path specification depending on the conditions.

Example of module path specification code

from directly_package.directly folder import utils //Absolute path
def x(): //Any function definition x
return'x'
def y():Let y be the definition of any function
    return utils.z('y') //utils.The def defined in py.Described in return utils as z

Exception handling example

try:
    from directly_package import utils
except ImportError:
    from directly_package import utils
utils.any_function('word')

To try tomorrow

Create setup.py and try to create your own library.

Recommended Posts

Module import and exception handling in python
Python module import
Python exception handling
Python exception handling
Implementation module "deque" in queue and Python
Import Error in Python3: No module named'xxxxx'
Handling json in python
python original module import
Exception message in Python
Python, about exception handling
Hexadecimal handling in Python 3
Import cv2 ModuleNotFoundError: No module named'cv2' in python3
Python exception handling (Python learning memo ⑥)
How Python module import works
Stack and Queue in Python
Python debug and test module
Unittest and CI in Python
Relative url handling in python
Sample of getting module name and class name in Python
[Python of Hikari-] Chapter 08-03 Module (Import and use of standard library)
Python module num2words Difference in behavior between English and Russian
MIDI packages in Python midi and pretty_midi
Difference between == and is in python
[Python] logging in your own module
View photos in Python and html
Sorting algorithm and implementation in Python
Python class definitions and instance handling
Manipulate files and folders in Python
About dtypes in Python and Cython
Exception handling during Python API communication
Handling of JSON files in Python
Cooperation between python module and API
[Code] Module and Python version output
About Python, from and import, as
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Python3 socket module and socket communication flow
Python unittest module execution in vs2017
Function synthesis and application in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Master the weakref module in Python
What to do if you get Swagger-codegen in python and Import Error: No module named
I get "sanity check" and "No module" errors in import numpy
Exception handling
Overlapping regular expressions in Python and Java
I tried to summarize Python exception handling
Differences in authenticity between Python and JavaScript
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Line graphs and scale lines in python
Comparison of Japanese conversion module in Python3
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)