An odd issue with copy module and Sublime Text2 - python

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.

Related

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

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.

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

Can't access full python module from file, but can from shell

Maybe I am completely missing something here but when I run this code form the shell it works:
import nltk
tokens = nltk.word_tokenize("foo bar")
and returns:
['foo','bar']
But when I but this into a file and execute it with python -u "path/to/file/myfile.py" it returns
AttributeError: 'module' object has no attribute 'word_tokenize'
I've tried reinstalling and every thing i can think of. Let me know if you need any more information.
Thanks in Advance!
You have more than likely called your file nltk.py so python is trying to import from that as opposed to the actual nltk module. Just rename your .pyfile.

python uuid weird bug

I first tried with the interpreter to produce uuid's with python's uuid module. I did the following:
>>>import uuid
>>>uuid.uuid1()
UUID('d8904cf8-48ea-11e0-ac43-109add570b60')
So far so good. I create a simple little function to produce the uuid's.
import uuid
def get_guid():
return uuid.uuid1()
if __name__ == '__main__':
print get_guid()
and I get the following error:
AttributeError: 'module' object has no attribute 'uuid1'
Ok...hmm...go back to the interpreter and now it too is broken. I get the same error running the same code I used to test this. I am baffled. What makes uuid break like this? And what is wrong with my code?
I am using python 2.6
Your test file name is most likely named uuid.py
When you went back to the interpreter, you launched the interpreter from the same directory, which by default, will first look for the module name to import in your current working directory.
Just change your test file name to something else, i.e. uuid_test_snippet.py

Categories