2017-04-26 9 views
0

私はpeewee(ORM)を使って小さなPython-MySQLアプリケーションを作成しています。次のように 私のコードは、単一のファイルで完璧に動作します:ピーウィーを使用して定義されているよう複数のPythonクラスを別々のメインPythonファイルにリンクするにはどうすればいいですか?

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:[email protected]: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") 

ユーザー& USERPROFILESがモデルです。これらのモデルを使って1つのファイルにエントリを作成することができます。

今、私は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:[email protected]:3306/db') 

db.connect() 
Users.create(username = 'testname', password = '@[email protected]@', 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:[email protected]: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:[email protected]: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内部のアクションを実行するためにmain.pyにu​​sers.pyとuserprofiles.pyをインポートするにはどうすればよいですか?

上記のpyファイルをインポートし、2つのモデルをリンクしてdbアクションを実行しようとしています。 私はコーディングの初心者です。 Pythonの

+0

users.pyとuserprofiles.pyの両方の内容が同じです。コピー/貼り付けの間違いだと思いますか?また、あなたは今までmain.pyで何を試しましたか? – zmo

+0

@zmoそれは間違いだった。私は私の質問を再編集します。 – thepassionatecoder

+0

[mcve]の作成方法を参照してください –

答えて

1

入門ディレクトリを作成し、空のファイルを作成__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:[email protected]:3306/db') 

    db.connect() 

    Users.create(username = 'testname', password = '@[email protected]@', mobile_number='1234567811') 

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

最後にあなたとあなたのコードを実行することができます

python that_pkg/main.py 

setup()機能内でsetup.pyを作成し、that_pkgをパッケージとして公開することもできます。

これまでに何が行われましたか?

我々はいくつかのモジュールが含まれthat_pkgと呼ばれるのpython パッケージ(Pythonのパッケージは、その中__init__.pyファイルとディレクトリ)を作成しました:users.pyuserprofiles.pymain.pyを。

最初の2つのモジュールはおおまかにモデルを記述しており、最後のモジュールはORMを積極的にインスタンス化しており、その中にデータベースとデータを設定しています。

最後に、あなたのコードを素敵で清潔にするには、create a setup.py fileとし、開発を容易にするためにvirtualenvを使用してください。

あなたはPythonパッケージに慣れていないので、私には、pipenvで始めることをお勧めします。これは、pipenfileの要件リストを維持しながら、virtualenvのような開発に必要な依存関係を維持するのに役立ちます。

Nota Bene:Pythonの初心者ですから、python2.7で学習を始めないでください。python3で学習を始めましょう。これは現在すべてのプラットフォームで広く利用できます。

+0

ありがとうございます。私のような初心者にとっては簡潔でした。 – thepassionatecoder

関連する問題