Make writing a habit together! This is the 13th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.

Packages in Python and package import

Packages are folders, and packages can contain subpackages and subfolders. Python scripts are modules, and modules contain classes and functions. Python packages must contain an __init__.py file. The file contents are arbitrary, and without __init__

Create three Python packages, alpha, Bravo, and Charlie. Using PyCharm to create a Python Package will automatically include __init__.py

Create a new alpha module under the alpha package and add functions

def walk() :
    return 'Walk function called in alpha module under alpha package'

def run() :
    return 'Run function called in alpha module under alpha package'
Copy the code

Create a new Bravo module under the Bravo package and add functions

def rush() :
    return 'Rush function called in bravo module under Bravo package'

def run() :
    return 'Run function called in bravo module under Bravo package'
Copy the code

Create a Charlie module under the Charlie package and add category global variables

heros = ['stark'.'banner'.'thor']

heros_org_name = 'Avengers'

class Hero:

    def __init__(self, name) :
        self.name = name

    def __str__(self) :
        return 'The Hero object in the Charlie module under the Charlie package is instantiated'
Copy the code

Package and module imports

Import a package or module from Python into the current script using the from and import keywords. After the module is imported, you can use the function classes or variables of the imported module in the current file.

Import modules from the command line

Create a new test.py directory at the same level as the lilith package

def hello() :
    print('hello')
Copy the code

In this directory, run the command to go to the Python3 running interface, and use the import keyword to import

Create a lilith. Py script file in the lilith package and add lilith functions to the file

def lilith() :
    print('Lilith function called in lilith module under lilith package')
Copy the code

To reference the LILith function in the test.py file, you must import the LILith module using the from import keyword and call the lilith function from the lilith module

from lilith import lilith
lilith.lilith()
Copy the code

Multistage import

The lilith module also contains alpha, Bravo, and Charlie packages. To import alpha, Bravo, and Charlie packages into the test.py file, you need to import these packages into lilith module. Import statements can be written in an __init__.py file under the LILith package

from .alpha import alpha
from .bravo import bravo
from .charlie import charlie
Copy the code

Import the alpha package in the test.py file

from lilith import alpha
print(alpha.walk())
Copy the code

Alpha and Bravo modules have run functions, which can be renamed using the AS keyword

from lilith.alpha.alpha import run as alpha_run
from lilith.bravo.bravo import run as bravo_run
print(alpha_run())
print(bravo_run())
Copy the code

The import statement is very long. The first step is to import the classes and variables in the Charlie module

from .charlie.charlie import Hero, heros, heros_org_name
Copy the code

Import the required variables and classes directly into the current LILith package, so that you can import these variables directly from the LILith module in the test.py file

from lilith import Hero, heros_org_name, heros

hero = Hero('stark')
print(hero)
print(heros_org_name)
for i in heros:
    print(i)
Copy the code

Third party packages/libraries used in Python

There are many third-party libraries with complete functions in Python, which can greatly improve the development efficiency. Some third-party libraries are built with Python, and some are not built with Python. You need to use the PIP installation tool to install them. Using official Python sources is very easy to time out and fail the installation

Pip3 install the name of the third party libraries -i https://pypi.tuna.tsinghua.edu.cn/simpleCopy the code

Datetime module

Datetime is a common library that can be used to obtain the current time, time interval, etc. It also supports conversion between time objects and strings

The Datetime library needs to be imported for use

import datetime

Get the current time
now_time = datetime.datetime.now()
print(now_time)

Instantiate a time object
datetime_obj = datetime.datetime(2022.4.14.17.00.0000)
print(datetime_obj)
print(type(datetime_obj))
Copy the code

from datetime import datetime
from datetime import timedelta

diff_time = timedelta(weeks=0, days=1, hours=0, minutes=0, seconds=0, microseconds=0)

last_day = datetime.now() - diff_time
print(last_day)
print(type(last_day))
Copy the code