如何在单独的主 python 文件中 link 多个 python 类?

How to link multiple python classes in a separate main python file?

我正在使用 peewee (ORM) 创建一个小型 Python-MySQL 应用程序。 我的代码在单个文件中完美运行,如下:

import os

from peewee import *
from playhouse.db_url import connect

# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')

db.connect()

class Users(Model):
    users_id = PrimaryKeyField()
    username = CharField()
    password = CharField()
    mobile_number = CharField()
    created_at = DateTimeField()
    updated_at = DateTimeField()

    class Meta:
        database = db

class User_profiles(Model):
    users_id = IntegerField()
    user_profiles_id = PrimaryKeyField()
    profile_name =  CharField()
    address = CharField()
    created_at = DateTimeField()
    updated_at = DateTimeField()    

    class Meta:
        database = db

Users.create(username = "Adam", password = "Dummy1", mobile_number = "1234567891")
User_profiles.create(users_id=4,profile_name="shop", address="Delhi")

用户和用户配置文件是使用 peewee 定义的模型。我能够在单个文件中使用这些模型创建条目。

现在我想把它分成 3 个文件:main.pyusers.pyuserprofiles.py main.py - 应该调用 users.py 和 userprofiles.py

的主文件

我的main.py

import os

from peewee import *
from playhouse.db_url import connect

# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')

db.connect()
Users.create(username = 'testname', password = '@88@@', mobile_number='1234567811'):

我的users.py:

import os

from peewee import *
from playhouse.db_url import connect

# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
# db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')

db.connect()

class Users(Model):
    users_id = PrimaryKeyField()
    username = CharField()
    password = CharField()
    mobile_number = CharField()
    created_at = DateTimeField()
    updated_at = DateTimeField()

    class Meta:
        database = db

我的userprofiles.py:

import os

from peewee import *
from playhouse.db_url import connect

# Connect to the database URL defined in the environment, falling
# back to a local MySql database if no database URL is specified.
# db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')

# db.connect()

class User_profiles(Model):
    users_id = IntegerField()
    user_profiles_id = PrimaryKeyField()
    profile_name =  CharField()
    address = CharField()
    created_at = DateTimeField()
    updated_at = DateTimeField()    

    class Meta:
        database = db

如何在 main.py 中导入 users.py 和 userprofiles.py 以使用 peewee 在 main.py 中执行操作?

我正在尝试导入上述 py 文件并通过链接两个模型来执行数据库操作。 我是编码新手。开始 Python

创建一个目录,并创建一个空文件__init__.py:

mkdir that_pkg 
touch that_pkg/__init__.py # unix command to create an empty file

所以你已经创建了一个新的 python 模块 that_pkg

然后创建that_module/users.py:

# always make explicit includes
from peewee import Model, PrimaryKeyField, CharField, DateTimeField

class Users(Model):
    users_id = PrimaryKeyField()
    username = CharField()
    password = CharField()
    mobile_number = CharField()
    created_at = DateTimeField()
    updated_at = DateTimeField()

    class Meta:
        database = db

然后创建that_pkg/userprofiles.py:

from peewee import Model, PrimaryKeyField, CharField, DateTimeField

class User_profiles(Model):
    users_id = IntegerField()
    user_profiles_id = PrimaryKeyField()
    profile_name =  CharField()
    address = CharField()
    created_at = DateTimeField()
    updated_at = DateTimeField()    

    class Meta:
        database = db

最后创建 that_pkg/main.py:

import os
from playhouse.db_url import connect

from that_pkg.users import User
from that_pkg.userprofiles import User_profiles

def main():
    db = connect(os.environ.get('DATABASE') or 'mysql://testdb:testdb@127.0.0.1:3306/db')

    db.connect()

    Users.create(username = 'testname', password = '@88@@', mobile_number='1234567811')

# code that will be executed when you run this file directly:
if __name__ == "__main__":
    main()

最后你可以执行你的代码了:

python that_pkg/main.py

您还可以创建一个 setup.py,并在 setup() 函数中将 that_pkg 作为包公开。

上面做了什么?

我们创建了一个名为 that_pkg 的 python (python 包是一个包含 __init__.py其中的文件)包含几个模块:users.pyuserprofiles.pymain.py.

前两个模块仅粗略地描述您的模型,最后一个模块主动实例化 ORM 并填充数据库和其中的数据。

最后,为了使您的代码美观整洁,您应该 create a setup.py file,并且为了便于开发,请使用 virtualenv。

由于您是 python 打包的新手,我对您的建议是从 pipenv 开始,这将帮助您维护开发所需的依赖项,例如 virtualenv,同时维护列表pipfile 中的要求。

Nota Bene:由于您是 python 的新手,请不要从 python2.7 开始学习,而是从 python3 开始学习,它现在已在所有平台上广泛使用平台。