Why does importing a Python library throw a syntax error?

Question:

I have a Windows machine in which some Python code works, but now needs to work on new machine.

I have installed the same Python version 3.6.5.

First issue was that when I run the code, it can not find a re.py library, which is in fact in Python’s Lib folder. So I have added sys.path.append('C:PythonLib') and now it can find it.

But now I get the syntax error from that library, where I say import re, that lines throws an error regarding some line in re.py library. If I import getopt, I also get syntax error on some line.

How is that even possible? Syntax error in pythons Lib files which came with installation?

And the thing is that on machine 1 it works, same file contents, same python version. I am under impression that I have wrong in python.exe version for this version of libraries, but I have simply downloaded Windows installer and installed it.

I don’t even know what to google for, does someone has any idea? I am importing re in WeblogicAuto.py on line 5.

D:Jenkinsworkspaceweblogic-full-deployment-copyweblogic-deployment>MainAutoDeployment.py -f DEV -v 2.61.0.12 

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Problem invoking WLST - Traceback (innermost last):
  File "D:Jenkinsworkspaceweblogic-full-deployment-copyweblogic-deploymentWeblogicAuto.py", line 5, in ?
  File "C:PythonLibre.py", line 247
        b"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
         ^
SyntaxError: invalid syntax
Asked By: Igor Igorito

||

Answers:

You are using WLST (WebLogic Scripting Tool).

WLST is very specific distribution of python:

  • it’s jython
  • it roughly corresponds with python 2.7

And you trying to import libs from your CPython 3.6.5 distribution.

You need to:

  • remove your sys.path.append that you added earlier
  • check python libraries in jython library path. Which could be $HOME/.jython or WLST-specific path (look in WebLogic documentation)
Answered By: Alex Yu

OK many thanks to ya all, I’ve found the solution, it was about Jython libraries. I will try to elaborate a bit if someone stumbles upon this issue.

What I do here is that I deploy java modules to Oracle Weblogic application server. So this answer will also help someone trying to deploy to Weblogic from python.

From the start I was avoiding to install Webloglogic software on a machine from where I do the deploy (and that is a Jenkins slave which runs these python file, simple job).
I noticed that first machine (first Jenkins slave) has Weblogic installed, but I like to keep it minimal 🙂

What you do need is weblogic.jar and (not sure at this moment) wlfullclient.jar which is generated on Weblogic server (google how if needed or you may already have it).

The thing is that even though I have pure python code, when you call another python code with java weblogic.WLST pythonCode.py, it will be run with jython application! And it needs its libraries in its sys path.

In jython file I’ve added print(sys.path) then run in on both machines (slaves). I’ve noticed that path is different on those machines, even though if you type it in command prompt you get the same, but different then when Jenkins runs it.

So instead of looking how to fix those paths and copy files to them, I have created folders where it expects them, and copied them from first machine (easy fix, I may look into it later).

These are the sys.path and files that were needed, present on first machine:

['D:\Jenkins\weblogic\Lib', '__classpath__', 'C:/bea10/wlserver_10.3/server/lib/weblogic.jar', 'C:/bea10/wlserver_10.3/common/wlst/modules/jython-modules.jar/Lib', 'C:/bea10/wlserver_10.3/common/wlst', 'C:/bea10/wlserver_10.3/common/wlst/lib', 'C:/bea10/wlserver_10.3/common/wlst/modules', '.']

This was sys.path on second machine, so I simply copied there:

['D:\Jenkins\weblogic\Lib', '__classpath__', 'D:/Jenkins/server/lib/weblogic.jar', 'D:/Jenkins/common/wlst/modules/jython-modules.jar/Lib', 'D:/Jenkins/common/wlst', 'D:/Jenkins/common/wlst/lib', 'D:/Jenkins/common/wlst/modules', '.']

Note that jython-modules.jar is a file, so /Lib should be from that file if I get how java works.

Feel free to contact me for more details.

Answered By: Igor Igorito