I'm currently using Python 3.7 and Pycharm for my work. Recently I got a code that is done at Python2.7

You shouldn't use Python 3.7 to run code that was written for Python 2.x, unless you can port that code to Python 3, or you know that it works fine. There are some caveats, and it depends on what the functionality of that code is. In some cases it might be very time consuming to convert it. 2to3 might help here.

So, depending on your use case, you may want to keep a legacy version of Python 2.7 around on your system to run that particular code, but note that Python 2.x will not get any (security) updates anymore, so you're better off using Python 3 from now on.

Try running the code in Python 3.7 and see if it works.

and it includes a library named 'ctypes.'

This is included by default in Python, so you don't need to install anything. Assuming you have the correct Python 2.7 interpreter and all other required packages, the code should just run fine as-is.

If the person who wrote the code was doing a good job, he or she should have left a README and/or a requirements.txt file or something similar that would specify which other packages or libraries are needed to run it.

Answer from slhck on Stack Exchange
🌐
Anaconda.org
anaconda.org › conda-forge › pywin32-ctypes
pywin32-ctypes - conda-forge | Anaconda.org
conda-forge/pywin32-ctypes · Community ... · Labels 3 · Badges · Versions · 0.2.3 · To install this package, run one of the following: $conda install conda-forge::pywin32-ctypes ·...
Top answer
1 of 2
2

I'm currently using Python 3.7 and Pycharm for my work. Recently I got a code that is done at Python2.7

You shouldn't use Python 3.7 to run code that was written for Python 2.x, unless you can port that code to Python 3, or you know that it works fine. There are some caveats, and it depends on what the functionality of that code is. In some cases it might be very time consuming to convert it. 2to3 might help here.

So, depending on your use case, you may want to keep a legacy version of Python 2.7 around on your system to run that particular code, but note that Python 2.x will not get any (security) updates anymore, so you're better off using Python 3 from now on.

Try running the code in Python 3.7 and see if it works.

and it includes a library named 'ctypes.'

This is included by default in Python, so you don't need to install anything. Assuming you have the correct Python 2.7 interpreter and all other required packages, the code should just run fine as-is.

If the person who wrote the code was doing a good job, he or she should have left a README and/or a requirements.txt file or something similar that would specify which other packages or libraries are needed to run it.

2 of 2
0

This S.O question has an answer which says

You don't need to install ctypes at all; it is part of the Python standard library, as of Python 2.5 onwards. See the module documentation.

which was provided by @MartijnPieters who has over 700k rep and so, presumably, knows what he is talking about.

🌐
Anaconda
anaconda.org › Esri › pywin32-ctypes
Pywin32 Ctypes - conda install
Conda · Files · Labels · Badges · License: 3-clause BSD · Home: https://github.com/enthought/pywin32-ctypes · 386298 total downloads · Last upload: 1 year and 8 months ago · win-64 v0.2.0 · To install this package run one of the following: conda install esri::pywin32-ctypes
🌐
GitHub
github.com › conda-forge › pywin32-ctypes-feedstock
GitHub - conda-forge/pywin32-ctypes-feedstock: A conda-smithy repository for pywin32-ctypes.
# Search all versions available on your platform: mamba repoquery search pywin32-ctypes --channel conda-forge # List packages depending on `pywin32-ctypes`: mamba repoquery whoneeds pywin32-ctypes --channel conda-forge # List dependencies of `pywin32-ctypes`: mamba repoquery depends pywin32-ctypes --channel conda-forge · conda-forge is a community-led conda channel of installable packages.
Starred by 2 users
Forked by 6 users
Top answer
1 of 2
1

In the conda documentation for using shared libraries, different approaches are described depending on the operating system. On Linux, shared libraries in conda packages typically have a modified rpath (using patchelf). For example:

>>> import importlib.util
>>> importlib.util.find_spec("_ctypes").origin
'<$CONDA_PREFIX>/lib/python3.10/lib-dynload/_ctypes.cpython-310-x86_64-linux-gnu.so'
CONDA_PREFIX/lib/python3.10/lib-dynload/
$ ldd _ctypes.cpython-310-x86_64-linux-gnu.so
        linux-vdso.so.1 =>  (0x00007ffe565c0000)
        libffi.so.8 => not found
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fadad8de000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fadad6c2000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fadad2f4000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fadadae2000)

Since libffi is not found, in python an import ctypes will give the error: ImportError: libffi.so.8: cannot open shared object file: No such file or directory. Now, libffi is installed as a conda package in this case, so

$ patchelf --set-rpath '$ORIGIN/../..' _ctypes.cpython-310-x86_64-linux-gnu.so

will fix that problem. Now if I create a shared library test.c:

#include <stdio.h>

void special()
{
    printf("special\n");
}

and compile it i.e. cc -shared -fPIC -o special.so test.c, Then special.so will exist in this directory but ctypes won't be able to find it. Now, if I do cp special.so $CONDA_PREFIX/lib, then in Python loading it works fine:

>>> import ctypes
>>> ctypes.cdll.LoadLibrary("special.so")
<CDLL 'special.so', handle f86430 at 0x7ff44691e5f0>
>>> libsp = ctypes.CDLL("special.so")
>>> x = libsp.special()
special

On windows, to answer your specific question, the documentation states:

This path [(install prefix)\\Library\\bin] is added to os.environ["PATH"] for all Python processes, so that DLLs can be located, regardless of the value of the system's PATH environment variable. This is the difference between the "raw" and the conda installation.

See also: How does conda work internally?

2 of 2
0

You're actually being caught out by the addition of os.add_dll_directory in Python 3.8, and the associated change to only load DLLs from safe directories, and the fact that Anaconda undid that change for their own builds.

Essentially, using the PATH environment variable to choose code to execute is these days considered insecure. Windows allows it by default in regular installs, but it can be disabled by a parent process or an administrator. To avoid the potential confusion due to inconsistent behaviour, Python 3.8 started changing the default itself. That way, users would know by checking for os.add_dll_directory whether they could (probably) modify PATH or if they should use the proper function.

Anaconda found that too many libraries broke with this change, and rather than fixing the libraries, decided to change it back for their build of Python. As of 3.10, they are no longer doing this and the behaviour should be consistent.

As a workaround, if you load your DLL using the full path instead of just the name, it will work consistently everywhere.


As for your example with ctypes: DLLs can only be loaded once-per-process. After it has been loaded once, attempting to load it again will succeed regardless of the settings used. So using the (unpatched) ctypes from Anaconda succeeds, and then the (patched) ctypes doesn't even consider search paths because the DLL is already in memory.

If you tried to load a different DLL the second time, it should fail again.

🌐
Anaconda
anaconda.org › channels › conda-forge › packages › pywin32-ctypes › overview
Anaconda.org
conda-forge/pywin32-ctypes · Community ... · Labels 3 · Badges · Versions · 0.2.3 · To install this package, run one of the following: $conda install conda-forge::pywin32-ctypes ·...
🌐
Python
docs.python.org › 3 › library › ctypes.html
ctypes — A foreign function library for Python
Source code: Lib/ctypes ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these ...
🌐
GitHub
github.com › conda › conda › issues › 12531
ImportError: DLL load failed while importing _ctypes: Can't find specified module. · Issue #12531 · conda/conda
March 25, 2023 - Traceback (most recent call last): ... get_free_space_on_windows as get_free_space File "C:\ProgramData\Anaconda3\lib\site-packages\conda\common\_os\windows.py", line 12, in <module> from ctypes import (POINTER, Structure, WinError, byref, c_ulong, c_char_p, c_int, c_ulonglong, File ...
Author   Fradhyle
Find elsewhere
🌐
Libraries.io
libraries.io › conda › pywin32-ctypes
pywin32-ctypes 0.2.2 on conda - Libraries.io - security & maintenance data for open source software
June 27, 2019 - The default behaviour will try to use cffi (>= 1.3.0), if available, and fall back to using ctypes. Homepage conda Python Download · License · BSD-3-Clause · Install · conda install -c anaconda pywin32-ctypes ·
🌐
Readthedocs
gpib-ctypes.readthedocs.io › en › latest › installation.html
Installation — gpib-ctypes 0.1.0dev documentation
If you don’t have pip installed, this Python installation guide can guide you through the process. The sources for gpib-ctypes can be downloaded from the Github repo.
🌐
Stack Overflow
stackoverflow.com › questions › 59008914 › pywin32-ctypes-error-in-32bit-python-3-7-conda-env
x86 - pywin32-ctypes error in 32bit python 3.7 conda env - Stack Overflow
pyinstaller works on x64 conda envs just fine. but attempting to run pyinstaller in a 32bit conda env causes the error: PyInstaller cannot check for assembly dependencies. Please install pywin32-ctypes.
🌐
Stan Forums
discourse.mc-stan.org › interfaces › cmdstan
DLL issue using CmdStanPy on Win10 with Jupyter: `ImportError: DLL load failed while importing _ctypes` - CmdStan - The Stan Forums
June 22, 2022 - I’ve set up CmdStanPy on Windows 10 using Anaconda (in a dedicated env). C++ toolchain is working, and I can execute the bernoulli example via cmdline. I’d like to set up jupyter in this env to conveniently interface wi…
🌐
GitHub
github.com › conda › conda › issues › 1313
Importing ctypes causes an ImportError on Windows 64-bit (Python 3.4) · Issue #1313 · conda/conda
May 1, 2015 - That may be the case, but again this is a fresh install. C:\Users\continuum\Anaconda3>python Python 3.4.3 |Anaconda 2.2.0 (64-bit)| (default, Mar 6 2015, 12:06:10) [MSC v.1600 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import ctypes Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\continuum\Anaconda3\lib\ctypes\__init__.py", line 7, in <module> from _ctypes import Union, Structure, Array ImportError: DLL load failed: %1 is not a valid Win32 application.
Author   mcg1969
🌐
PyPI
pypi.org › project › ctypeslib2
ctypeslib2 · PyPI
and install ctypeslib2 and the corresponding Python bindings to clang with pip ... (In theory it is possible to install different versions of clang with e.g. conda. To avoid invoking installing the XCode Command Line Tools or invoking xcrun ...
      » pip install ctypeslib2
    
Published   Feb 19, 2025
Version   2.4.0
🌐
Finxter
blog.finxter.com › how-to-check-ctypes-package-version-in-python
How to Check 'ctypes' Package Version in Python?
July 18, 2022 - Accelerate your career. Use English words to tell AIs what you want. You are only limited by your imagination · More than 100,000 AI enthusiasts stay on top of things with our free newsletter from companies such as:
🌐
CodersLegacy
coderslegacy.com › home › python › how to install ctypes in python
How to Install ctypes in Python - CodersLegacy
July 8, 2022 - In this tutorial we will explore how to install the ctypes Library in Python. It may come as a surprise to some, but ctypes has been....
🌐
Anaconda.org
anaconda.org › conda-forge › ctypesgen
Ctypesgen | Anaconda.org
Conda · Files · Labels · Badges ... 10 months and 3 days ago · noarch v1.1.1 · To install this package run one of the following: conda install conda-forge::ctypesgen ·...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-fix-modulenotfounderror-no-module-named-ctypes
How to Fix ModuleNotFoundError: No Module Named '_ctypes' - GeeksforGeeks
July 23, 2025 - Install Python ensuring that check the box to the add Python to the PATH during installation. ... Ensure that we Python environment is correctly set up and that the Python executable is in the system's PATH. we can verify this by running: ... This command should return the path to the Python executable. If it doesn't we may need to add Python to the PATH. If we are using a virtual environment the issue might be with the virtual environment setup. The Try creating a new virtual environment: ... try: import _ctypes print("The _ctypes module is available.") except ModuleNotFoundError: print("The _ctypes module is not available.")