You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows正常运行的Python 3.5.0代码在Ubuntu 16.04遇OSError求助

Fixing OSError: loadLibrary: cannot open shared object file on Ubuntu 16.04

Hey there, let's work through this frustrating cross-platform library issue you're facing! Moving code from Windows to Linux often hits snags with shared libraries, since the two systems use completely different binary formats (.dll vs .so). Let's go through the most likely fixes step by step:

1. Verify you have the correct Linux-compatible shared library

Windows .dll files won't work on Linux at all—you need a Linux-specific .so (shared object) file. Here's what to check:

  • If you built the library yourself, recompile it directly on Ubuntu 16.04 using the appropriate compiler (like gcc for C/C++ libraries).
  • Run ls -la in your code directory to confirm the .so file exists. If it's missing, you'll need to obtain or build the Linux version of the library.
  • Make sure the library has executable permissions: run chmod +x libyourlibrary.so to set them if needed.

2. Tell the dynamic linker where to find your library

Even if the .so is in the same directory as your code, Linux's dynamic linker might not look there by default. Try these options:

Temporary fix (per session)

Before running your Python script, set the LD_LIBRARY_PATH environment variable to include your code directory:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/full/path/to/your/code/directory
python your_script.py

Permanent fix (in your code)

Add this at the top of your Python script to set the path dynamically before loading the library:

import os
# Get the directory of your current script
script_dir = os.path.dirname(os.path.abspath(__file__))
# Update the library path to include this directory
os.environ['LD_LIBRARY_PATH'] = f"{script_dir}:{os.environ.get('LD_LIBRARY_PATH', '')}"

# Now load your library
# Example using ctypes:
import ctypes
your_lib = ctypes.CDLL(os.path.join(script_dir, "libyourlibrary.so"))

3. Fix your ldconfig setup

Running sudo ldconfig -v only works for libraries in system-default paths (like /usr/lib or /lib). To make your code directory a permanent library path:

  1. Create a custom config file:
    sudo nano /etc/ld.so.conf.d/my_code_libs.conf
    
  2. Add the full path to your code directory (e.g., /home/aiswarya/my_python_code) and save the file.
  3. Run sudo ldconfig -v again—this will now scan your directory for shared libraries.

4. Check for missing dependencies in your library

Your .so file might depend on other system libraries that aren't installed on Ubuntu. Use the ldd command to check:

ldd libyourlibrary.so

Look for lines that say not found—for example:

libssl.so.1.0.0 => not found

If you see this, install the missing package using apt-get:

sudo apt-get install libssl1.0.0

5. Double-check your library loading code in Python

If you're using ctypes or a similar library to load the shared object:

  • Use the Linux library name (usually prefixed with lib and ending with .so—e.g., libmylib.so instead of mylib.dll).
  • Always use absolute paths when loading the library to avoid relative path confusion (like the example in step 2).

内容的提问来源于stack exchange,提问作者Aiswarya M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 13:18:14