Sublime Forum

Python 3: Error on import (Mac)

#1

I am trying to run Python 3 scripts from within sublime text. And it mostly work fine.
But if I try to run one that uses TensorFlow, I get an error on importing the library that say:
“TypeError: expected string or bytes-like object”

Running the script, with python3, from a terminal works just fine. So there is some sort of difference between my terminal environment, and sublime text.

Also, it works fine if I run a TensorFlow project with Python 2 from Sublime Text

Could someone please point me to where I need to look to investigate further?

0 Likes

#2

The only appreciable difference between the embedded Python 3 and standalone Python 3 is that we don’t attach a stdin file handle.

You’ll have to look at the backtrace and see where the exception is being thrown. That should help in tracking down what the issue is.

0 Likes

#3

does https://packagecontrol.io/packages/Fix%20Mac%20Path make a difference?

0 Likes

#4

Full traceback:

`
Traceback (most recent call last):
File “/Users/greger/devel/projects/generative/neural/imggen/train.py”, line 12, in
from imgtrainer import ImageTrainer
File “/Users/greger/devel/projects/generative/neural/imggen/imgtrainer.py”, line 10, in
import tensorflow as tf
File “/usr/local/lib/python3.5/site-packages/tensorflow/init.py”, line 24, in
from tensorflow.python import *
File “/usr/local/lib/python3.5/site-packages/tensorflow/python/init.py”, line 71, in
from tensorflow.python.framework.framework_lib import *
File “/usr/local/lib/python3.5/site-packages/tensorflow/python/framework/framework_lib.py”, line 73, in
from tensorflow.python.framework.ops import Graph
File “/usr/local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py”, line 41, in
from tensorflow.python.framework import registry
File “/usr/local/lib/python3.5/site-packages/tensorflow/python/framework/registry.py”, line 28, in
from tensorflow.python.platform import tf_logging as logging
File “/usr/local/lib/python3.5/site-packages/tensorflow/python/platform/tf_logging.py”, line 249, in
remove_undocumented(name, _allowed_symbols)
File “/usr/local/lib/python3.5/site-packages/tensorflow/python/util/all_util.py”, line 102, in remove_undocumented
should_have = make_all(module_name, doc_string_modules)
File “/usr/local/lib/python3.5/site-packages/tensorflow/python/util/all_util.py”, line 53, in make_all
for m in _reference_pattern.finditer(doc_module.doc)
TypeError: expected string or bytes-like object

`
Fix Mac Path did not help.

0 Likes

#5

And upon further investigation:

Around the problematic code there is a call to
sys.modules[‘tensorflow.python.platform.tf_logging’]

and on the returned object the __doc__ attribute is accessed.
When running from inside sublime text, __doc__ is None, but when run from Shell, it is a whole lot of text.

0 Likes

#6

Your problem is that Sublime runs with the -OO parameter when running Python3. This causes problems with TensorFlow, both when rendering _doc_ and, for me, in relation to this error: https://github.com/tensorflow/tensorflow/issues/3369

Aside: I received the “AttributeError: ‘module’ object has no attribute ‘constant’” error after fixing the _doc_ errors, manually.

Example:
[shell_cmd: python3 -OO -u “/Users/name/path/tensorflow/testing_tensorflow_basics.py”]

To test my theory, I tried running both with and without the -OO parameter in a BASH shell (testing outside of Sublime). The program ran normally after removing the -OO.

From Python Help:
-OO : remove doc-strings in addition to the -O optimizations
-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x
-u : unbuffered binary stdout and stderr, stdin always buffered

How to fix the problem:
I setup a separate “Python3TF” Build System in Sublime. It only took a couple minutes.
First, you want to install the PackageResourceViewer in Sublime if you don’t have it. Here are instructions, but stop before the C++ part: http://stackoverflow.com/a/23790095/4427457
Next, you’ll want to open the existing Python3.sublime-build:

  • Tools -> Command Palette, type “prvo”, then select “PackageResourceView: Open Resource”.
  • Select Python 3 -> Python3.sublime-build
  • Copy everything
    Finally, create a new Build System without the -OO parameter:
  • Tools -> Build System -> New Build System
  • Paste over the new build template, then remove the -OO parameter.
  • Save the file as… “Python3TF”
    It will now appear as a new Build System (Tools -> Build System -> Python3TF) in Sublime. When you run your TensorFlow files under this build system, it should build successfully.
0 Likes