Windows正常运行的Python 3.5.0代码在Ubuntu 16.04遇OSError求助
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
gccfor C/C++ libraries). - Run
ls -lain your code directory to confirm the.sofile 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.soto 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:
- Create a custom config file:
sudo nano /etc/ld.so.conf.d/my_code_libs.conf - Add the full path to your code directory (e.g.,
/home/aiswarya/my_python_code) and save the file. - Run
sudo ldconfig -vagain—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
liband ending with.so—e.g.,libmylib.soinstead ofmylib.dll). - Always use absolute paths when loading the library to avoid relative path confusion (like the example in step 2).
内容的提问来源于stack exchange,提问作者Aiswarya M

