4

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

def getId():
    return "commands"

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

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

After I added the 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 complains about non-ascii characters, but there should be none in the file. What is causing the error?

Edit: The problem goes away temporarily if I remove the .pyc file, but comes back next time.

varesa
  • 2,399
  • 7
  • 26
  • 45
  • 2
    help(imp) says "undocumented functions are obsolete" and `load_source` is undocumented. Is there some reason not to use the documented `load_module` function? Based on the error it seems like `load_source` is loading the compiled (`.pyc`) file as if it were source code, but since it's not documented, that's all just a guess. – torek Jul 19 '13 at 23:09
  • working fine for me ! may be try removing `[working dir/]modules/__pycache__` – rnbguy Jul 19 '13 at 23:13
  • @torek The only reason I use that is that it is the first one I found a nice example of with google. I might look into load_module – varesa Jul 19 '13 at 23:17
  • @rnbcoder I can not find such a file/directory – varesa Jul 19 '13 at 23:17
  • hum. better to use `load_module` then. – rnbguy Jul 19 '13 at 23:18
  • @rnbcoder Wanna turn that into an answer? – varesa Jul 19 '13 at 23:30
  • As an interesting fact load_source seems to be somewhat popular. A google for "load_module example" brings this as promising link as the second: [How to import a module given the full path? (stackoverflow)](http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path). Multiple answers with load_source have literally got 100x more upvotes than the single one that mentions load_module – varesa Jul 19 '13 at 23:33
  • 1
    Related, mentions deprecation of load_source: http://stackoverflow.com/questions/10533679/python-import-a-module-from-a-directory-thats-not-a-package – torek Jul 20 '13 at 00:01
  • Use hexedit to check the code is really what you think it is. Some editors, may, sometime, insert nonprintable characters which you don't see but they break stuff. – Jan Matějka Jul 20 '13 at 00:28
  • @varesa I don't know much about imp module. Look into [this](http://stackoverflow.com/a/282778/1682673). – rnbguy Jul 20 '13 at 06:23

1 Answers1

3

(As nobody seemed to want the rep, I'll write an answer myself)

The method load_source() from the imp-module has been marked obsolete, and even removed completely from the documentation of python 3.X.

The solution was to use find_module() and load_module() from the same imp-module instead. After changing to them it started working flawlessly.

The obsolete functions seem to be somewhat buggy in that behavior (atleast in python 2.6.6 on Centos 6)

varesa
  • 2,399
  • 7
  • 26
  • 45