Python Language Tutorial

Find Tips and tricks on how to better use the Zeus IDE. Feel free to post your own tips but please do not post bug reports, feature requests or questions here.
Post Reply
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Python Language Tutorial

Post by jussij »

Writing Python Code

To program in Python using the Zeus IDE do the following.

1) Create a c:\temp\test.py test file using the Python 3.x code shown below:

Code: Select all

print ("Hello world")

result = input("Enter a value: ")

print (result)

print ("Complete.....")

2) Use the Compiler, Compile menu to syntax check the Python code.

3) To run the code use the Macros, Execute 'test.py' menu and in the resulting console window type in 1234 and then hit the enter key.

The following output will be captured inside Zeus:

Code: Select all

---------------------------------------------------------------------------
      Zeus for Windows Programmers Editor - Version 3.97k
      Copyright (c) Xidicone P/L 1993-2012.  All rights reserved.
---------------------------------------------------------------------------

Hello world
Enter a value: 1234
Complete.....

Setting up the Language Server
To setup the Python language server follow the instructions found here: https://www.zeusedit.com/lsp/python.html

Cheers Jussi
pwdiener
Posts: 134
Joined: Wed Jul 11, 2007 3:45 pm

Post by pwdiener »

What should the Compiler command line be set to for this? Mine was set to python.exe "$fn", and I got an error that python.exe could not be found.

I'm using your example program from above - I copied it and deleted the blank lines. I tried pythonc.exe too, and got

Code: Select all

---------------------------------------------------------------------------
      Zeus for Windows Programmers Editor - Version 3.97n-beta9
      Copyright (c) Xidicone P/L 1993-2012.  All rights reserved.
---------------------------------------------------------------------------
 **** Unregistered Software. To be used for evaluation purposes only. ****
---------------------------------------------------------------------------


Using document type compiler options....

Hello world
Enter a value: Traceback (most recent call last):
  File "C:\Program Files\Zeus\zScript\testpy.py", line 2, in <module>
    result = raw_input("Enter a value: ")
EOFError: EOF when reading a line
on the compile. Got similar results when running:

---------------------------------------------------------------------------
Zeus for Windows Programmers Editor - Version 3.97n-beta9
Copyright (c) Xidicone P/L 1993-2012. All rights reserved.

Code: Select all

---------------------------------------------------------------------------
 **** Unregistered Software. To be used for evaluation purposes only. ****
---------------------------------------------------------------------------


Scripting Error :> Traceback (most recent call last):
Scripting Error :>   File "c:\Program Files\Zeus\zScript\testpy.py", line 2, in <module>
Scripting Error :>     result = raw_input("Enter a value: ")
Scripting Error :> EOFError: EOF when reading a line

Scripting Error :> Hello world
Scripting Error :> Enter a value:
Macro script generated '20' Debug, Error and/or Warning message(s).
I also tried zpython.cmd with not quite the same results, but very similar.

I know nothing at all about Python, but maybe it's time to change that. What am I doing wrong?

Bill Diener
pwdiener
Posts: 134
Joined: Wed Jul 11, 2007 3:45 pm

Post by pwdiener »

I've resolved the python.exe not found issue by installing Python 2.7 (duh) and adding it to the path, but still get similar results. Running 3.97 beta9, BTW.

Bill Diener
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

I've resolved the python.exe not found issue by installing Python 2.7 (duh)

That is one way to do it but the pythonc.exe should also have worked.

The pythonc.exe is a version of the Python 2.7 that comes with Zeus.
but still get similar results. Running 3.97 beta9, BTW.

That is very starange. It is working fine for me at this end :?

Here is my c:\temp\test.py file.

Code: Select all

print "Hello world"

result = raw_input("Enter a value: ")

print result

print "Complete....."
Below I run this file from the command line prompt using the Tools, DOS Shell menu:

Code: Select all

C:\TEMP>pythonc test.py
Hello world
Enter a value: testing
testing
Complete.....

C:\TEMP>
There is also an option to execute Python found at the bottom of the Macros menu and that also worked.

Just open the python file and go to the macros menu.
I know nothing at all about Python, but maybe it's time to change that.

Python : http://www.python.org/doc/
What am I doing wrong?

As you can see from the output the macro did in fact run but something went wrong with the raw_input call.

But I am not sure why that call works at this end :?:

I suggest trying another form of Python input.

Cheers Jussi
pwdiener
Posts: 134
Joined: Wed Jul 11, 2007 3:45 pm

Post by pwdiener »

When I execute the sample program via the DOS shell or via the run as script menu item, it works just fine. My problem occurs when I try to run it via the F10 (MacroExecuteScript) key. When I enter the name of the script file (containing the sample program), I get the result shown previously.

I still have LUA set as my active scripting language - I don't need to change that, do I?
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

My problem occurs when I try to run it via the F10 (MacroExecuteScript) key.

Ok, this now makes sense. The MacroExecuteScript only runs Zeus macros.
When I enter the name of the script file (containing the sample program), I get the result shown previously.

Zeus macros are only a subset of the full macro language.

So for example Zeus macros they don't expect and in fact don't allow input from stdin and this is why it is failing.

If want user input in a Zeus macro you need to use the user_input function.

Code: Select all

function key_macro()
    -- the default command
    local nill = ""

    -- ask for a user for a command line
    input1 = user_input("One Text:", nill, "One User Input Test")

    message_box(0, "One Text: " .. input1)

    -- ask for a user for a command line
    input1, input2 = user_input("One Text:", nill, "Two Text:", nill, "User Input Test")

    message_box(0, "One Text: " .. input1)
    message_box(0, "Two Text: " .. input2)
end

key_macro() -- run the macro
I still have LUA set as my active scripting language - I don't need to change that, do I?
No. A similar Lua macro would fail in a similar way.

Cheers Jussi
dijerydack
Posts: 7
Joined: Sat May 25, 2013 11:02 am

Post by dijerydack »

If you are using Python 3.x then you will need to use this code:

Code: Select all

print ("Hello world")

result = input("Enter a value: ")

print (result)

print ("Complete.....")
This code will only work for the Python 2.7.x version:

Code: Select all

print "Hello world" 

result = raw_input("Enter a value: ") 

print result 

print "Complete....."
alia10
Posts: 1
Joined: Wed Mar 20, 2019 6:36 pm

Re: Python Language Tutorial

Post by alia10 »

In compiler options / command line I have put : c:\python33\python.exe "$fn" (I dont know what "$fn" is, it was there already so I left it.) That is my python directory. however Python loads and crashes immediately and I get this error :

Using document type compiler options....

Fatal Python error: Py_Initialize: unable to load the file system codec
File "C:\Users\Jack\AppData\Roaming\Xidicone\Zeus\zScript\PyLib\encodings\__init__.py", line 123
raise CodecRegistryError,\
^
SyntaxError: invalid syntax

What am I doing wrong?
Last edited by alia10 on Sat Mar 23, 2019 11:12 am, edited 1 time in total.
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Re: Python Language Tutorial

Post by jussij »

I dont know what "$fn" is, it was there already so I left it.

The $fn is a Zeus macro tag and it get substituted with a value.

In this case the $fn is the file name tag and it is replaced with the name of the currently active document.

Similarly there is a $fdd which is is the file drive and directory tag.

For lots more tag examples paste this line of text into a new document in Zeus and the use the View, Open File menu to load the file:

Code: Select all

ztag.lua

Now to the error you are seeing:

Code: Select all

Fatal Python error: Py_Initialize: unable to load the file system codec

What has happened is you have tried to run the current file using the version of Python found here:

Code: Select all

c:\python33\

Now this has caused an issue only because Zeus also ships with a version of Python and these two versions are different.

To make Python work seamless inside Zeus, it sets the PYTHONPATH to point to it's version of the Python library:

Code: Select all

C:\Users\Jack\AppData\Roaming\Xidicone\Zeus\zScript\PyLib\

Now when you compile the script it runs the other version of Python and it uses that PYTHONPATH value, meaning it runs using the wrong version of the Python library.

Now you could fix this issue by running the other version of Python via a batch file, which would then allow you to set the PYTHONPATH to point to the correct library.

However, a much simpler option would be to just run the version of Python that is installed with Zeus:

Code: Select all

pythonc.exe --version
Python 3.7.0

So all you really need to do is change the compiler command line to be this:

Code: Select all

pythonc.exe "$fn"

However there is one thing you should note.

If you have a Python test.py file open, in the Macros menu you will see this option (towards the end of the menu):

Code: Select all

Execute "test.py" Script

That menu item will run your Python script.

While you can also run the script using the compile menu using your compiler options, where this will cause problems is when your macro script asks for user input.

When the compiler is run it never expects user input so it never checks for user input.

So if your script does ask for user input that will not run as expected using the compiler menu.

However, the macro menu approach does check for user input so the Python script will run perfectly with or without user input :)

FYI the default Zeus Python compiler command would have been set to this:

Code: Select all

zpython.cmd '$f' "$fdd"

If you open that zpython.cmd batch file, you will see all the batch file does it try to syntax check the Python code found in the file.

Cheers Jussi
Post Reply