1

I have a Python 2 script that I'm trying to run within the latest Python docker container. That container supports Python 3, and I thought could manually adapt the script. My manual adaptations worked just fine... except for this:

The original Python 2 script contained this line:

from multiprocessing import Queue, Manager, Lock

But thanks to post like this and this, I know that "Queue" isn't a Python 3 module; the module I need is "queue". So I changed my code to this:

import queue
from multiprocessing import Manager, Lock

When I spin up my container then run the script (within the container), I get this:

Traceback (most recent call last):
  File "/usr/local/bin/myscript", line 4, in <module>
    __import__('pkg_resources').run_script('myscript==0.1.0', 'myscript')
  File "/usr/local/lib/python3.9/site-packages/pkg_resources/__init__.py", line 651, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/usr/local/lib/python3.9/site-packages/pkg_resources/__init__.py", line 1455, in run_script
    exec(script_code, namespace, namespace)
  File "/usr/local/lib/python3.9/site-packages/myscript-0.1.0-py3.9.egg/EGG-INFO/scripts/myscript", line 21, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/usr/local/lib/python3.9/site-packages/myscript-0.1.0-py3.9.egg/openbmp/myscript/logger.py", line 15, in <module>
ModuleNotFoundError: No module named 'Queue'

The above makes little sense to me; here's the referenced part of the script, with line numbers included:

15 import sys
16 import signal
17 #from multiprocessing import queue, Manager, Lock
18 import queue
19 from multiprocessing import Manager, Lock
20
21 from myscript.logger import LoggerThread

Ugh. The Docker container is adding as extra layer of complexity and I don't understand what's going on.

More experienced programmers: Is there a suitable fix here? Or should I just give up and use the Python 2 Docker container? Thank you.

Pete
  • 1,511
  • 2
  • 26
  • 49
  • 1
    There is a place that you still use "queue". Note that [```from multiprocessing import Queue```](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Queue) works on Python 3. Check where else you use Queue, maybe initializing. If you really want to use ```import queue``` maybe you could use as ```import queue as Queue``` – insightfulbit Dec 27 '22 at 15:48
  • @RenatoAlves Innnnnnneteresting! Yes, I'll bet you are right. I'll dig into it, thanks! – Pete Dec 27 '22 at 16:04
  • 1
    Your host system likely has a Python 3 installation already. Could you update the code to work with a modern version of Python in a non-Docker virtual environment? That would give you one fewer variable to work with. – David Maze Dec 27 '22 at 16:28
  • @DavidMaze Yeah, that's a good idea. The code generates some network traffic, so I was kinda hoping to get it working from inside the container off the bat. But you may be right, the error messages from within the container are too cryptic. Thanks! – Pete Dec 27 '22 at 16:36

1 Answers1

1

In your error message it says;

ModuleNotFoundError: No module named 'Queue'

"Queue" isn't a Python 3 module; the module I need is "queue"

Somehow, your code is stilling running this line:

from multiprocessing import Queue, Manager, Lock
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • Thanks Talha, That's crazy weird! But even when I entirely delete Line 17 (`#from multiprocessing import queue, Manager, Lock`), I still get a `ModuleNotFoundError: No module named 'Queue'` error message. There must be something else going on... Grrr.... – Pete Dec 27 '22 at 15:59
  • 1
    You're welcome...hmm this is weird. – Talha Tayyab Dec 27 '22 at 16:00
  • Yeah, this may be more trouble than its worth. I've never adapted a Python 2 script for a Python 3 machine, I'm starting to think that adaptation is a bad idea... – Pete Dec 27 '22 at 16:06