# Importing time module bug

**URL:** <https://forum.inductiveautomation.com/t/importing-time-module-bug/33906>\
**Category:** Ignition\
**Tags:** scripting, ignition80\
**Created:** [April 14, 2020, 12:48am UTC](https://forum.inductiveautomation.com/t/importing-time-module-bug/33906 "2020-04-14T00:48:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [April 14, 2020, 12:48am UTC](https://forum.inductiveautomation.com/t/importing-time-module-bug/33906/1 "2020-04-14T00:48:29Z")

</div>

I wanted to post this here because I would like to check if anyone else is having this same issue within the community.

A couple of days ago a user reported they were getting a “cryptic” error that read “Cannot create PyString with non-byte value”.

At first I could not replicate the error on my computer, so after doing some remote assistance I finally found the issue, and it relied on a harmless line of code; an import of the time module.

The only way I was able to replicate this behavior on my machine running Windows 10 I had to switch my system language to Čeština (a.k.a. Czech), run the Designer, and open the Script Console.

This is the code I executed on the Script Console:

```auto
import time

print time. __name__

```

And this is the console output:

```auto
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43) 
[OpenJDK 64-Bit Server VM (Azul Systems, Inc.)] on java11.0.5

>>> 
Java Traceback:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
	at org.python.core.PyString.<init>(PyString.java:57)
	at org.python.core.PyString.<init>(PyString.java:70)
	at org.python.core.PyString.<init>(PyString.java:74)
	at org.python.modules.time.Time.classDictInit(Time.java:124)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyJavaType.init(PyJavaType.java:571)
	at org.python.core.PyType.createType(PyType.java:1573)
	at org.python.core.PyType.addFromClass(PyType.java:1508)
	at org.python.core.PyType.fromClass(PyType.java:1609)
	at org.python.core.imp.createFromClass(imp.java:469)
	at org.python.core.imp.loadBuiltin(imp.java:578)
	at org.python.core.imp.find_module(imp.java:534)
	at org.python.core.imp.import_next(imp.java:838)
	at org.python.core.imp.import_module_level(imp.java:957)
	at org.python.core.imp.importName(imp.java:1057)
	at org.python.core.ImportFunction. __call__ ( __builtin__.java:1280)
	at org.python.core.PyObject. __call__ (PyObject.java:450)
	at org.python.core. __builtin__. __import__ ( __builtin__.java:1232)
	at org.python.core.imp.importOne(imp.java:1076)
	at org.python.pycode._pyx5.f$0(<input>:3)
	at org.python.pycode._pyx5.call_function(<input>)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1614)
	at org.python.core.Py.exec(Py.java:1658)
	at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
	at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:131)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:605)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:593)
	at java.desktop/javax.swing.SwingWorker$1.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.desktop/javax.swing.SwingWorker.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

	at org.python.core.Py.JavaError(Py.java:552)
	at org.python.core.Py.JavaError(Py.java:543)
	at org.python.core.PyJavaType.init(PyJavaType.java:578)
	at org.python.core.PyType.createType(PyType.java:1573)
	at org.python.core.PyType.addFromClass(PyType.java:1508)
	at org.python.core.PyType.fromClass(PyType.java:1609)
	at org.python.core.imp.createFromClass(imp.java:469)
	at org.python.core.imp.loadBuiltin(imp.java:578)
	at org.python.core.imp.find_module(imp.java:534)
	at org.python.core.imp.import_next(imp.java:838)
	at org.python.core.imp.import_module_level(imp.java:957)
	at org.python.core.imp.importName(imp.java:1057)
	at org.python.core.ImportFunction. __call__ ( __builtin__.java:1280)
	at org.python.core.PyObject. __call__ (PyObject.java:450)
	at org.python.core. __builtin__. __import__ ( __builtin__.java:1232)
	at org.python.core.imp.importOne(imp.java:1076)
	at org.python.pycode._pyx5.f$0(<input>:3)
	at org.python.pycode._pyx5.call_function(<input>)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1614)
	at org.python.core.Py.exec(Py.java:1658)
	at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
	at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:131)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:605)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:593)
	at java.desktop/javax.swing.SwingWorker$1.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.desktop/javax.swing.SwingWorker.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
	at org.python.core.PyString.<init>(PyString.java:57)
	at org.python.core.PyString.<init>(PyString.java:70)
	at org.python.core.PyString.<init>(PyString.java:74)
	at org.python.modules.time.Time.classDictInit(Time.java:124)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyJavaType.init(PyJavaType.java:571)
	... 29 more
Traceback (most recent call last):
  File "<input>", line 1, in <module>
java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
	at org.python.core.PyString.<init>(PyString.java:57)
	at org.python.core.PyString.<init>(PyString.java:70)
	at org.python.core.PyString.<init>(PyString.java:74)
	at org.python.modules.time.Time.classDictInit(Time.java:124)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyJavaType.init(PyJavaType.java:571)
	at org.python.core.PyType.createType(PyType.java:1573)
	at org.python.core.PyType.addFromClass(PyType.java:1508)
	at org.python.core.PyType.fromClass(PyType.java:1609)
	at org.python.core.imp.createFromClass(imp.java:469)
	at org.python.core.imp.loadBuiltin(imp.java:578)
	at org.python.core.imp.find_module(imp.java:534)
	at org.python.core.imp.import_next(imp.java:838)
	at org.python.core.imp.import_module_level(imp.java:957)
	at org.python.core.imp.importName(imp.java:1057)
	at org.python.core.ImportFunction. __call__ ( __builtin__.java:1280)
	at org.python.core.PyObject. __call__ (PyObject.java:450)
	at org.python.core. __builtin__. __import__ ( __builtin__.java:1232)
	at org.python.core.imp.importOne(imp.java:1076)
	at org.python.pycode._pyx5.f$0(<input>:3)
	at org.python.pycode._pyx5.call_function(<input>)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1614)
	at org.python.core.Py.exec(Py.java:1658)
	at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
	at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:131)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:605)
	at com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleWorker.doInBackground(JythonConsole.java:593)
	at java.desktop/javax.swing.SwingWorker$1.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.desktop/javax.swing.SwingWorker.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

```

This error was being thrown on a function I created where I [Temporarily enable DB connection](http://forum.inductiveautomation.com/t/temporarily-enable-db-connection/30414), and between retries I “sleep” the process for a second, and the only way I could squash this bug was to remove that sleep and replace it with a while loop.

I am aware this may sound a bit weird, but it is the behavior we are noticing.

Thanks!

---

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [April 14, 2020, 1:15am UTC](https://forum.inductiveautomation.com/t/importing-time-module-bug/33906/2 "2020-04-14T01:15:40Z")

</div>

Additional info.

The error goes away on the third execution. As in I click the _Execute_ button for the third time on the Script Console.

Code:

```auto
from java.lang import Exception as JException

try:
    import time
    print time. __name__
except JException, e:
    print e.message

```

Output:

```auto
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43) 
[OpenJDK 64-Bit Server VM (Azul Systems, Inc.)] on java11.0.5

>>> 
Cannot create PyString with non-byte value
>>> 
Cannot create PyString with non-byte value
>>> 
Time
>>> 

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 14, 2020, 11:08am UTC](https://forum.inductiveautomation.com/t/importing-time-module-bug/33906/3 "2020-04-14T11:08:17Z")

</div>

Python’s time module is tailored for and expects a CPython environment. Don’t use it. Use java.lang.Date and java.lang.Calendar instead.

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [April 14, 2020, 1:28pm UTC](https://forum.inductiveautomation.com/t/importing-time-module-bug/33906/4 "2020-04-14T13:28:16Z")

</div>

Apparently this is a known bug in Jython 2.7.1 when trying to print unicode. Like Phil said, if you need date strings in a locale that will go beyond 8-bit ASCII, you’ll have to change to Java’s libraries.

---

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [April 14, 2020, 8:02pm UTC](https://forum.inductiveautomation.com/t/importing-time-module-bug/33906/5 "2020-04-14T20:02:25Z")

</div>

I tested the following code, and hereby I confirm that switching to `java.lang.Thread` solves the problem experienced with Python’s `time` module.

```auto
import system.date

from java.lang import Exception as JException, Thread

def java_sleep():
    print 'Thread.sleep'
    print system.date.now()
    Thread.sleep(5000)
    print system.date.now()

def py_sleep():
    print 'time.sleep'
    import time
    print system.date.now()
    time.sleep(5)
    print system.date.now()

try:
    java_sleep()
    py_sleep()
except JException, e:
    print e.message

```

Output:

```auto
>>>>
Thread.sleep
Tue Apr 14 22:01:02 CEST 2020
Tue Apr 14 22:01:07 CEST 2020
time.sleep
Cannot create PyString with non-byte value
>>>>

```

But still mind-boggling that after multiple attempts calling Python’s `time.sleep` does work without throwing an error; and after one successful execution the error occurs no more.

I’ll be making the switch to `Thread.sleep`.

Thanks, @KathyApplebaum and @pturmel.

---

<div class="post-metadata">

**Author:** ![mpeteri](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mpeteri](https://forum.inductiveautomation.com/u/mpeteri)\
**Post date:** [June 22, 2022, 11:55am UTC](https://forum.inductiveautomation.com/t/importing-time-module-bug/33906/6 "2022-06-22T11:55:49Z")

</div>

Thanks
