I'm programming a game with pygame, but when I try to use the python's math module, my program tries to use pygame.math instead. How can I solve this issue?
Example:
import pygame, math
from pygame import *
pygame.init()
a = math.sin(90)
# some code that uses a
Error message:
AttributeError: module 'pygame.math' has no attribute 'sin'
The error might be because of from pygame import *, but my code doesn't work without it.
Solved in the commments:
Use the code as following, as it will prevent errors:
import pygame
import math
pygame.init()
a = math.sin(90)
This way it imports as separate modules. You had it saying:
Import Pygame and Pygame Math
Initiate Pygame
define variable "a"
I use pygame and math in my code but I have them as separate imports:
import pygame
import math
And everything works as expected without error
Related
I am trying to enable 4x multisampling for OpenGL in Python. But I keep getting an error saying "GFLW_SAMPLES" is not defined. The way I understand it this should be included in glfw.
from OpenGL.GL import *
import glfw
import sys
glfw.window_hint(GLFW_SAMPLES, 4)
Is there another library I have to import for this?
You have 2 possibilities:
import glfw
glfw.window_hint(glfw.SAMPLES, 4)
or
from glfw.GLFW import *
glfwWindowHint(GLFW_SAMPLES, 4)
Is there any difference between coding:
import turtle
and coding
from turtle import *
(Using module turtle as an example, I suppose the answer will be true for all modules.)
My understanding was that the * imports all methods in the module, so why use the longer syntax?
I'm trying to make a turtle game on Repl.it and I don't know why this error keeps coming up. Here is my code:
import turtle
wn = turtle.Screen()
wn.bgcolor("white")
wn.setup(width=800, height=800)
wn.tracer(0)
# Set up the ground
ground = turtle.Turtle()
ground.color("white")
ground.shape("square")
ground.speed(0)
ground.shapesize(stretch_wid=200, stretch_len=20)
ground.speed(0)
ground.color("black")
ground.penup()
ground.goto(0, -400)
ground.direction = "stop"
It seems that the implementation of the turtle library on repl.it is somewhat limited and not all commands are available. You can either run it locally to use all commands or remove incompatible commands.
Source: https://repl.it/talk/ask/Turtle-python-resizing/7312
There is actually a way to do this.
Whenever you create a new repl instead of creating a "python with turtle" repl just create a normal python one. Then import the turtle module and it should work. When importing it on a basic python file it imports everything.
Whenever you do import turtle it is also a good idea to add from turtle import * that way you import everything.
I also had this problem
You can use Python with pygame instead of python with turtle because for some reason, it works
I'm trying to use Turtle, but for some reason, it won't let me import it. I've tried from turtle import * (and that works) but if I try print(dir(turtle)) or to use any functions, I get an error saying turtle is not defined.
from turtle import Turtle doesn't work but print(dir(Turtle)) after using from turtle import * does work. However, prefixing commands with Turtle. ie Turtle.color("red") doesn't work.
Also, the demonstrations in the turtledemo folder work. I'd really appreciate any help
This imports turtle into the main namespace and is used as follows:
import turtle
turtle.something()
Thus, you now have an identifier in your main namespace, named turtle.
This imports all visible identifiers from turtle into the main namespace
and is used differently:
from turtle import *
something()
In this scenario, turtle is not in the main namespace. Its contents
are. Thus, dir(turtle) will fail, because that identifier isn't there.
Are you sure that from turtle import Turtle doesn't work? It worked
for me.
If you import all from turtle then you have to use the Turtle object directly, like this:
from turtle import *
print(dir(Turtle))
and not like this:
from turtle import *
print(dir(turtle))
I thought that
from tkinter import *
imports all names into my current file's namespace so that I can access all of it directly. However, I get an error on instantiating a message box:
messagebox.showinfo("Something")
Once I add
from tkinter import messagebox
all works fine. I don't understand why. Didn't the first import statement already import all names in the tkinter module including messagebox?
Importing a module (tkinter) does not automatically import submodules (tkinter.messagebox) unless the module do it explicitly for you.
messagebox is a submodule of tkinter.
You should import module "messagebox"
(use "import ... as ..." to make it shorter)
import tkinter.messagebox
tkinter.messagebox.showinfo("Something")
Or as you figured out yourself,
from tkinter import messagebox
Since messagebox is a file inside the Tkinter module, you won't be able to access it by just calling Tkinter. To import the submodules you have to call out the specific files like this:
from tkinter import messagebox