Estuarine and Coastal Environment, Coastal Engineering, Coastal Management, Sustainability Science
Blog

Uncategorized

Import module or package in Python

Suppose that a main script of main.py (or a corresponding Jupyter Notebook) exists in the directory of myproject, and its subdirectory sub contains modules of mod_a.py (a function of func_a () included) and mod_b.py (a function of func_b() included) where main.py imports the module mod_a and the module mod_a imports the module mod_b. First, creating an empty file __init__.py in the directory sub so that sub is recognized as a package as shown below: myproject/   main.py   sub/     __init__.py     mod_a.py (func_a())     mod_b.py (func_b()) The sub-directory, sub, containing two modules, becomes a package when __init__.py is included. Import mod_a.py in main.py The following is …

Handling SQLite database using Python

Suppose that the file name of SQLite’s database is mpos_S.sqlite, its table (like a sheet of Excel) name is mpos_S, and mpos_S contains column data. If the column name of water temperature data is tp, this column tp contains water temperature data. In order to handle it from Python, you need to import sqlite3. First connect to the database and get the cursor cur. >>> import sqlite3 >>> conn = sqlite3.connect(‘mpos_S.sqlite’) >>> cur = conn.cursor() Check information of columns. >>> cur.execute("PRAGMA TABLE_INFO(mpos_S)")  ### Gets information about columns of table mpos_S >>> cols = cur.fetchall()  ### Information of a column is a tuple containing 6 elements. >>> print(cols) To get only …

PAGETOP