2

I am trying to call a python function from a C program but when trying to run the compiled program I get the error:

Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007fff94214380 (most recent call first):
Abort trap: 6

I am using python3.7 installed using anaconda3 on macOS High Sierra 10.13.5.

To compile my code called callpy.c, I used

gcc -o callpy callpy.c -I/Users/wernop/anaconda3/include/python3.7m -L/Users/wernop/anaconda3/lib/python3.7/config-3.7m-darwin -lpython3.7m

which runs without errors or warnings.

I saw this question: Fatal Python error: initfsencoding: unable to load the file system codec and therefore made sure to set the environmnent variables

PYTHONPATH='/Users/wernop/anaconda3/bin/python3.7'
PYTHONHOME='/Users/wernop/anaconda3/bin/python3.7'

I would be grateful for any help.

6
  • I saw this question: Fatal Python error: initfsencoding: unable to load the file system codec and therefore made sure to set the environmnent variables Are you not using an environment, though? Commented Apr 16, 2020 at 0:12
  • @AMC Yes, I am using the conda base environment, which is located in the directory /Users/wernop/anaconda3. Should my environment variables be set differently? Commented Apr 16, 2020 at 15:20
  • Err, PYTHONPATH='/Users/wernop/anaconda3/bin/python3.7' doesn't do what you expect. PYTHONPATH should point to library roots, not executables. Run the following: python3.7 -c "import encodings; print(encodings.__file__). Make sure you use the right executable from anaconda installation. This should print you a path like /usr/lib64/python3.7/encodings/__init__.py. You thus set PYTHONPATH="/usr/lib64/python3.7". Print the contents of sys.path in your code to see whether PYTHONPATH was applied correctly. Commented Apr 16, 2020 at 17:02
  • Aside from that, check what flags you'll need for compilation by running python3-config --cflags --ldflags. Commented Apr 16, 2020 at 17:02
  • Yes, I am using the conda base environment Don't! Using a single environment for everything, especially the base environment, is a surefire way to end up with a bunch of issues. I wouldn't be surprised if it's at least partially responsible for the problems you're experiencing. Should my environment variables be set differently? I think there should be no need to mess with environment variables manually. How did you install Conda? Commented Apr 16, 2020 at 20:12

1 Answer 1

0

I don't know if I answer to your question, but when I call Python from C, I use PyObjects and it works really fine

You have some examples here : https://docs.python.org/3.8/extending/embedding.html

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

3 Comments

Thanks for your reply. I also used PyObjects in my C code. I think my problem is not so much about the C code, but about the Python environment.
Could you run your compilation with the -Wall option. This option show sometimes more details about compilation warning.
No warnings were given even with the -Wall option

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.