1

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.

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. Someone suggested that the name "Recordset" might be conflicting with something else, so I changed it.

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:

from PyConnection import PyConnection
from PyField import PyField

class PyRecordset: pass

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.

I tried using relative addressing inside PyConnection.py:

from . import PyConnection
from . import PyField

But that puts me back to the __init__() error:

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.

Mad Physicist
  • 76,709
  • 19
  • 122
  • 186
ROBERT RICHARDSON
  • 1,610
  • 3
  • 16
  • 40
  • Provide a sample PyConnection.py – Mad Physicist Jan 27 '17 at 15:26
  • Looks like the error `TypeError: module.__init__() takes at most 2 arguments (3 given)` is caused by the fact that you have modules and classes with the same name and basically got confused as to which is which: http://stackoverflow.com/a/14584837/2988730. Remember that the class `PyConnection` in the module `PyConnection` is referenced as `PyConnection.PyConnection` unless you import like `from adodb_pyodbc.PyConnection import PyConnection`. – Mad Physicist Jan 27 '17 at 15:28
  • Possible duplicate of [TypeError: module.\_\_init\_\_() takes at most 2 arguments (3 given)](http://stackoverflow.com/questions/14583761/typeerror-module-init-takes-at-most-2-arguments-3-given) – Mad Physicist Jan 27 '17 at 15:31

1 Answers1

2

When you one to use PyConnection from outside of the package you have to either import it from the module where it was defined:

from adodb_pyodbc.PyConnection import PyConnection as Connection

Or, more conveniently, import it in the package init file adodb_pyodbc/__init__.py:

from .PyConnection import PyConnection

And then, from the outside, you can just do:

from adodb_pyodbc import PyConnection as Connection
jdehesa
  • 52,620
  • 6
  • 60
  • 94