English

Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not be used when making a shared object.

When running some Python libs such as matplotlib or pymc, you might face and error similar to below:

libpython3.9.a(bytearrayobject.o): relocation R_X86_64_PC32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
---
E           /usr/bin/ld: /home/bgasparotto/.pyenv/versions/3.9.7/lib/libpython3.9.a(longobject.o): relocation R_X86_64_PC32 against symbol `PyExc_OverflowError' can not be used when making a shared object; recompile with -fPIC
E           /usr/bin/ld: final link failed: bad value
E           collect2: error: ld returned 1 exit status

In a nutshell, this error tells us that a Python shared lib expected to be available in your Python was not found during execution.

Solution One: pyenv and shared libs

This goes if you are using pyenv to manage your Python versions. By default, pyenv does not install Python interpreters with their shared libs. However, this can be fixed by reinstalling Python whilst enabling the shared libs:

PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install --force 3.9.5

Solution Two: libpython conflict

A Python lib might be present in more than one place thus resulting in a conflict. On Unix based systems, you can run the command below to find its occurrences:

sudo find / -name "libpython3.9.a"
/usr/lib/libpython3.9.a
/usr/lib/python3.9/config-3.9-x86_64-linux-gnu/libpython3.9.a

Then, rename one of the occurrences to something else so it is not picked up anymore. I chose to rename the first occurrence to libpython3.9.a.backup:

sudo mv /usr/lib/libpython3.9.a /usr/lib/libpython3.9.a.backup

References

bgasparotto

Recent Posts

Python function decorator

This guide will show you how to create a Python function decorator with a few…

2 years ago

Got permission denied while trying to connect to the Docker daemon socket

This guide will show you how to fix the error Got permission denied while trying…

2 years ago

Python virtual environment on Intellij IDEA

This guide will show you how to create a Python virtual environment on Intellij IDEA…

2 years ago

Find and kill processes on Linux and Mac by port number

This tutorial will quickly show you how to to find and kill processes on Linux,…

2 years ago

Kubernetes useful commands

I condensed below a cheat sheet of Kubernetes useful commands. I will keep updating this…

2 years ago

Create aliases for commands on Unix

This tutorial will show how to create aliases for commands on Unix based systems, so…

4 years ago