Creating a multiprocessing queue throws an error - python

I wrote a very simple program to try to demonstrate multiprocessing queues. However, it throws an error when the queue is created.
import multiprocessing as mp
q = mp.Queue() #error right here
q.put(1)
print(q.get())
I get this message.
Traceback (most recent call last):
File "C:\Users\User\Documents\python\mptest.py", line 2, in <module>
q = mp.Queue()
File "C:\Python33\lib\multiprocessing\__init__.py", line 200, in Queue
from multiprocessing.queues import Queue
File "C:\Python33\lib\multiprocessing\queues.py", line 22, in <module>
from multiprocessing.connection import Pipe
File "C:\Python33\lib\multiprocessing\connection.py", line 21, in <module>
import tempfile
File "C:\Python33\lib\tempfile.py", line 35, in <module>
from random import Random as _Random
ImportError: cannot import name Random
I've never seen this happen before and a google search yielded no results. It only happens when it's ran from the command line. It runs perfectly fine in IDLE. I'm using Python 3.3.2.

I think its more to do with the version of python installed. I tried your code on cmd line as well as IDE but I got no errors. I have python 2.7 and 3.2 installed. But just to replicate your problem, I installed 3.3.2. I got the same error as you as I probably have random.py file too.

Related

What is a correct way to import "RPi.GPIO as GPIO" for use in a python thread?

I have a python program to which I am adding GPIO pin control functionality. It is a multi-threaded system that runs fine until I try to do an "import RPi.GPIO as GPIO". Then I get the error below:
Traceback (most recent call last):
File "/usr/local/System/main.py", line 20, in <module>
from LightUpAlarm import AlarmCli
File "/usr/local/System/LightUpAlarm/AlarmCli.py", line 26, in <module>
from AlarmManager import AlarmManager
ImportError: No module named AlarmManager
The module "AlarmManager" was available until I tried to import "RPi.GPIO". I would appreciate any help understanding what I am doing wrong. Thanks in advance.
Pastebin link to full python file generating error. AlarmThread.py

ImportError: No module named 'Queue' [duplicate]

This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed last month.
I am trying to import requests module, but I got this error
my python version is 3.4 running on ubuntu 14.04
>>> import requests
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 10, in <module>
from queue import LifoQueue, Empty, Full
ImportError: cannot import name 'LifoQueue'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/requests/__init__.py", line 58, in <module>
from . import utils
File "/usr/local/lib/python3.4/dist-packages/requests/utils.py", line 26, in <module>
from .compat import parse_http_list as _parse_list_header
File "/usr/local/lib/python3.4/dist-packages/requests/compat.py", line 7, in <module>
from .packages import chardet
File "/usr/local/lib/python3.4/dist-packages/requests/packages/__init__.py", line 3, in <module>
from . import urllib3
File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/__init__.py", line 10, in <module>
from .connectionpool import (
File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 12, in <module>
from Queue import LifoQueue, Empty, Full
ImportError: No module named 'Queue'
import queue is lowercase q in Python 3.
Change Q to q and it will be fine.
(See code in https://stackoverflow.com/a/29688081/632951 for smart switching.)
Queue is in the multiprocessing module so:
from multiprocessing import Queue
I solve the problem my issue was I had file named queue.py in the same directory
It's because of the Python version. In Python 2.x it's import Queue as queue; on the contrary in Python 3 it's import queue. If you want it for both environments you may use something below as mentioned here
try:
import queue
except ImportError:
import Queue as queue
In my case it should be:
from multiprocessing import JoinableQueue
Since in python2, Queue has methods like .task_done(), but in python3 multiprocessing.Queue doesn't have this method, and multiprocessing.JoinableQueue does.
I run into the same problem and learn that queue module defines classes and exceptions, that defines the public methods (Queue Objects).
Ex.
workQueue = queue.Queue(10)
I just copy the file name Queue.py in the */lib/python2.7/ to queue.py and that solved my problem.

Python, Windows, and Multiprocessing

I have a Python program originally built on and for Linux, which I'm now trying to port over to Windows. I am running the program in a virtual environment which contains all of the dependencies (my program is installed as a wheel with pip install --find-links wheels my_module). The program is launched with
(venv) C:\>venv\Scripts\python.exe -m base_module.Launcher arg1 arg2
The base_module loads my module as interpreted by the arguments provided, and his relevant code is:
from multiprocessing.managers import SyncManager
import OtherCustomClass
class BaseModule(object):
def __init__(self, arg1, arg2):
self.manager = SyncManager()
self.manager.start(ignore_interrupt)
def main(argv=None):
ret = -1
try:
basmod = BaseModule(argv[0], argv[1])
ret = basmod.run()
except Exception, err:
print("error: " + str(err))
print(traceback.format_exc())
return ret
if __name__ == "__main__":
exitCode = main(sys.argv[1:])
sys.exit(exitCode)
This has worked fine in Linux, but on Windows I get the following exception:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python27\Lib\multiprocessing\forking.py", line 380, in main
prepare(preparation_data)
File "C:\Python27\Lib\multiprocessing\forking.py", line 505, in prepare
'__parents_main__', file, path_name, etc
File "build/bdist.linux-x86_64/egg/base_module/BaseModule.py", line 2, in <module>
ImportError: No module named OtherCustomClass
exception in main:
Traceback (most recent call last):
File "build/bdist.linux-x86_64/egg/base_module/BaseModule.py", line 12, in main
File "build/bdist.linux-x86_64/egg/base_module/BaseModule.py", line 7, in __init__
File "C:\Python27\Lib\multiprocessing\managers.py", line 528, in start
self._address = reader.recv()
EOFError
The latter EOFError is caused by the unexpected early termination from the forking in SyncManager, where the true error is being unable to import OtherCustomClass. I have confirmed that OtherCustomClass exists in the base_module's folder within venv/lib/site-packages, and this error isn't happening when I launch the module first as Python would never reach the instructions in main() or init if the script wouldn't compile.
I've done some research, and I know this problem has hit other people (often using third party libraries, who fixed the issue without posting the solution). It seems to trace back to Windows' lack of a fork(), and python's handling of multiprocessing on Windows - see also http://docs.python.org/library/multiprocessing.html#windows. But I'm at a loss as to how to fix this.
This is the latest Python 2.7 branch (2.7.8), running on Windows 7 x64.
You can work around this by using an absolute import for OtherCustomClass:
from base_module import OtherCustomClass
I'm not exactly sure why, but it seems that when multiprocessing spawns a new process and imports your __main__, it's not able to handle the implicit relative import you're using with OtherCustomClass. If you explicitly import it from base_module, it works fine. My guess is that the spawned child process is not recognized as being part of the base_module package, so the implicit import fails, but that's just a guess.
Note that you shouldn't be using implicit relative imports anyway (they're altogether removed from Python 3), so switching to an absolute import isn't a bad thing.
Also of note, that doing an explicit relative import works on Python 3.4:
from . import OtherCustomClass
But it fails on Python 2.7:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\python27\lib\multiprocessing\forking.py", line 380, in main
prepare(preparation_data)
File "C:\python27\lib\multiprocessing\forking.py", line 495, in prepare
'__parents_main__', file, path_name, etc
File "C:\Users\oreild1\Desktop\base_module\Launcher.py", line 5, in <module>
from . import OtherCustomClass
ValueError: Attempted relative import in non-package
error:
Traceback (most recent call last):
File "C:\Users\oreild1\Desktop\base_module\Launcher.py", line 18, in main
basmod = BaseModule(argv[0], argv[1])
File "C:\Users\oreild1\Desktop\base_module\Launcher.py", line 10, in __init__
self.manager.start()
File "C:\python27\lib\multiprocessing\managers.py", line 528, in start
self._address = reader.recv()
EOFError

ZeroVM import error: No module named thread

When working on a small ZeroVM application that will run on ZeroCloud (that is, the ZeroVM integration with Swift), and trying to import the multiprocessing module, I get an error:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import multiprocessing
File "/lib/python2.7/multiprocessing/__init__.py", line 65, in <module>
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/lib/python2.7/multiprocessing/util.py", line 38, in <module>
import threading # we want threading to install it's
File "/lib/python2.7/threading.py", line 6, in <module>
import thread
ImportError: No module named thread
Why is that?
There is currently no support for threading or multiprocessing in ZeroVM. The Python status document lists both modules as unsupported.
The ZeroVM platform is a single process, single threaded environment. However, there is a pthreads port available. Instead of normal premeptive multithreading, this port uses cooperative multitasking instead. It might be possible to use this to enable some form of threading in the ZeroVM Python port.

Old error message keep popping up on IDLE

For some reason, anytime I try to import a python library, a particular old error message keeps popping up.
However, everything works fine when working with shell.
I know this is not a real programming question, but I'm stuck and would appreciate any help.
Any ideas on how to fix this?
Error message
>>> import nltk
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import nltk
File "C:\Python27\lib\site-packages\nltk\__init__.py", line 91, in <module>
from internals import config_java
File "C:\Python27\lib\site-packages\nltk\internals.py", line 22, in <module>
except ImportError: from xml.etree import ElementTree
ImportError: No module named etree
Does "C:\Python27\lib\xml\etree\ElementTree.py" exist in your computer?
Since IDLE and shell behaves differently, you may try the two lines in IDLE and Shell:
import sys
sys.path
Then check the difference of env path

Categories

Resources