Bad magic number in python code

2.6k Views Asked by At

I have some problems while trying to import psspy module. I have a python code containing the following lines:

import os,sys
PYTHONPATH = r'C:\Program Files (x86)\PTI\PSSE32\PSSBIN'

MODELFOLDER = r'C:\Program Files (x86)\PTI\PSSE32\MODELDRW'

sys.path.append(PYTHONPATH)
os.environ['PATH'] += ';' + PYTHONPATH

import psspy
import redirect

# Redirect output from PSSE to Python:
redirect.psse2py()

# Last case:
CASE = r"C:\Program Files (x86)\PTI\PSSE32\EXAMPLE\savnw.sav"
psspy.psseinit(12000)
psspy.case(CASE)

# Convert loads (3 step process):
psspy.conl(-1,1,1)
psspy.conl(-1,1,2,[0,0],[100,0,0,100])
psspy.conl(-1,1,3)

# Convert generators:
psspy.cong()

# Solve for dynamics
psspy.ordr()
psspy.fact()
psspy.tysl()

# Save converted case
case_root = os.path.splitext(CASE)[0]
psspy.save(case_root + "_C.sav")

# Add dynamics data
psspy.dyre_new(dyrefile="C:\Program Files 
(x86)\PTI\PSSE32\EXAMPLE\savnw.dyr")

# Add channels by subsystem
#   BUS VOLTAGE
psspy.chsb(sid=0,all=1, status=[-1,-1,-1,1,13,0])
#   MACHINE SPEED
psspy.chsb(sid=0,all=1, status=[-1,-1,-1,1,7,0])

# Add channels individually
#   BRANCH MVA
psspy.branch_mva_channel([-1,-1,-1,3001,3002],'1')

# Save snapshot
psspy.snap(sfile="C:\Program Files 
(x86)\PTI\PSSE32\EXAMPLE\python_test.snp")

# Initialize
psspy.strt(outfile="C:\Program Files 
(x86)\PTI\PSSE32\EXAMPLE\python_test_1.out")
psspy.run(tpause=0)

# 3-phase fault on bus 201 (default bus fault is a 3phase and there is no 
bus 200)
psspy.dist_bus_fault(ibus=201)

# Run to 3 cycles
time = 3.0/60.0
psspy.run(tpause=time)

# Clear fault (assuming only part of bus faults)
psspy.dist_clear_fault()
psspy.dist_branch_trip(ibus=201, jbus=151, id='1')

# Run to 20 seconds
time = 20
psspy.run(tpause=time)

#-----------------------------

# Run 2nd fault if you want
psspy.case(case_root + "_C.sav")
psspy.rstr(sfile="C:\Program Files 
(x86)\PTI\PSSE32\EXAMPLE\python_test.snp")

# Initialize
psspy.strt(outfile="C:\Program Files 
(x86)\PTI\PSSE32\EXAMPLE\python_test_2.out")
psspy.run(tpause=0)

# 1-phase fault branch 3001 to 3003
psspy.dist_branch_fault(ibus=3001, jbus=3003, id='1',units=1,values=
[352,-2389])

# Run to 4 cycles
time = 4.0/60.0
psspy.run(tpause=time)

# Clear fault
psspy.dist_clear_fault()
psspy.dist_branch_trip(ibus=3001, jbus=3003, id='1')

# Run to 20 seconds
time = 20
psspy.run(tpause=time)
# Halt
psspy.pssehalt_2()

But I get this error: "Bad magic number in C:\Program Files (x86)\PTI\PSSE32\PSSBIN\psspy.py" There is no information in the documentation that mentions any change in module name or setup. Does anybody know how to correct this? Thanks!!!

2

There are 2 best solutions below

0
On

Bad Magic Numbers are associated with python versions.

  • PSSE32 --> Python 2.5
  • PSSE33 --> Python 2.7

The above example uses PSSE32 and hence the code should be executed in Python 2.7

0
On

Since Python2 will be officially not supported by Jan 2020, it will be a good idea to considering updating the code to Py3. Correspondingly the Magic Number of Py3 for PSSE is version 34. Hope this helps.