Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在一个单独的主python文件中链接多个python类?_Python_Python 2.7_Class_Python Module_Peewee - Fatal编程技术网

如何在一个单独的主python文件中链接多个python类?

如何在一个单独的主python文件中链接多个python类?,python,python-2.7,class,python-module,peewee,Python,Python 2.7,Class,Python Module,Peewee,我正在使用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

我正在使用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.py、users.py、userprofiles.py main.py-应调用users.py和userprofiles.py的主文件

我的主要朋友

My users.py:

My userprofiles.py:

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

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

创建一个目录,并创建一个空文件\uuuu init\uuuuu.py:

因此,您创建了一个新的python模块

然后创建该_模块/users.py:

然后创建_pkg/userprofiles.py:

最后创建_pkg/main.py:

最后,您可以使用以下命令执行代码:

python that_pkg/main.py
您还可以创建setup.py,并在setup函数中将该_pkg作为包公开

上面做了什么

我们已经创建了一个名为pkg的python包。python包是一个目录,其中包含一个uu init u u.py文件,其中包含几个模块:users.py、userprofiles.py和main.py

前两个模块只是松散地描述模型,最后一个模块是主动实例化ORM并填充数据库和其中的数据

最后,为了使您的代码美观、干净,您应该使用virtualenv,并且为了便于开发

由于您是python打包新手,我给您的建议是开始使用它,它将帮助您维护开发所需的依赖项,如virtualenv,同时在一个文件中维护需求列表


注:由于您是python新手,请不要开始使用python2.7学习,而要开始使用python3学习,python3现在在所有平台上都广泛可用。

users.py和userprofiles.py都有相同的内容。我认为这是复制/粘贴错误?还有,到目前为止你在main.py中尝试了什么?@zmo这是个错误。我将重新编辑我的问题。查看如何创建感谢。对于像我这样的初学者来说,它很简洁。
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
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
mkdir that_pkg 
touch that_pkg/__init__.py # unix command to create an empty file
# 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
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
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