I had create a Python package in my project with name text_analysis, and inside a class TextAnalysis with a method search_records
When I write a test in the main.py I can import TextAnalysis, but the ojbect (with IDE) doesn't show the method search_records. If I write the test.search_records(barcode) I got this error message:
Traceback (most recent call last):
File "C:..../main.py", line 19, in <module>
analysys = TextAnalysis(bib)
TypeError: 'module' object is not callable
Make sure that you are defining a class in TextAnalysis.py that contains the modules you want to call. If you would rather call individual modules without a class structure within TextAnalysis.py, call them as TextAnalysis.module_name().
Here is a simple example: Class vs Module structure
Related
I have a super simple python project, but I can't get it to work.
In the directory called "demo", I have a file called "demo.py", reading:
#!/usr/bin/python
from calc import plus
print(plus(1, 4))
I also have a sub-directory called "calc" with the following files:
__init__.py
minus.py
plus.py
The init.py is empty, whereas "minus.py" and "plus.py" read respectively:
def minus(a, b):
return a - b
and
def plus(a, b):
return a + b
When I run the demo.py, I get the error:
Traceback (most recent call last):
File "./demo.py", line 3, in <module>
print(plus(1, 4))
TypeError: 'module' object is not callable
It must be something absurdly simple, but I just can't figure it out.
Any help and advice would be highly appreciated.
Cheers
You need fix your import.
You have a file and one function with same name, then you need import the module(file) and after import the function.
from calc.plus import plus
Or custom your init.py
https://docs.python.org/3/tutorial/modules.html
I'm trying to instantiate a class in a submodule using a string name. I've been trying to follow this SO question unsuccessfully:
Python dynamic instantiation from string name of a class in dynamically imported module
I've created the following directory structure:
__init__.py
mymodule/
├── __init__.py
└── MyClass.py
MyClass.py contains:
class MyClass():
def __init__(self, someparam):
print(someparam)
From python I try the following which produces an error.
getattr(importlib.import_module('mymodule'), 'MyClass')
AttributeError: 'module' object has no attribute 'MyClass'
I've tried most of the other solutions put forth in the referenced question and not gotten any of them to work with this setup.
Here other failed attempts based on answers I've tried to follow to illustrate what I've tried and failed at:
import importlib
module = importlib.import_module('mymodule')
class_ = getattr(module, 'MyClass')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MyClass'
Your code is equivalent to:
from mymodule import MyClass
mymodule doesn't contain MyClass, so you will get an error. You want the equivalent of:
from mymodule.myclass import MyClass
That would be:
getattr(importlib.import_module('mymodule.MyClass'), 'MyClass')
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.
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
I'm new to python and now learning how to to import a module or a function, but I got these posted errors. The python code is saved under the name: hello_module.py
python code:
def hello_func():
print ("Hello, World!")
hello_func()
import hello_module
hello_module.hello_func()
error message:
Traceback (most recent call last):
File "C:/Python33/hello_module.py", line 9, in <module>
import hello_module
File "C:/Python33\hello_module.py", line 10, in <module>
hello_module.hello_func()
AttributeError: 'module' object has no attribute 'hello_func'
You cannot and should not import your own module. You defined hello_func in the current namespace, just use that directly.
You can put the function in a separate file, then import that:
File foo.py:
def def hello_func():
print ("Hello, World!")
File bar.py:
import foo
foo.hello_func()
and run bar.py as a script.
If you try to import your own module, it'll import itself again, and when you do that you import an incomplete module. It won't have it's attributes set yet, so hello_module.hello_func doesn't yet exist, and that breaks.
After changing the import as a from-import
i'm running into this error:
from datetime import datetime, date, timedelta
today = date.today()
from time import mktime
from feedparser import feedparser
import settings
def check_calendar():
d = feedparser.parse(settings.personal_calendar_feed)
for entry in d.entries:
if(date.fromtimestamp(mktime(entry.date_parsed))==today):
Traceback (most recent call last):
File computer.py", line 734, in <module>
check_calendar()
File "computer.py", line 210, in check_calendar
if(date.fromtimestamp(mktime(entry.date_parsed))==today):
AttributeError: 'function' object has no attribute 'fromtimestamp'
It says
AttributeError: 'function' object has no attribute 'fromtimestamp'
in the error. Apparently you may have a function named "date" in your code. Since Python allows you to use any name, and new conflicting names will override the older ones.
Instead, when python can't find a function from a module or object, it usually says type object has no attribute or module has no attribute, like if I want to call "fromtimes" :
type object 'datetime.date' has no attribute 'fromtimes'
You may want to check your code carefully again.
It is highly possible that you have redeclared date as function def date(): earlier in code. Otherwise it makes no sense.