Multiprocessing issue 'Can't pickle
  • 11-Jun-2023
Lightrun Team
Author Lightrun Team
Multiprocessing issue 'Can't pickle

Multiprocessing issue ‘Can’t pickle
Lightrun Team
Lightrun Team

Explanation of the problem

The code snippet provided attempts to use the multiprocessing module in Python 3. The intention is to parallelize the execution of the do() function using a process pool. However, when running the code in ptpython on Python version 3, an error occurs during pickling of the do() function. The error traceback suggests that the pickling process fails to serialize the do() function, resulting in a PicklingError. The following is the problematic code:


from multiprocessing.pool import Pool

def do(x):
    return x 

pool = Pool(processes=16, maxtasksperchild=100)
done_x = []
for x in pool.imap_unordered(do, [1, 2, 3, 4, 5]):


The error indicates that the pickling process fails to look up the do() function in the __main__ module. This issue arises because the multiprocessing module relies on the functions being pickled to be defined in a separate module and imported, rather than being defined directly in the main script or interpreter session.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for: Multiprocessing issue ‘Can’t pickle <function ….’

To resolve this issue, the do() function can be defined in a separate module and imported into the main script. This ensures that the function can be properly pickled and used by the multiprocessing module. The following code demonstrates the modification:


from multiprocessing.pool import Pool
from mymodule import do

pool = Pool(processes=16, maxtasksperchild=100)
done_x = []
for x in pool.imap_unordered(do, [1, 2, 3, 4, 5]):


By defining the do() function in the mymodule module and importing it into the main script, the pickling error should be resolved, and the code should execute without any issues.


Other popular problems with ptpython

Problem 1: One common issue with ptpython is encountering a “ModuleNotFoundError” when trying to import certain modules. This problem typically occurs when ptpython is unable to locate the required module, either due to it not being installed or the module not being accessible in the current environment. Here’s an example of the error message:


ModuleNotFoundError: No module named 'numpy'


To resolve this problem, you need to ensure that the required module is installed in your Python environment. You can use the pip package manager to install the missing module. Here’s an example of how to install the “numpy” module:


!pip install numpy


After installing the necessary module, you should be able to import it successfully in ptpython.

Problem 2: Another common issue with ptpython is encountering an “IndentationError” when pasting indented code blocks. This problem occurs because ptpython interprets the indentation of pasted code differently from the regular Python interpreter. As a result, the indentation may be misaligned, leading to an indentation error. Here’s an example of the error message:


IndentationError: unexpected indent


To resolve this problem, you can use the ptipython command instead of ptpython. The ptipython command uses the IPython interpreter, which handles indentation correctly when pasting code blocks. By using ptipython instead of ptpython, you can avoid the indentation error.

Problem 3: Sometimes, ptpython may fail to start due to an incompatible Python version or conflicting dependencies. This issue can occur if you’re using an unsupported Python version or if there are conflicts between the dependencies required by ptpython and other packages installed in your environment. When encountering this problem, you might see an error message similar to the following:


ImportError: This version of ptpython only supports Python 3.x.x


To resolve this issue, ensure that you’re using a compatible Python version as indicated by the error message. If you’re using an unsupported version, consider upgrading to a supported version. Additionally, you can check for any conflicting dependencies by reviewing the package requirements for ptpython and other installed packages. If there are conflicts, you may need to manage your dependencies or create a separate virtual environment for ptpython to ensure a clean and compatible installation.

By addressing these common problems, you can enhance your experience with ptpython and ensure smooth and effective Python interactive sessions.

A brief introduction to ptpython

ptpython is an advanced Python REPL (Read-Eval-Print Loop) that offers enhanced features and capabilities compared to the standard Python interpreter. It provides an interactive environment for executing Python code, exploring modules, and experimenting with different Python constructs. With its rich set of features, ptpython aims to improve developer productivity and facilitate a more convenient and efficient coding experience.

One notable feature of ptpython is its powerful autocompletion functionality. It offers intelligent suggestions as you type, making it easier to discover and access available methods, attributes, and variables. This feature helps reduce the time spent on manual typing and enables faster code exploration. Additionally, ptpython supports syntax highlighting, allowing for better code readability and visual differentiation of various elements in your code.

Another valuable aspect of ptpython is its support for interactive documentation and help. It integrates with the Python standard library’s pydoc module to provide detailed documentation on functions, classes, and modules. By simply pressing the F2 key on a selected item, you can quickly access its documentation within the ptpython interface. This feature is particularly useful when exploring unfamiliar modules or trying to understand the usage of specific functions or classes.

Overall, ptpython offers an enhanced interactive Python environment with features such as autocompletion, syntax highlighting, and interactive documentation. These features empower developers to write code more efficiently, explore Python modules with ease, and gain insights into the usage and functionality of various Python constructs.

Most popular use cases for ptpython

  1. Enhanced Interactive Python Shell: ptpython provides an advanced interactive Python shell with features such as syntax highlighting, multiline editing, and command history navigation. It offers a more convenient and powerful alternative to the default Python REPL, allowing developers to experiment with code, debug issues, and explore Python modules in a more efficient manner. Here’s an example of ptpython’s syntax highlighting:


def greet(name):
    print("Hello, " + name)



  1. Intelligent Autocompletion: ptpython offers intelligent autocompletion that suggests Python keywords, module names, class attributes, function names, and variable names as you type. This feature saves time and reduces the chances of typographical errors. Developers can easily discover available options and navigate through complex module hierarchies without having to refer to external documentation. For instance, when working with the numpy module, ptpython can provide autocompletion suggestions for its functions and attributes:


import numpy as np

# Autocompletion for numpy functions and attributes
np.arra  # Suggestion: np.array
np.lin  # Suggestion: np.linspace


  1. Interactive Documentation: With ptpython, developers can access interactive documentation for Python objects directly within the shell. By pressing the F2 key on a selected object, ptpython retrieves and displays the relevant documentation, including the object’s docstring, available methods, and parameters. This feature helps in understanding the usage and functionality of Python objects without switching to external documentation sources. Here’s an example of accessing interactive documentation for the random module:


import random

# Accessing interactive documentation for the random module
random  # Press F2 to view documentation


In summary, ptpython offers an enhanced interactive Python shell with features like syntax highlighting, intelligent autocompletion, and interactive documentation. It provides developers with a more efficient and productive environment for Python coding, debugging, and exploration of Python modules.



It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.