3

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~~~

7
  • Probably dupe of stackoverflow.com/questions/31233777/… but I can't verify now. Commented Jul 31, 2017 at 23:52
  • @matt961: If you rename analyzer.py to analyzer2.py (without changing the import), does the error change? Commented Jul 31, 2017 at 23:55
  • @Ryan nope, same error, not even an import error. Commented Aug 1, 2017 at 0:00
  • 2
    Try find . -name '*.py' -exec grep -Pa '\x00' {} \; Commented Aug 1, 2017 at 0:04
  • Are you doing this under Windows? Did you use Notepad? It smells like the file was saved as Unicode rather than plain ole ASCII. Commented Aug 1, 2017 at 0:19

2 Answers 2

0

Don't use powershell to create files being read by python. Used Vim to create __init__.py files instead. Thanks commenters.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.