AttributeError: module 'discord' has no attribute 'Bot' - discord.py

When I run my code I get this error:
File "main.py", line 16, in <module>
bot = discord.Bot(command_prefix = "lol");
AttributeError: module 'discord' has no attribute 'Bot'
I am not totally sure what I have to change to get it to run either. Does someone happen to know what to do?

If you are trying to use the commands extension, you want to put from discord.ext import commands at the top of your code. Then, change discord.Bot to commands.Bot.

Related

Discord.py intents error, how do I fix this?

I am getting an Attribute Error containing the following:
File "bot.py", line 30, in <module>
intents = discord.intents.default()
AttributeError: module 'discord' has no attribute 'intents' ```
intents should start with a capitalised I(That means discord.Intents.default()
Sources:
in comments of your post — by Łukasz Kwieciński
#discord.Intents in discord.ext.commands api reference, as it said class discord.Intents(**kwargs)

AttributeError: 'module' object has no attribute 'testmod' Python doctest

When ever I try to doctest in python, basically whenever I run the code
if __name__ =="__main__":
import doctest
doctest.testmod()
I get this response from the interpreter
AttributeError: 'module' object has no attribute 'testmod'
I can run this code just fine, but whenever I run it on my windows machine, it doesn't work.
My machine is running Windows theirs is OS X, but are running python 2.7.5.
Thank you :)
Make sure that you are not trying to save your test file as doctest.py. The print statement suggested above will show it. If the file name is doctest.py, then rename it and try again.
AttributeError: 'module' object has no attribute 'testmod'
Clearly stats that the doctest module you're importing do not has the testmod() method.
Possible reasons can be:
You have more than one doctest modules in the lib.
and, it is the other one (without the testmod() method) which is getting imported as result of import doctest.
Solution: Look for the path of standard doctest module.
if __name__ =="__main__":
import doctest
if doctest.__file__ == "/path/to/standard/doctest-module":
doctest.testmod()
It looks like there is a different module called doctest that is being imported instead of the standard one.
To find out which module is being imported exactly, simply add the following print:
if __name__ =="__main__":
import doctest
print doctest.__file__ # add this
doctest.testmod()
The print should produce something similar to C:\Python27\lib\doctest.pyc, depending on the location and version of Python you're using. Any other output means you are importing the wrong module, and explain why you're getting the error.

Tkinter: NameError: name 'tk' is not defined

I have been following tutorials of how to create a graphical user interface (GUI), in order to get used to it because I will use it in the future. The majority of tutorials use these commands at the first lines:
from tkinter import *
root = tk()
root.title("Simple GUI")
root.geometry("200x100")
root.mainloop()
If I run this simple code I get the following error:
File
"C:/Users/Gerard/Dropbox/Master_Thesis_Gerard_Pujol/Python_Tryouts/creting_simpleGUI.py", line 11, in
root=tk()
NameError: name 'tk' is not defined
After that I changed my code, so I used something like that:
import tkinter as tk
root = tk()
root.title("Simple GUI")
root.geometry("200x100")
root.mainloop()
Now, the error is the following:
"C:/Users/Gerard/Dropbox/Master_Thesis_Gerard_Pujol/Python_Tryouts/creting_simpleGUI.py", line 11, in
root=tk()
TypeError: 'module' object is not callable
Do you know what's going wrong? Could you help me please?
I'm using Spyder for Python 3.3, but I suppose it isn't a problem.
The tutorials you've seen is probably for Python 2. In Python 3 they've changed the naming conventions. So instead of root = tk() in P2, it's root = Tk() in P3 (Tk() is a class, hence the capital T).
In your second example your should write root = tk.Tk() after the import statement
I've just had a similar problem which I found out was because my Python console window in Spyder was connected to a different .py file that I was working on earlier so I closed it and opened a new python console in Spyder and the problem was gone.

Can't load jinja2 with webapp2/Google App Engine

I'm going through the GAE webapp2 tutorial (runtime 2.7) using GoogleAppEngineLauncher on my Mac, and though I'm following everything exactly, I keep on getting an error at the point where we import the Jinja2 module: Using Templates - Google App Engine
Error:
Traceback (most recent call last): File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py",
line 168, in Handle handler =
_config_handle.add_wsgi_middleware(self._LoadHandler()) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py",
line 206, in _LoadHandler handler = import(path[0]) File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",
line 640, in Decorate return func(self, *args, **kwargs) File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",
line 1756, in load_module return self.FindAndLoadModule(submodule,
fullname, search_path) File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",
line 640, in Decorate return func(self, *args, **kwargs) File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",
line 1628, in FindAndLoadModule description) File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",
line 640, in Decorate return func(self, *args, **kwargs) File
"/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py",
line 1571, in LoadModuleRestricted description) File
"/Users/ykessler/Dropbox/appgrinders/gae_apps/helloworld2.7/helloworld.py",
line 9, in import jinja2 ImportError: No module named jinja2
So it for some reason can't find the module, even though it's supposed to be packaged as part of webapp2's extras. When I do a search on my file system it looks like it's there:
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2_extras/jinja2.py
And if I deploy the app to GAE, it works fine. Why would it be failing locally?
webapp2 packages its own jinja2 'glue' module, which is what you're importing, but it doesn't package jinja2 itself. If you want to use it, you'll need to install it in your system Python using easy_install, or put it in your app's directory.
Don't use the latest parameter on the app.yaml file, specify a version to prevent a highly unlikely but nonetheless possible incompatibility catastrophe.
Alias, specify 2.6, which is the only one supported, acording to the docs.
libraries:
- name: jinja2
version: "2.6"
Then, at your script, just import jinja2, as we're told by the docs.
I got this error too, to fix it I needed to add jinja2 to the bottom of my app.yaml file (as it says at the start of the tutorial .. )
First add the following to the bottom of helloworld/app.yaml:
libraries:
- name: jinja2
version: latest

Embedded Python loads module but does not load that module's internal import statements

At long last(!) I've compiled Boost::Python and have gotten my XCode project to import a local module. This module starts with the line from xml.dom import minidom, but when it executes, I'm given this error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "FeedStore.py", line 1, in <module>
from xml.dom import minidom
ImportError: No module named xml.dom
However, I know that I've installed the xml Python module -- when I open Python from my command prompt and type from xml.dom import minidom, everything goes smoothly. Moreover, when I import the module, it behaves as I would expect.
I suspected that there was something wrong with sys.path, so I compared the one I get from the prompt to the one that's being used in my embedded module. The only difference is that the embedded sys.path does not include ''. I've tried appending it, but that didn't change the behavior.
I also suspected that the embedded version was accessing a different version of Python than I was using from the prompt, but sys.prefix matched between both executions.
Here's the code that imports my module and runs it. It's pretty bare-bones at the moment (not even reference counting yet) because at this point I'd just like to make sure I'll be able to embed my module (I'm a total newbie C++ programmer).
Py_Initialize();
//PyRun_SimpleString("import sys");
//PyRun_SimpleString("sys.path.append('')"); //tried this to no avail!
PySys_SetPath("/Users/timoooo/Documents/Code/TestEmbed/"); //this allows me to import my local module
PyRun_SimpleString("import FeedStore as fs"); //here's where it whines about the lack of xml.dom
PyRun_SimpleString("store = fs.feedStore()");
PyRun_SimpleString("print store.next()");
Py_Finalize();
I'm probably misunderstanding something essential about boost::python. Can anyone help me out?
Despite having identical sys.path values, calling
PyRun_SimpleString("sys.path.append(\"<<path>>\")");
with the places I needed fixed the problem.

Resources