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.

AMC
  • 2,642
  • 7
  • 13
  • 35
wernop
  • 21
  • 2
  • _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? – AMC Apr 16 '20 at 00: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? – wernop Apr 16 '20 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. – hoefling Apr 16 '20 at 17:02
  • Aside from that, check what flags you'll need for compilation by running `python3-config --cflags --ldflags`. – hoefling Apr 16 '20 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? – AMC Apr 16 '20 at 20:12
  • Thank you both @AMC and @hoefling. I made a new anaconda environment, compiled with the flags given by `python3-config --cflags --ldflags`, set `PYTHONHOME` to the directory given by `python3.7 -c "import encodings; print(encodings.__file__)"`, and `PYTHONPATH` to the directory of my code and now it works! – wernop Apr 17 '20 at 13:29

1 Answers1

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

Naleakan
  • 28
  • 6
  • 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. – wernop Apr 14 '20 at 14:03
  • Could you run your compilation with the -Wall option. This option show sometimes more details about compilation warning. – Naleakan Apr 14 '20 at 14:07
  • No warnings were given even with the -Wall option – wernop Apr 14 '20 at 15:05