77

I have created a python virtual environment using virtualenv, after activating it, I can see where is Python installed in my shell as following:

(virtualenv-test) bash-4.1$ whereis python
python: /usr/bin/python2.6 /usr/bin/python2.6-config /usr/bin/python
/usr/lib/python2.6 /usr/lib64/python2.6 /usr/X11R6/bin/python2.6
/usr/X11R6/bin/python2.6-config /usr/X11R6/bin/python
/usr/bin/X11/python2.6 /usr/bin/X11/python2.6-config
/usr/bin/X11/python /usr/include/python2.6
/usr/share/man/man1/python.1.gz

Also I can see what python version I'm using:

(virtualenv-test) bash-4.1$ which python
/data/virtualenv-test/bin/python

However, after typing python, I got the following error message:

(virtualenv-test) bash-4.1$ python
python: error while loading shared libraries: libpython3.4m.so.1.0: cannot open shared object file: No such file or directory

What can be the underlying reason?

0

15 Answers 15

81

Try adding the python3.4's lib path to the $LD_LIBRARY_PATH environment variable.

First find out the lib path of python3.4 (depends on how you installed python3.4)

For me it was: /opt/python361/lib, then add it to environment variable like so:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/python361/lib

P.S. I came across a similar problem while using virtualenv with python3.6, and I fixed it like so:

  • First, append include <lib path of python3.x> to /etc/ld.so.conf (Something like: include /opt/python361/lib or include /usr/local/lib)
  • Then, activate the new configuration by running sudo /sbin/ldconfig -v.
Sign up to request clarification or add additional context in comments.

5 Comments

2. worked for me. Could you point me in the direction of a good resource to understand why this works? thanks.
1. did the trick on Red Hat (no virtual env). My path was /opt/local/lib (I built python from source with --enable-shared in the call to ./configure and it printed out the path of the shared files.
First answer worked for me but then when I used gunicorn I got the same error in which case the P.S. solution did it.
in centos 7 it is export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
@AbhilashSinghChauhan this worked for me on Amazon Linux 2
37

Another way is adding LDFLAGS="-Wl,-rpath /usr/local/lib" in configure, for example

./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"

/usr/local/lib is the path where libpython3.*.so files are in

3 Comments

This worked for me. Python 3.7.4 under CentOS. Thanks!
This worked for me building and installing python-3.8.2 on ubuntu 14.04.6
I received the following error when trying this solution: ./python: symbol lookup error: ./python: undefined symbol: __gcov_indirect_call_callee I'm removing these flags and am going to try @williezh's solution.
22

For Python 3.6, it was fixed by

sudo apt-get install libpython3.6-dev

3 Comments

python 3.6 isn't available in my repos. I'm on Jessie. Yay!
And also for Python 3.9: sudo apt-get install libpython3.9-dev
Also works for 3.11.11 sudo apt-get install libpython3.11-dev.
13
export LD_LIBRARY_PATH=[your python path to libpython3.4m.so]

libpython3.4m.so is under your python source from which you built it.

Put it in your .bashrc to set it at login automatically.

I can't force virtualenv to 3.4 on my machine but you can see that under lib of your virtualenv there's just a bunch of symlink to your local python installation. I guess libpython3.4m.so is fetched by one of those.

1 Comment

To add to this answer for Linux users, if you cannot find where it may be located (perhaps due to multiple installations of Python), use the locate/find command.
7

For me, libpython3.6m.so.1.0 was in the folder where I downloaded Python source (~/Python3.6.9).

I simply did:

sudo cp ~/Python3.6.9/libpython3.6m.so.1.0 /usr/local/lib/python3.6/

and:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/python3.6

Comments

6

This one worked for me.

cd ~/
vim .bashrc
export LD_LIBRARY_PATH=~/miniconda/envs/python3.6/lib/

1 Comment

old question, add versioning and why this works to improve answer quality. Otherwise it helps trigger down-voters or bot labeling question "low quality answer". End of Review. Welcome and enjoy SO ;-)
5

Kudos to above, For python 3.X you can fix this issue with:

sudo apt-get install libpython3.x-dev

No need to any changes to environment path manually.

1 Comment

Not on ububtu 20.04
3

If you are manually installing another python3 or python,

e.g: python3.6,

the active files(e.g: python3.6, python3.6m, python3.6m-config) are located at /usr/local/bin,

the library files(e.g: python3.6, libpython3.so, libpython3.6m.so.1.0) are located at /usr/local/lib,

While you do not configure any environment before make & make install,

Now you need to add the load_library_path(LD_LIBRARY_PATH) of python3.6,

if you are using zsh, just:

cd ~

vim ~/.zshrc

Add the below code to ~/.zshrc:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

Just use :wq to save and quit the configuration file of zsh,

also, you need to active the hnewable .zshrc:

source ~/.zshrc

then try:

python

If you use bash, just:

cd ~

vim ~/.bashrc

And the below code to ~/.bashrc:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

Just use :wq to save and quit the configuration file of bash,

also, you need to active the newable .bashrc:

source ~/.bashrc

Now, for your turn!

First, you need to found out where libpython3.4m.so.1.0 is?

From you asked and told, I guess libpython3.4m.so.1.0 is located at

/data/virtualenv-test/lib/ because I saw your python is located at

/data/virtualenv-test/bin/python.

Second, add that loading library path to your bash's configuration file ~/.bashrc:

cd ~

vim ~/.bashrc

Use i to enter VIM editing state, and add the below code to ~/.bashrc:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/virtualenv-test/lib/

Use :wq to save and quit .bashrc, and active the newable .bashrc:

source ~/.bashrc

Third, test your installed Python.

All in all, you could change the upsatirs /data/virtualenv-test/lib/ to

yours(where libpython3.4m.so.1.0 is).

END!

Comments

2

I got it running by installing the package:

sudo apt-get install libpython3.x-dev

2 Comments

not on ubuntu 20.04
Worked on ubuntu 24.04 using vitualenvwrapper and deadsnakes python3.11
1

On python 3.8, I resolved this by deleting the virtualenv directory (./venv in my case) and recreating using python's built-in venv module installed of the pip-installed virtualenv. I'm on arch linux and also first did sudo pacman -Syu. Python was originally installed using just sudo pacman -S python.

$ rm -r ./venv
$ python -m venv venv
$ . ./venv/bin/activate
$ python --version
Python 3.8.1

Comments

0

I could solve it by installing libpython3.X without the (-dev). In your case, it gives:

sudo apt-get install libpython3.4

Comments

0

For Fedora or dnf-based system

# dnf search python3.10

=====================================================================
python3.10.i686 : Version 3.10 of the Python interpreter
python3.10.x86_64 : Version 3.10 of the Python interpreter
======================================================================= Name delle corrispondenze: python3.10 =======================================================================
python3.10-debug.i686 : Debug version of the Python runtime
python3.10-debug.x86_64 : Debug version of the Python runtime
python3.10-devel.i686 : Libraries and header files needed for Python development
python3.10-devel.x86_64 : Libraries and header files needed for Python development
python3.10-idle.i686 : A basic graphical development environment for Python
python3.10-idle.x86_64 : A basic graphical development environment for Python
python3.10-libs.i686 : Python runtime libraries
python3.10-libs.x86_64 : Python runtime libraries
python3.10-test.i686 : The self-test suite for the main python3 package
python3.10-test.x86_64 : The self-test suite for the main python3 package
python3.10-tkinter.i686 : A GUI toolkit for Python
python3.10-tkinter.x86_64 : A GUI toolkit for Python
# dnf install python3.10-devel

Comments

0

For Github Actions, this could be worked around by installing the dependency directly, disabling the actions/setup-python cache and using the generic cache action instead:

   - uses: actions/setup-python@v4
      with:
        python-version: '3'
    - uses: actions/cache@v3
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip
    - run: pip install 'mydependency<2'
      shell: bash
    - run: ${{ github.action_path }}/myscript
      shell: bash

Comments

0

My python binary also had this error while using bazel with the linux-sandbox and --experimental_use_hermetic_linux_sandbox

This was caused because the binary we were running did not have access to /dev/, /proc, and /sys

After mounting those directories with --sandbox_add_mount_pair, it started working

Comments

0

Please refer to the question: Why: python3.11: error while loading shared libraries: libpython3.11.so.1.0: cannot open shared object file: No such file or directory

I have found the root cause of the issue, check /etc/ld.so.conf /etc/ld.so.conf.d/* and execute sudo ldconfig

Comments

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.