blender game engine import error - python

i just started learning blender and its scripting and tried to run its sample code
import bge
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
sens = cont.sensors['mySensor']
actu = cont.actuators['myActuator']
if sens.positive:
cont.activate(actu)
else:
cont.deactivate(actu)
main()
and get the following error:
ImportError: No module named 'bge'
i searched for a solution but couldn't find any.how to solve this problem?
i am using blender 2.65

Running import bge does not work when you press "Run script" or try to issue this command in the terminal. You have to press "P" to activate game engine mode. To run your script, connect a controller to the script.

import 'bge' must be 'blender game', rather than blender rendering
give an 'always' run python scripts in 'game logic editor'
start game

Related

Pyinstaller "Failed to execute script due to unhandled exception" Error

I made a simple program that works as a .py file, but when I use pyinstaller to make a .exe file, the command prompt opens and closes immediately. By screen recording opening the .exe, I was able to see the following error flash on the command prompt before it closed: "Failed to execute script "kahoot_spammer" due to unhandled exception!"
I am using pyinstaller --onefile kahoot_spammer.py to convert my .py to a .exe file. Here is the code in my program; which I plan on using at school to add a ton of users to the class Kahoot game:
from pynput.mouse import Button, Controller as mController
from pynput.keyboard import Key, Controller as kController
import time
mouse = mController()
keyboard = kController()
#ask user for the kahoot game pin, what name they want the fake players to have, and how many players to create
game_code = input("Game Code: ")
username = input("Username: ")
amount = input("Player Amount: ")
#delay the program so the user can switch to their browser with kahoot open
time.sleep(5)
#create players
for i in range(amount):
#enter game pin
mouse.position = (775, 500)
mouse.click(Button.left, 1)
keyboard.type(game_code)
mouse.position = (775, 550)
mouse.click(Button.left, 1)
time.sleep(0.5)
#enter username
mouse.position = (775, 520)
mouse.click(Button.left, 1)
keyboard.type(username + str(i))
mouse.position = (775, 570)
mouse.click(Button.left, 1)
time.sleep(0.2)
#open new tab with kahoot ready to go
keyboard.press(Key.ctrl)
keyboard.press("t")
keyboard.release(Key.ctrl)
keyboard.release("t")
keyboard.type("https://kahoot.it")
keyboard.press(Key.enter)
keyboard.release(Key.enter)
time.sleep(1)
Does anyone know how I can fix this error, and have the program run as it should? The .pyc file that is created alongside the .exe works as it should; the command prompt stays open and the program functions as expected.
I would suggest using pyinstaller --onefile --debug=all kahoot_spammer.py to get more debug information.
In my specific case my application failed when importing a logging object associated with my GUI library Kivy. So I commented out the line in my code that turned logging off and it works now.
This link helped me: https://pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html

Python Script as window service need to start on boot

I want to make my python script run in the background as well as it needs to start on boot.
i'm using windows7 system
python 3.6
I referred this link to make python script as window service. But when i start window service, its showing error:
The service is not responding to the control function.
I have used SYSTEMD in linux to daemonize python script and also to manage processes. Is there any alternative for systemd ?
in windows so that I can go for that.
Python script which i want to run is :
My python script:
import schedule
import time
import datetime
#datetime.datetime.now().strftime("%H:%M")
def job():
print("Scheduling is Working...")
createfile()
def createfile():
company = "HELLO USER"
with open('company.txt', 'w+') as f:
f.write(str(company))
print("File Created on:",time.ctime(time.time()))
f.close()
return True
# schedule.every(10).minutes.do(job)
# schedule.every().hour.do(job)
#schedule.every().day.at("11.40").do(job)
schedule.every(1).minutes.do(job)
while 1:
schedule.run_pending()
time.sleep(1)
Use the Task Scheduler to run the script on startup.
https://www.pingshiuanchua.com/blog/post/scheduling-a-python-script-to-run-upon-boot-at-a-predefined-time

Infinite loop in python causes Quickly App to freeze

I'm trying to develop an Ubuntu App using Quickly. When I run an infinite loop in python the Ubuntu App freezes and stops responding. The following is the code I used:
# Code for other initialization actions should be added here.
self.startbutton = self.builder.get_object("startbutton")
def on_startbutton_clicked(self,widget):
while 1 :
if "100" in open("file1.txt").read():
print "success"
exit ()
When I write 100 into file.txt, the terminal prints success but the Ubuntu App will still not respond. So what should I change in my code so that my App won't freeze?
I have changed my while loop
flag=1
while flag :
fp = open("file1.txt","r")
if fp.read()=="100":
print "success"
flag=0
fp.close()
Even then the App freezes

Prevent OS X from going to sleep with Python?

Is there a way to prevent a computer running OS X from going to sleep from within a Python script?
You can use the built-in caffeinate command.
subprocess.Popen('caffeinate')
This is how I use it:
import sys
import subprocess
if 'darwin' in sys.platform:
print('Running \'caffeinate\' on MacOSX to prevent the system from sleeping')
subprocess.Popen('caffeinate')
You can also run caffeinate in an external terminal window and leave it open to achieve what the OP wants.
open a terminal
type caffeinate
press Enter
Once you have done this, your Mac will stay awake for as long as you leave the Terminal running.
You can minimize or hide it, and your Mac will not go to sleep until you use the keyboard shortcut Ctrl+C to interrupt the command.
source
Since OS 10.6, you have to make use of the IOPMAssertion family of functions, available in Cocoa. This is really well explained there.
Then, you will have to call it from Python. I'm not sure that there're already specific bindings for Cocoa in Python, but you can call Objective-C functions. It is really well described here.
There is a Python utility that illustrates how to raise the required assertions in Python directly: https://github.com/minrk/appnope
Another alternative would be to run the below script with
python <location/of/my/script.py> <hour until I want the PC to be awake>
e.g.
python /Users/johndee/Downloads/keep_awake.py 18:30
The script, which is to be saved locally:
#!/usr/bin/env python3
import random
import sys
import time
from datetime import datetime
from tkinter import Tk
import pyautogui
CHECK_STATUS_ONCE_IN = 120
WAIT_FOR_POSITION_CHANGE = 10
def current_position():
tkinter = Tk()
return [tkinter.winfo_pointerx(), tkinter.winfo_pointery()]
def mouse_is_moving():
pos1 = current_position()
time.sleep(WAIT_FOR_POSITION_CHANGE)
pos2 = current_position()
return not pos1 == pos2
def keep_awake():
# Shake the mouse a lil bit
initial_x, initial_y = current_position()
try:
for _ in range(random.randint(1, 10)):
# Mouse
pyautogui.moveTo(random.randint(1, 1000), random.randint(1, 1000))
# Keys
pyautogui.press("shift")
# Restore controls
pyautogui.moveTo(initial_x, initial_y)
except pyautogui.FailSafeException as e:
print(e)
def inspect_activity_until(time_to_stop: datetime):
time_to_stop = datetime.now().replace(
hour=time_to_stop.hour, minute=time_to_stop.minute
)
while datetime.now() < time_to_stop:
if not mouse_is_moving():
keep_awake()
time.sleep(CHECK_STATUS_ONCE_IN)
print(f"Stopping at {datetime.now()}")
if __name__ == "__main__":
given_time = sys.argv[1]
date_time_obj = datetime.strptime(given_time, "%H:%M")
inspect_activity_until(date_time_obj)

Visual Studio Python Debugger closes right after run

I have a python project. I click on a .py file and hit run with out debugger and the console comes up and the program runs. if I use run with debugger the console flashes and close. there are words on it. but it closes to fast for me to read. I have Set everything in tools I think I can. I have python debugger interactive.
import sys class Test():
def init(self):
var = raw_input("Hit Enter")
t = 'test'
print t
stepB = raw_input("Hit Enter")
a = Test()
place brake pint at t = 'test'

Categories

Resources