1

ImportError: No module named 'encodings'

While the error is self explaining, I know the cause of the issue. The problem is that the platform is using colon in their path name so when I set the path using:

Py_SetPath("the/path/with:colon/");

And then call:

Py_Initialize();

It fails to find the required modules to import. So, my question is: Is there any way to workaround without renaming the path (which I'm not controlling)? Can I somehow escape the colons?

Thanks!

1 Answer 1

2

From the looks of the source code, there is no way to do this that I can see...

Py_SetPath simply clears out any path that may have been set previously and then resets it to the string you give it with no real processing. That is what gets returned by Py_GetPath and that's all there is to it I suppose.

One suggestion that might work is to create a symbolic link to the directory somewhere where there isn't a : in the path name and use that instead ...

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

5 Comments

Thanks for the answer, do you have any example about the symlink? It seems interesting!
You'd just do something like (IIRC): ln -s "the/path/with:colon/" "some/path/without/colon", then in the C code, you'd use "some/path/without/colon" instead ...
Py_Initialize creates the sys module and calls PySys_SetPath(Py_GetPath()), which calls makepathobject using the platform DELIM. It doesn't handle escaping the delimiter. I don't know if working around this is possible since it's in the middle of bootstrapping the interpreter. Using a symlink is a good idea.
Thanks! But how am I suposed to run that in my code? I'm looking for a solution I can build... I found out a int symlink (const char *oldname, const char *newname), is that still available? (I'm not able to test till tomorrow and I will sleep better if I know :D)
@JohnSmith that is how you would do it; use /tmp as the target directory and a name take from /dev/urandom, retrying on error EEXIST or EINTR with a different name

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.