SyntaxError: non-ASCII character '\ xd1' in file modules / commands.pyc

I have a python file, [working dir/]modules/commands.py , which contains only the following:

 def getId(): return "commands" 

Then I have another [working dir/]main.py that uses the following:

 fpath = "modules/commands.py" mname = "commands" imp.load_source(mname, fpath) 

After adding getId() to commands.py I started getting the following error when trying to run main.py :

SyntaxError: Non-ASCII character '\xd1' in file modules/commands.pyc on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

The error is related to non-ascii characters, but there should not be any in the file. What causes the error?

Edit: The problem disappears temporarily if I delete the .pyc file, but returns the next time.

+4
source share
1 answer

(Since no one seemed to want a reputation, I will write the answer myself)

The load_source() method from the imp module was marked as deprecated and even completely removed from the python 3.X documentation.

The solution was to use find_module() and load_module() from the same imp module. After switching to them, he began to work flawlessly.

Deprecated functions seem a bit flawed in this behavior (at least in python 2.6.6 on Centos 6)

+2
source

Source: https://habr.com/ru/post/1492401/


All Articles