Python script can't run in terminal but run in shell

the python script i use is successfully to run in shell but unable to run in terminal.
in the below is the hint i got :

python code below:

import pygame
import time, threading
import RPi.GPIO as GPIO
import smbus
from time import sleep
#from pythonosc import osc_message_builder
from pythonosc import udp_client

sender=udp_client.SimpleUDPClient(‘127.0.1.1’,4560)
bus = smbus.SMBus(1) # Pi 2

MHD_R = 0x2B
NHD_R = 0x2C
NCL_R = 0x2D
FDL_R = 0x2E
MHD_F = 0x2F
NHD_F = 0x30
NCL_F = 0x31
FDL_F = 0x32
ELE0_T = 0x41
ELE0_R = 0x42
ELE1_T = 0x43
ELE1_R = 0x44
ELE2_T = 0x45
ELE2_R = 0x46
ELE3_T = 0x47
ELE3_R = 0x48
ELE4_T = 0x49
ELE4_R = 0x4A
ELE5_T = 0x4B
ELE5_R = 0x4C
ELE6_T = 0x4D
ELE6_R = 0x4E
ELE7_T = 0x4F
ELE7_R = 0x50
ELE8_T = 0x51
ELE8_R = 0x52
ELE9_T = 0x53
ELE9_R = 0x54
ELE10_T = 0x55
ELE10_R = 0x56
ELE11_T = 0x57
ELE11_R = 0x58
FIL_CFG = 0x5D
ELE_CFG = 0x5E
GPIO_CTRL0 = 0x73
GPIO_CTRL1 = 0x74
GPIO_DATA = 0x75
GPIO_DIR = 0x76
GPIO_EN = 0x77
GPIO_SET = 0x78
GPIO_CLEAR = 0x79
GPIO_TOGGLE = 0x7A
ATO_CFG0 = 0x7B
ATO_CFGU = 0x7D
ATO_CFGL = 0x7E
ATO_CFGT = 0x7F

TOU_THRESH = 6
REL_THRESH = 12

def playSound(i):
if (sounds[i]):
sounds[i].play()

def readData(address):
MSB = bus.read_byte_data(address, 0x00)#
LSB = bus.read_byte_data(address, 0x01)#
touchData = bus.read_word_data(address, 0x00)
return touchData

def setup(address):

bus.write_byte_data(address, ELE_CFG, 0x00)

# Section A - Controls filtering when data is > baseline.
 
bus.write_byte_data(address, MHD_R, 0x01)
bus.write_byte_data(address, NHD_R, 0x01)
bus.write_byte_data(address, NCL_R, 0x00)
bus.write_byte_data(address, FDL_R, 0x00)

# Section B - Controls filtering when data is < baseline.

bus.write_byte_data(address, MHD_F, 0x01)
bus.write_byte_data(address, NHD_F, 0x01)
bus.write_byte_data(address, NCL_F, 0xFF)
bus.write_byte_data(address, FDL_F, 0x02)	

#Section C - Sets touch and release thresholds for each electrode

bus.write_byte_data(address, ELE0_T, TOU_THRESH)
bus.write_byte_data(address, ELE0_R, REL_THRESH)

bus.write_byte_data(address, ELE1_T, TOU_THRESH)
bus.write_byte_data(address, ELE1_R, REL_THRESH)

bus.write_byte_data(address, ELE2_T, TOU_THRESH)
bus.write_byte_data(address, ELE2_R, REL_THRESH)

bus.write_byte_data(address, ELE3_T, TOU_THRESH)
bus.write_byte_data(address, ELE3_R, REL_THRESH)

bus.write_byte_data(address, ELE4_T, TOU_THRESH)
bus.write_byte_data(address, ELE4_R, REL_THRESH)

bus.write_byte_data(address, ELE5_T, TOU_THRESH)
bus.write_byte_data(address, ELE5_R, REL_THRESH)

bus.write_byte_data(address, ELE6_T, TOU_THRESH)
bus.write_byte_data(address, ELE6_R, REL_THRESH)

bus.write_byte_data(address, ELE7_T, TOU_THRESH)
bus.write_byte_data(address, ELE7_R, REL_THRESH)

bus.write_byte_data(address, ELE8_T, TOU_THRESH)
bus.write_byte_data(address, ELE8_R, REL_THRESH)

bus.write_byte_data(address, ELE9_T, TOU_THRESH)
bus.write_byte_data(address, ELE9_R, REL_THRESH)

bus.write_byte_data(address, ELE10_T, TOU_THRESH)
bus.write_byte_data(address, ELE10_R, REL_THRESH)

bus.write_byte_data(address, ELE11_T, TOU_THRESH)
bus.write_byte_data(address, ELE11_R, REL_THRESH)	



bus.write_byte_data(address, FIL_CFG, 0x04)



bus.write_byte_data(address, ELE_CFG, 0x0c)  # Enables all 12 Electrodes	

TOU_THRESH = 10
REL_THRESH = 20
setup(0x5a)
last_touched = readData(0x5a)

pygame.mixer.pre_init(44100, -16, 12, 512)#
pygame.init()
pygame.mixer.music.set_volume(1.0)

sounds = [0,0,0,0,0,0,0,0,0,0,0,0]
key = 0

lastTap = 0

while True:
currentTap = readData(0x5a)
for i in range(12):
pin_bit = 1<<i
if currentTap & pin_bit and not lastTap & pin_bit:
sender.send_message(’/play_this’,i)
print ("Touched: " + str(i))

if not currentTap & pin_bit and lastTap & pin_bit:
  if i==10:
     j=99
     sender.send_message('/play_this',j)
  print ("Released: " + str(i))

lastTap = currentTap
time.sleep(0.1)

Hi Kenny - is this related to a Sonic Pi project in some way? :slightly_smiling_face:

yes, but when i define “from python osc import udp_client” on python program. it can’t run on terminal

Looking here:

It appears that python-osc requires at least Python 3.5 - so you will need to make sure that you install dependencies and start the script you’re trying to run all on python 3.

Beyond that, I’m not really a Python expert - so unless anyone else has any advice to offer, you may well have more luck asking folks in either a more general question and answer place (such as stack overflow) or a more focused Python forum somewhere :man_shrugging: :slightly_smiling_face:

unfortunately, i adjust the code by that link. Although,it can run on the shell, but still can’t exec. in terminal. :frowning:

I think local address is wrong here.
sender=udp_client.SimpleUDPClient(‘127.0.1.1’,4560)
should be 127.0.0.1

Also, you may find it works if you put the actual address of the PI computer in rather than 127.0.0.1
eg 192.168.1.23 (or whatever it is).

It could be that the default python version being used is python 2.x, so maybe if you try running it with python3 instead of python that might help.

Thanks for all of your help, I’ve tried all of the methods provided above. Eventually, it exec. in the terminal successfully when i type python3.7 rather than python which is the wrong mistake i made it before.

i also tried both address 127.0.0.1 and 192.168… , they all works when i run it in terminal.