Python3: Importerror: No Module Named '_Ctypes' When Using Value from Module Multiprocessing

No module named '_ctypes'

Required

Install foreign function interface headers

sudo apt install libffi-dev

Reinstall Python

Substitute desired python version

Ubuntu

sudo add-apt-repository ppa:deadsnakes/ppa -y && sudo apt reinstall python3.9-distutils

MacOS

Use brew install python3.9 or port install python3.9 (I recommend port)

Windows

Use Microsoft Store

Specify project python version

Poetry

poetry env use 3.9

Virtual envs

virtualenv -p python3.9 myproject

etc...

Msys2 with python 3.8: ImportError: cannot import name 'open_code' from 'io'

The ImportError: cannot import name 'open_code' from 'io' (unknown location) comes from the fact that there are two different versions of Python conflicting with each other. python still points to the old version 3.7 but PYTHONPATH got updated to point to the new 3.8 version. As the documentation of PYTHONPATH states, it becomes prepended to the module search path and hence shadows any builtin modules:

The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.

You can reproduce that behavior by creating two different virtual environments and then start one while having PYTHONPATH point to the other. In the following I used Miniconda to create two different environments, py37 and py38, containing a 3.7 and 3.8 installation respectively.

(py37) user@pc:~$ python --version
Python 3.7.6
(py37) user@pc:~$ PYTHONPATH=~/miniconda3/envs/py38/lib/python3.8/ python
Fatal Python error: init_sys_streams: can't initialize sys standard streams
Traceback (most recent call last):
File "/home/user/miniconda3/envs/py38/lib/python3.8/io.py", line 54, in <module>
ImportError: cannot import name 'open_code' from 'io' (unknown location)
Aborted (core dumped)

Multiprocessing sends value of the object instead of the object as an argument when passed alone

The args parameter must be a tuple. (communication_line) is not a tuple. A comma makes a tuple. Parentheses are only needed for order of operations. Consider 1, (1), ((1)) are all integer one. But 1,, (1,) and ((1,)) are all tuples. Use args = (communication_line,).

Or another example:

>>> x=1,
>>> type(x)
<class 'tuple'>
>>> x=(1)
>>> type(x)
<class 'int'>


Related Topics



Leave a reply



Submit