简体   繁体   English

Python导入多个脚本

[英]Python importing multiple scripts

I've been working on a python script that will require multiple libraries to be imported. 我一直在研究python脚本,该脚本需要导入多个库。

At the moment my directory structure is 目前,我的目录结构是

program/
    main.py
    libs/
        __init__.py
        dbconnect.py
        library01.py
        library02.py
        library03.py

My dbconnect.py which has the following contents 我的dbconnect.py具有以下内容

import psycopg2

class dbconnect:

    def __init__(self):
        self.var_parameters = "host='localhost' dbname='devdb' user='temp' password='temp'"

    def pgsql(self):
        try:
            var_pgsqlConn = psycopg2.connect(self.var_parameters)
        except:
            print("connection failed")

    return var_pgsqlConn

I am able to import and use this in my main.py using 我可以使用以下方法在main.py中导入并使用它

from libs.dbconnect import dbconnect

class_dbconnect = dbconnect()
var_pgsqlConn = class_dbconnect.pgsql()

This works as expected however I am trying to import all of the library scripts each which have similar contents to bellow 这按预期工作,但是我试图导入所有与以下内容相似的库脚本

def library01():
    print("empty for now but this is library 01")

I have added to my __init__.py script 我已经添加到我的__init__.py脚本中

__all__ = ["library01", "library02"]

Then in my main.py I tried to import and use them as bellow 然后在我的main.py中,我尝试将它们导入并用作波纹管

from libs import *

library01()

I am getting the following error 我收到以下错误

TypeError: 'module' object is not callable

I'll suppose content in your library0x.py are different (the functions/class have different names) 我想您的library0x.py中的内容是不同的(函数/类具有不同的名称)

The best way is to import all your subfiles content in the __init__.py 最好的方法是将所有子文件的内容导入__init__.py

# __init__.py
from .dbconnect import *
from .library01 import *
from .library02 import *
from .library03 import *

Then you can use the following : 然后,您可以使用以下命令:

from libs import library01, library02

If you want to restrict for some reasons importation with the wildcard ( * ) in your library0x.py files, you can define a __all__ variable containing all the names of the function you will import with the wildcard : 如果出于某些原因要限制在library0x.py文件中使用通配符( * )导入,则可以定义一个__all__变量,其中包含将使用通配符导入的函数的所有名称:

# library01.py

__all__ = ["library01"]

def a_local_function():
    print "Local !"

def library01():
    print "My library function"

Then, by doing from .library01 import * , only the function library01 will be import. 然后,通过from .library01 import * ,仅将导入函数library01


EDIT: Maybe i missunderstand the question : here are some ways to import the function library01 in the file library01.py : 编辑:也许我误解了这个问题:这是在文件library01.py导入函数library01一些方法:

# Example 1:
from libs.library01 import library01
library01()

# Example 2:
import libs.library01
libs.library01.library01()

# Example 3:
import libs.library01 as library01
library01.library01()

In your case library01 is a module which contains a function named library01 . 在您的情况下, library01是一个模块 ,其中包含一个名为library01函数 You import the library01 module and try to call it as a function. 您导入library01模块并尝试将其作为函数调用。 That's the problem. 那就是问题所在。 You should call the function like this: 您应该像这样调用函数:

library01.library01()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM