AttributeError: module 'x' has no attribute 'y' - python

I'm trying to move a python function over to Google Cloud Functions, and I seem to be having an issue with aliases.
I have a module with the following in its init;
import tweetBot.generator, tweetBot.poster
# Add aliases when this module is imported
tweetBot.ebooksGen = tweetBot.generator.ebooksGen
tweetBot.genreGen = tweetBot.generator.genreGen
tweetBot.variantGen = tweetBot.generator.variantGen
tweetBot.postTweet = tweetBot.poster.postTweet
It's then imported with a simple import tweetBot.
Running this on my local machine, it works perfectly and the function has absolutely no issues. The output is all correct, and everything is fine.
However, when running this using Google Cloud Functions, I get the following error;
AttributeError: module 'tweetBot' has no attribute 'genreGen'
This makes absolutely no sense to me, since it is absolutely defined in the import. In addition, I know this works because it's working absolutely fine on my local machine from the exact same repo.
https://github.com/Jademalo/JadeBots/tree/gcp-functions
Ignore the code being terrible, at the very least it works. I simply don't understand why I'm having an attribute error running it on Google Cloud when it's absolutely fine locally.
Thanks
EDIT: After replacing the calls for the aliases with the originals (such as tweetBot.generator.genreGen, I'm still having the same issue;
File "/workspace/JadeBots.py", line 18, in postGenreDefining
tweetText, altGenreGameDebug, altGenreExtraDebug, gameText, genreText, altPostDebug = tweetBot.generator.genreGen(gameFile, genreFile, genreExtraFile, altPostFreq, altGenreGameFreq, altGenreExtraFreq)
AttributeError: module 'tweetBot' has no attribute 'generator'

It seems that Google Cloud Functions isn't downloading the submodule when creating the function.
Downloading the source results in the submodule folder being empty, which explains why the arrtibute error is happening.

Related

Python not able to import module

I have quite a number of different file which I want to use to create an API. The files are :
app.py -- main file
utils.py -- utilities file
recorder.py
consts.py
submitter.py
All of these files have a dependency on one another. However, when I try to import them in a particular file ( for ex - importing consts in recorder.py, I am getting error saying The module consts is not found.
I am importing it using :
from .consts import consts
Can someone tell me what am I doing wrong here, and how to solve this issue.
If i import it in the main app.py, it doesn't give any error. but it does when I want to access any consts in other files.
Thank you in advance
EDIT : Here is the folder structure :
When you say they are interdependent, that may be your issue. You cannot have two files that depend directly on one another with out some hacky solutions as youll run into the problem of importing one that tries to import the importer which tries to import the importee and so on forever.
Edit: i misunderstood how imports work, the problem comes from:
"If you import X from your main program, Python will load the code for
X and execute it. When Python reaches the import Y statement, it loads
the code for Y, and starts executing it instead.
At this time, Python has installed module objects for both X and Y in
sys.modules. But X doesn’t contain anything yet; the def spam
statement hasn’t been executed."
As stated in the link i included at the bottom of this response. The link also gives some possible solutions like changing when you import the different modules.
What we really need is to see the code for the files to understand how you are importing everything
this link should help you in your desire for circular dependency.

The error was:call of non-function ('module' object)

I am stuck using JES for this particular project, and yes, I would rather be poked in the eye. In the mean time, I am stuck with this interesting issue where I cannot load my python package in JES. I was told that this strategy would work. When I give dummy definitions to all the JES functions, and then run the Project in Pycharm, everything works "fine" (given the expected behavior of the dummy functions). I can't figure out what exaclty is going on with JES.
Does anyone have any experience with loading custom python packages in JES?
Project Structure
Project
main.py
MyPackage
__init__.py
allmygoodies
Main.py
""" getMediaPath() and setMediaPath() are built in JES functions """
import sys
setMediaPath() # Setting it to project root
sys.path.append(getMediaPath()) # Appending Project Root
sys.path.append(getMediaPath() + "MyPackage") # Also tried this
from MyPackage import * # Importing everything from package
myObject = MyObject()
Error
The error was:call of non-function ('module' object)
Inappropriate argument type.
An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer.
In order to accomplish this, I had to switch to JES 5+. I was previously using JES 4 which does not support this ability.

weakref module has no attribute 'weakvaluedictionary'

There came up strange error from python today. Whatever i want to launch or do, i can't getting error : 'module' has no attribute 'weakvaluedictionary'.
Even tried to launch pip install/uninstall and got same error.
Nothing has been changed from last day, and yesterday everything was working perfectly.
I checked init.py and did not see anything strange with weakref:
there is import weakref and _handlers = weakref.WeakValueDictionary() #map of handler names to handlers lines.
Please help!!
I was having same issue than you. The problem was that I was naming the file I was trying to run/edit as weakref.py Then, only change the name. I changed name to "weakref_example.py"

Problems when converting from imp to importlib in python 3.4

I've made a Python application which can load plugins. These plugins are loaded based on a name and path.
I am currently using
pluginModule = imp.load_source(pluginModuleName, pluginModulePath)
and then getting a class instance in the module this way
# Load the module class and initialize it.
if hasattr(pluginModule, pluginClassName):
try:
pluginClassInst = getattr(pluginModule, pluginClassName)()
except Exception as e:
errorMsg = ('In plugin module [{}], {}'.format(os.path.basename(pluginModulePath), e))
exceptionTracePrint(self._log)
self._log.error(errorMsg)
continue
Since the imp lib is deprecated I want to use importlib. And the only similar method of getting my class instance was to use
pluginModule = importlib.machinery.SourceFileLoader(pluginModuleName, pluginModulePath).load_module()
The weird thing here is that (I am using pyCharm as IDE). when I run my code in debugging mode the above command works fine and I get my class instance. however running the code normally gives me the following error.
pluginModule = importlib.machinery.SourceFileLoader(pluginModuleName, pluginModulePath).load_module()
AttributeError: 'module' object has no attribute 'machinery'
Why is there a difference between run and debug.
Is there an alternative way of doing what I want.
Ive also tried
pluginModuleTmp = importlib.util.spec_from_file_location(pluginModuleName, pluginModulePath)
Which also gives me the correct data however I cannot load the module this way or at least I do not know how
Regards
Anders
Found the solution. Apparently in debug mode a lot more modules are imported behind my back. I fixed it by adding the import.
import importlib.machinery
Regards
Anders

An odd issue with copy module and Sublime Text2

Something strange is happening. I am getting an error when running this code in Sublime Text 2 while the code is valid elsewhere.
import copy
s = 'string'
cs = copy.copy(s)
print s == cs
I got the TypeError: 'module' object is not callable
Also, copy.deepcopy() throws an error AttributeError: 'module' object has no attribute 'deepcopy' while running inside of ST2.
I am aware this is the ST2 specific problem, but maybe some of you know whether this can be solved?
It looks like you've masked the built-in copy module my adding your own copy module somewhere in the module search path used by sublimetext2.
To fix that rename your copy.py file to something else and also delete the copy.pyc file.
The location of the file can be found using __file__ attribute of the module object.
import copy
print copy.__file__
In future please don't name your modules or packages same as python built-in modules, otherwise you'll face same issues.

Categories