简体   繁体   English

如何在Python包中导入符号?

[英]How do I import symbols inside a Python package?

I have three database-related classes that I want to combine into a package, in the following structure: 我想将三个与数据库相关的类组合成一个包,结构如下:

adodb_pyodbc /
    __init__.py  # empty
    PyConnection.py
    PyRecordset.py
    PyField.py

This package is in my Lib/site-packages folder. 该软件包在我的Lib / site-packages文件夹中。

In an earlier iteration of this attempt, I did not use the "Py" prefix, and I got an error complaining that module.__init__() takes only two arguments, and three were being passed into it. 在此尝试的较早迭代中,我没有使用“ Py”前缀,并且在抱怨module.__init__()仅接受两个参数,并且向其中传递了三个参数时出错。 Someone suggested that the name "Recordset" might be conflicting with something else, so I changed it. 有人建议名称“ Recordset”可能与其他名称冲突,因此我将其更改。

The classes work when the files are in the same folder as the project that is using them. 当文件与使用它们的项目位于同一文件夹中时,这些类将起作用。 In that case, I can just use: 在这种情况下,我可以使用:

PyRecordset.py: PyRecordset.py:

from PyConnection import PyConnection
from PyField import PyField

class PyRecordset: pass

DerivedSet.py DerivedSet.py

from PyRecordset import PyRecordset

class DerivedRecordset(PyRecordset): pass

But the same files don't work when they are located inside a package. 但是,当它们位于包中时,相同的文件将不起作用。 My test program begins with this line: 我的测试程序从以下这一行开始:

from adodb_pyodbc import PyConnection as Connection

And when I run it I get this error message: 当我运行它时,我收到此错误消息:

C:\Python35\python.exe "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py"
Traceback (most recent call last):
  File "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py", line 8, in <module>
    from Level3_CoilsSet import Level3_CoilsSet
  File "C:\Customers\Nucor Crawfordsville\Scripts\64 bit\Testing\Level3_CoilsSet.py", line 1, in <module>
    from adodb_pyodbc import PyRecordset as Recordset
  File "C:\Python35\lib\site-packages\adodb_pyodbc\PyRecordset.py", line 9, in <module>
    from PyConnection import PyConnection
ImportError: No module named 'PyConnection'

But when editing PyRecordset.py inside PyCharm, it appears to be able to find the PyConnection.py file. 但是当在PyCharm中编辑PyRecordset.py时,似乎可以找到PyConnection.py文件。

I tried using relative addressing inside PyConnection.py: 我尝试在PyConnection.py中使用相对寻址:

from . import PyConnection
from . import PyField

But that puts me back to the __init__() error: 但这使我回到__init__()错误:

C:\Python35\python.exe "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py"
Traceback (most recent call last):
  File "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py", line 8, in <module>
    from Level3_CoilsSet import Level3_CoilsSet
  File "C:\Customers\Nucor Crawfordsville\Scripts\64 bit\Testing\Level3_CoilsSet.py", line 3, in <module>
    class Level3_CoilsSet(Recordset):
TypeError: module.__init__() takes at most 2 arguments (3 given)

How am I supposed to do this? 我应该怎么做?

Thanks very much for your help. 非常感谢您的帮助。 In the meantime, I'm going to take those files out of the package and put them back in my test project. 同时,我将从包装中取出这些文件,并将它们放回我的测试项目中。 I've wasted far too much time on this question. 我在这个问题上浪费了太多时间。

When you one to use PyConnection from outside of the package you have to either import it from the module where it was defined: 当您从包外部使用PyConnection ,必须从定义它的模块中导入它:

from adodb_pyodbc.PyConnection import PyConnection as Connection

Or, more conveniently, import it in the package init file adodb_pyodbc/__init__.py : 或者,更方便地,将其导入到程序包初始化文件adodb_pyodbc/__init__.py

from .PyConnection import PyConnection

And then, from the outside, you can just do: 然后,从外面,您可以执行以下操作:

from adodb_pyodbc import PyConnection as Connection

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

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