When importing a module without null bytes I get ValueError: source code string cannot contain null bytes

Viewed 2681

Python isn't importing modules!

marketing
    ├───__init__.py
    ├───analyzation\
    │   ├───__init__.py
    │   ├───analyzer.py
    ├───api\
    ├───test\
    │   ├───test_res\ (not important)
    │   ├───__init__.py
    │   ├───test_analyzer.py

Above is my folder structure. So, in analyzer.py I have a class DataSet and nothing else other than some imports from pandas and an import in a method import random.

In test_analyzer.py I have a class that subclasses unittest.TestCase. This script tries to test the following class being imported: import marketing.analyzation.analyzer (which doesn't work). The error message is the following:

Traceback (most recent call last):
  File ".\test_analyzer.py", line 1, in <module>
    import marketing.analyzation.analyzer
ValueError: source code string cannot contain null bytes

Just to make sure it wasn't some dumb vim error, I retyped analyzer.py. Spent at least 2 hours trying to fix this, and another hour surfing stackoverflow and googling, but so far I've come up with nothing. Is it because test_analyzer.py is in its own submodule?

Thanks~~~

2 Answers

I had similar problem with pytest.

In my case the problem was in __init__.py: the file was created in UTF-16 encoding. I changed encoding to UTF-8 and the problem disappeared.

Related