While working the examples in a book on Parallel Programming in Python, I encountered the following error in code that uses the multiprocessing queue:
File "C:\pyDev\multiproc\queue-test.py", line 4, in <module>
queue = multiprocessing.Queue()
File "C:\Anaconda3\Lib\multiprocessing\context.py", line 100, in Queue
from .queues import Queue
File "C:\Anaconda3\Lib\multiprocessing\queues.py", line 20, in <module>
from queue import Empty, Full
builtins.ImportError: cannot import name 'Empty'
After some experimentation, I determined that all it takes to generate this error is the following code (which is the entirety of the queue-test.py file mentioned in the error message above).
import multiprocessing
if __name__ == "__main__":
queue = multiprocessing.Queue()
I'm running Python 3.5.1 with Anaconda 4.1.0 on a machine with Windows 7. I have ported the code above and the example from the book to another machine with Python 2.7.11 with Anaconda 2.5.0, with Windows 10 and it works fine on that machine.
Thinking that perhaps there was a corrupt file or other issue with my Python installation, I tried reinstalling Anaconda and it did not help. I performed a Google search and did not find this particular error. Other stackoverflow postings such as the one found here: ImportError: Cannot import name X don't appear to be relevent because this involves part of the Python standard library and not code and classes I wrote myself.
I encountered almost the same error in my code and finally figure out where went wrong. Hope it will help you somehow.
I named my python script as "queue.py", and then I run it, I got error info. below just like yours:
Traceback (most recent call last):
File "F:/02_Coding/01_Projects/PyHomeWork/Day23_Process/queue.py", line 19, in <module>
queue = multiprocessing.Queue()
File "E:\02_CodingSoftware\02_Installed\Anaconda3\lib\multiprocessing\context.py", line 101, in Queue
from .queues import Queue
File "E:\02_CodingSoftware\02_Installed\Anaconda3\lib\multiprocessing\queues.py", line 20, in <module>
from queue import Empty, Full
ImportError: cannot import name 'Empty'
I use the below method to create a Queue:
if __name__ =="__main__":
queue = multiprocessing.Queue()
Finally, I notice that I should not named the file in a name of "queue.py", it seems it will cause a misleading to python when it interpreter the script. And after I rename the script, the error is gone. What a stupid mistake, LoL.
So my suggestion is maybe you can check whether under your folder is there a script named as "queue.py" or any custom module would conflict with that in libraries.
Hope you can solve the issue. Best
Related
I'm learning Python 3.7, and Sublime Text3. Most programs run fine using control + b, but when I try to import a module from the standard library like random, I get the following error(Please note that I am not running the program from OneDrive, but from my own computer!)
Traceback (most recent call last):
File "C:\Users\chris\OneDrive\Documents\Python Programs\guess.py", line 1, in <module>
import random
File "C:\Users\chris\OneDrive\Documents\Python Programs\random.py", line 1
>>> import os
^
SyntaxError: invalid syntax
Obviously the OneDrive thing is a problem ,but what can I do?
Many thanks.
The part of the error message that you want to focus on here is the last part of the stack trace, which is showing you the line that has the problem and where the problem is, i.e.:
>>> import os
^
SyntaxError: invalid syntax
Based on this, your input file has the characters >>> in front of the import statement which, as the message indicates, is not valid Python syntax. Removing those characters should solve the problem for you. The line immediately above the error is showing you the name of the file that has the problem (in this case random.py).
The >>> is the prompt that the interactive Python interpreter gives you when you run it, as it's prompting you for what text to enter. That's not something that should appear in your code at all.
Check the random.py file, as the error message states, it's a syntax error due to the the >>> repl symbol in there.
After installing pyql from the package source(according to its own wizard described in this file) on Ubuntu 14.04 into /usr/local/lib/python2.7/dist-packages/ folder, all tests finished with success. This folder includes all additional installed packages, which are accessible, from the python using native import command.
But, this specific installation python don’t see, and I cannot import anything from it.
Have you any idea, what I need to define in addition?
Thanks,
Yigal B.
There are results of my sys.path:
import sys
print(sys.path)
['/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
python -c 'from quantlib.settings import __quantlib_version__; print __quantlib_version__'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named quantlib.settings
Can you post the content of you sys.path? And the output of
python -c 'from quantlib.settings import __quantlib_version__; print __quantlib_version__'
I am currently working with Gurobi solver using python. Previously, I have just been using a mix of a text editor and the terminal to write and run my models but I am currently experimenting with using Enthought Canopy Editor. I have a model that is works when I run in in the terminal using python model.py but when I try doing%run model.py in Canopy's iPython shell I get:
%run /home/cdhagmann/Copy/Code_Env/Farmer/model.py
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/home/cdhagmann/Canopy/appdata/canopy-1.0.1.1189.rh5-x86_64/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
181 else:
182 filename = fname
--> 183 __builtin__.execfile(filename, *where)
/home/cdhagmann/Copy/Code_Env/Farmer/model.py in <module>()
----> 1 from gurobipy import *
2 from data_IO import read_data,currency
3 import time
4
5 # Import model data
ImportError: No module named gurobipy
My guess is that the terminal and Canopy look for modules in different places but I don't know how to rectify it. I found this reference on the Gurobi group site about Enthought Canopy and Gurobi on OS X 10.8 but didn't know how much those instruction would differ from doing it on Linux and even what he meant about installing Gurobi in Canopy's site-packages.
UPDATE: Based on information found the first link I clicked on, I did in fact leave the boxed clicked making Canopy my default directory. You have to manually prepend the PATH variable, which I missed them saying that I had to do. So I did that. I now have Canopy's version of iPython running in my terminal (check by using sys.path); however, I get a huge list of errors when I try run python.
cdhagmann#Crispin ~ $ python
Traceback (most recent call last):
File "/usr/lib/python2.7/site.py", line 563, in <module>
main()
File "/usr/lib/python2.7/site.py", line 545, in main
known_paths = addusersitepackages(known_paths)
File "/usr/lib/python2.7/site.py", line 272, in addusersitepackages
user_site = getusersitepackages()
File "/usr/lib/python2.7/site.py", line 247, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/lib/python2.7/site.py", line 237, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/lib/python2.7/sysconfig.py", line 578, in get_config_var
return get_config_vars().get(name)
File "/usr/lib/python2.7/sysconfig.py", line 505, in get_config_vars
import re
File "/usr/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/usr/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/usr/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/usr/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
Suggestions?
And to answer #Jonathan questions:
sys.prefix = '/usr'
sys.path:
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
/usr/lib/pymodules/python2.7
/usr/lib/python2.7/dist-packages/ubuntu-sso-client
gurobipy.__file__ = '/usr/local/lib/python2.7/dist-packages/gurobipy/gurobipy.so'
If the terminal is correctly configured for Canopy Python, then python in terminal and Canopy Python should be looking for modules in the same places. If during installation, you disabled "make Canopy be default Python", or if you have not restarted terminal since installation, then it won't automatically be configured correctly.
These articles should help you understand and configure:
http://docs.enthought.com/canopy/configure/faq.html#where-are-all-of-the-python-packages-in-my-user-python-environment
https://support.enthought.com/entries/23646538-Make-Canopy-s-Python-be-your-default-Python-i-e-on-the-PATH-
https://support.enthought.com/entries/23389761-Installing-packages-into-Canopy-Python-from-the-command-line
If these articles do not suffice for you to solve this, then please report -- when you run python from terminal, what are these?
sys.prefix
sys.path
gurobipy.__file__
After installing Ming 0.3.2, I tested the installation by running the following code:
>>> from ming.datastore import DataStore
>>> bind = DataStore('mongodb://localhost:27017/', database='tutorial')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'database'
>>> ^D
I looked at the installation files and in the datastore.py file I found that the class's constructor did not contain a "database" argument.
class DataStore(object):
def __init__(self, bind, name, authenticate=None):
self.bind = bind
self.name = name
self._authenticate = authenticate
self._db = None
I then installed Ming 0.3.0 to look at the datastore.py file and found the DataStore class to match the documentation (it contained a database arg) and then tried that version where I encountered other complications.
I use easy_install to install Ming and I have a good install of mongodb and pymongo running. I run these on OS X Lion. Any advise on getting Ming running would be appreciated.
I think there may be a conflict with the newest version of pymongo and ming.
bind = DataStore('mongodb://localhost:27017/', name='test') gets me a bit further along, but I ended up just using pymongo by itself.
I've met the same issue. Here are the steps I've tried, and it works! Hopes it works for your environment too.
Uninstall the Ming 0.3.2 version by : pip uninstall Ming
Install 0.3.0 by: pip install -Iv http://downloads.sourceforge.net/project/merciless/0.3.0/Ming-0.3.0.tar.gz
Try the example on the Ming office website again. There will be another error
Traceback (most recent call last):
File "tutorial.py", line 1, in <module>
from ming.datastore import DataStore
File "/home/me/work/deploy/test/local/lib/python2.7/site-packages/ming/init.py", line 3, in <module>
from session import Session
File "/home/me/work/deploy/test/local/lib/python2.7/site-packages/ming/session.py", line 7, in <module>
from pymongo.son import SON
ImportError: No module named son
change the line 7 of "/home/me/work/deploy/test/local/lib/python2.7/site-packages/ming/session.py" to from bson.son import SON
try again. and it will works.
Here is the link I've referenced. It's a Japanese webpage, but you can translate it to English by google translator.
http://ryooo321.blogspot.com/2012/05/macsleepymongoose.html
try to remove database=.
In [8]: from ming.datastore import DataStore
In [9]: bind = DataStore('mongodb://grid:27017/', 'tutorial')
In [10]: bind.name
Out[10]: 'tutorial'
I'm using the execnet package to allow communication between Python scripts interpreted by different Python interpreters.
The following code (test_execnet.py):
import execnet
for python_version in ('python', 'python3'):
try:
gw = execnet.makegateway("popen//python="+python_version)
ch = gw.remote_exec('channel.send(1/3)')
res = ch.receive()
print(python_version, ': ', res, sep ="")
except:
print('problems with ', python_version)
Runs perfectly in the command-line Terminal, showing the following output:
$ python3 test_execnet.py
python: 0
python3: 0.333333333333
However, if I try to run the same code from within the Eclipse IDE, I get the following error:
'import site' failed; use -v for traceback
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 4, in <module>
File "<string>", line 2, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/site-packages/execnet/gateway_base.py", line 8, in <module>
import sys, os, weakref
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/os.py", line 380, in <module>
from _abcoll import MutableMapping # Can't use collections (bootstrap)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/_abcoll.py", line 54
class Hashable(metaclass=ABCMeta):
^
SyntaxError: invalid syntax
problems with python
problems with python3
NOTE:
Eclipse Version: 3.6.0
PyDev Interpreter configured for the project: python3
"Preferences/Interpreter - Python"'s Python Interpreters:
python (/usr/bin/python)
python3 (/Library/Frameworks/Python.Framework/Versions/3.1/Resources/Python.app/Contents/MacOS/Python
EDIT:
I write a code to show the os.environ like this:
for python_version in ('python', 'python3'):
try:
import os
for item in os.environ:
print(item, '= ', os.environ[item])
except:
print('problems with ', python_version)
I got the following outputs:
eclipse_output.txt
terminal_output.txt
A FileMerge comparison of the files can be found at eclipse_output.txt vs. terminal_output.pdf.
Any hints?
Thanks
seems like pydev does site-customizations and particularly modifies things for interactive/console usage (judging from a very quick skim of http://github.com/aptana/Pydev/blob/master/plugins/org.python.pydev/pysrc/pydev_sitecustomize/sitecustomize.py ). This is not useful or fitting for execnet-mediated processes.
You could try to "del os.environ['PYTHONPATH']" before you invoke execnet.makegateway, or, to be more careful, just delete the sitecustomize part of it.
hth,
holger
'import site' failed; use -v for traceback
I have seen that when python was unable to find its landmark. Which that indicates there is a PYTHONHOME problem.
Check out http://docs.python.org/using/cmdline.html#envvar-PYTHONHOME maybe eclipse is screwing your environment up.
Edit:
Looked at your env dumps, looks like eclipse is definitely messing with PYTHONPATH, which will cause your child python processes to not work correctly.
Basically what you have going on here is eclipse starts a python v2 instance with a PYTHONPATH pointing to the python v2 directories. Then you spawn a python v3 process which tries to load its landmark from the python v2 directories...
You need to find a way to have eclipse not mess with the PYTHONPATH. I am not sure what eclipse is trying to do by doing that, but it is certainly no friend when you want to spawn new python processes.