ImportError: cannot import name process (Twisted) - python

I'm using Mac OS X 10.9.1, and I remember installing Python some time ago (I can't remember why, given that Mac OS X comes with it). For some reason Twisted wasn't installed, so I installed Zope and Twisted.
I'm following this tutorial: http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server
The problem is, when I run this code:
from twisted.internet.protocol import Factory,Protocol
from twisted.internet import reactor
class IphoneChat(Protocol):
def connectionMade(self):
print "a client connected"
factory = Factory()
factory.protocol = IphoneChat
reactor.listenTCP(80, factory)
print "Iphone Chat server started"
reactor().run
I get this error:
Traceback (most recent call last):
File "/Users/Mattieman/Desktop/server.py", line 4, in <module>
from twisted.internet import reactor
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/reactor.py", line 38, in <module>
from twisted.internet import default
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/default.py", line 56, in <module>
install = _getInstallFunction(platform)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/default.py", line 52, in _getInstallFunction
from twisted.internet.selectreactor import install
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/selectreactor.py", line 18, in <module>
from twisted.internet import posixbase
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/posixbase.py", line 53, in <module>
from twisted.internet import process, _signals
ImportError: cannot import name process
So I took a look in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet
...and it appears that process.py is missing.
What do I do?
There seem to be other files missing as well, like _baseprocess.py
I used setup3.py, if it helps...

You're using Python 2.6. You shouldn't use setup3.py. You also shouldn't ever use a distutils setup.py-type script to install Python libraries into OS paths (like /Library/Frameworks/Python.framework/Versions/2.6/).
You may have a randomly corrupted Twisted installation on your system now (OS X itself ships with a copy of Twisted). Or you may just have some garbage in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted that you need to clean up. It's difficult to say which (which is why you should never use setup.py to install things - it leaves a big mess).
After you clean up that mess (I can't give you many tips on how to do this apart from "reinstall your OS X", sorry), you have a few options:
Use the version of Twisted distributed with your OS. Don't try to install a different version.
Treat your home directory as a simpler "virtual environment" and run setup.py install --user (--user tells it to leave its mess inside ~/.local where at least it won't ruin your OS install).
Create a virtualenv and install a new version of Twisted there
Of these, you probably want to go with virtualenv.
Also note that you shouldn't run setup3.py unless you're trying to install Twisted for Python 3. And if you're trying to do that then I'll warn you that only part of Twisted is ported - and it's a very small part. For example, it's not process support (hence the import error you're getting).

Related

Python/twisted importing twisted.internet.endpoints on windows

I am trying to write a server using twisted on python.
This is the head of my file :
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor
The first and the last import work perfectly. I get an error when trying to run the second import with the following traceback :
Traceback (most recent call last):
File "<ipython-input-1-c0d6286e105b>", line 1, in <module>
from twisted.internet.endpoints import TCP4ServerEndpoint
File "C:\Anaconda3\lib\site-packages\twisted-15.5.0-py3.4.egg\twisted\internet\endpoints.py", line 34, in <module>
from twisted.internet.stdio import StandardIO, PipeAddress
File "C:\Anaconda3\lib\site-packages\twisted-15.5.0-py3.4.egg\twisted\internet\stdio.py", line 30, in <module>
from twisted.internet import _win32stdio
ImportError: cannot import name '_win32stdio'
I've already tried solutions like installing pypiwin32 both manually (using whl file) and with pip install. But the problem is not solved.
I am working on windows 7 (yes !) with python 3.4.3 and Twisted 15.0
Thank you for your help.
You are using Python 3, and _win32stdio is not ported to Python 3. If you want the full set of Twisted functionality, you have to run Python 2 (PyPy 4.x+ recommended) for now.
You could install twisted-win with:
pip install twisted-win
From description:
Windows compatibility for Twisted, specifically for Scrapy
It works for me for win 7, python 3.5.2.

cannot import Python-Twitter

I recently started out with python and i'm right now looking to do an app that shows my Twitter feed. So I downloaded a module(not sure if its called that) called Python-Twitter and installed it. But now everytime I try to import it I just get this error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import twitter
File "build\bdist.win32\egg\twitter.py", line 38, in <module>
File "C:\Python27\lib\site-packages\requests_oauthlib-0.4.0-py2.7.egg\requests_oauthlib\__init__.py", line 1, in <module>
from .oauth1_auth import OAuth1
File "C:\Python27\lib\site-packages\requests_oauthlib-0.4.0-py2.7.egg\requests_oauthlib\oauth1_auth.py", line 4, in <module>
from requests.utils import to_native_string
ImportError: cannot import name to_native_string
Does anyone know what I might have done wrong with the install or something? Thx
In my case installing requests-oauthlib fixed it
sudo pip install requests-oauthlib
This is because of requests version.
requests.utils.to_native_string is available since requests 2.0.0
So just update requests:
pip install -U requests
See more details here on another thread
This sounds like an issue with your install. The method to_native_string should be defined in the requests\utils.py in your Python install.
I'm on Ubuntu, and installed the Twitter module, and can import it without any errors. When I look in my install, in /usr/lib/python2.7/dist-packages/requests/utils.py, there is a to_native_string method defined there.
The implication from the error you're getting is that in your installation, there is either no utils.py, or, if there is, it does not contain that method.
I would recommend checking your install to see if that is the case. If it is indeed missing, I would recommend axing your install and reinstalling. You might want to try a virtualenv environment instead, so it can act as a sandbox (in that case you can leave your current install as is, as long as it is intact enough to run virtualenv and pip).
If utils.py is actually there and does contain a method with that name, I would recommend running a debugger such as pudb or pdb (pudb isn't built in, but it's more full featured), and step through the import to see if that sheds any additional light.

global name 'GLib2Reactor' is not defined

I'm struggling to get some python code using the python-brisa framework to work, the code is not written by me but should be straight forward.
from brisa.core.reactors import install_default_reactor
reactor = install_default_reactor()
from brisa.core.threaded_call import run_async_function
import xml.etree.ElementTree as ET
from time import sleep
import sys, os
import sonos
import knx
Howewer after installing the frameworks I get
Traceback (most recent call last):
File "knxsonos.py", line 24, in <module>
reactor = install_default_reactor()
File "/usr/local/lib/python2.7/dist-packages/brisa/core/reactors/__init__.py", line 14, in install_default_reactor
return GLib2Reactor()
NameError: global name 'GLib2Reactor' is not defined
I have been looking both stack overflow, and googling for days without finding a solution.
Anyone??, help would be greatly appreciated...
Here's some possibilities:
GLib2Reactor doesn't return anything - Then your code is wrong
GLib2Reactor is not declared - try this:
x = GLib2Reactor()
return x
GLib2Reactor has to be imported - just import it
my best advice: read the docs
When I saw this problem on an Ubuntu Trusty (14.04) system, it was caused by import gobject failing. The fix was to install the correct package:
sudo apt-get install python-gobject-2
This package is marked as deprecated, so on newer distributions it might be necessary to install the python-gi package instead, and then modify the callers to use the new names:
from gi.repository import GObject

Error during library import in Python (Windows)

I am trying to configure svn hook for email notification using mailer.py script from apache, but it always crash on import of svn libs. I try two ways of installing svn and python. I try to install everything manually using command line and use CollableNet installer that have preinstalled Python and all libs I need but result is the same.
Now I try to import one library from hole package that I need using python in command line and I have such response:
>>> import svn.core
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named svn.core
I try to fix it using:
1) import sys; sys.path.append("C:\csvn\lib\svn-python\svn")
and
2) C:\csvn\lib\svn-python\svn>python "\__init__.py" install
But it didn't help.
For now I have no package with setup.py to install it.
I spend a lot of time on this task, and going crazy a little). Please give me any suggestion how to fix it.

Windows XP GTK App Glib Import Error

I am trying to run my GTK app on Windows XP and I am having troubles with an import. I have installed the following as needed and recommended:
python-2.6.msi
gtk2-runtime.2-16.6.exe
pycairo-1.8.6.exe
pygobject-2.20.0
pygtk-2.16.0.exe
pyserial-2.5.exe
However, I am getting the following error when running my script:
Traceback (most recent call last):
File "C:\python26\app\src\start.py", line 22, in <module>
import gtk
File "C:\Python26\Lib\site-packages\gtk-2.0\gtk\__init__.py", line 30, in <mod
ule>
import gobject as _gobject
File "C:\Python26\Lib\site-packages\gtk-2.0\gobject\__init__.py", line 26, in
<module>
from glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \
File "C:\Python26\Lib\site-packages\gtk-2.0\glib\__init__.py", line 22, in <mo
dule>
from glib._glib import *
ImportError: DLL load failed: The specified module could not be found.
GTK is installed into C:\gtk\bin and is setup in my System PATH.
Am I missing something obvious here?
Any help would be appreciated.
Thank-you kindly.
Andy
I was getting the same issue - I'm not sure why, but I suspect it has something to do with some of the install/lookup paths. I tried adding all sorts of stuff to my path, but nothing seemed to work.
What I did to fix it was to uninstall python et. al. and reinstall using the Python (x,y) package. It's a fairly large download, but it has tons of tools, including several (matplotlib, numpy, scipy, IPython, etc) that I needed/wanted.
You could install pygtk bundle found in this page:
http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.22/
where you should find the "all in one" installer suited to your version.
It solved the problem to me without having to install Py(x,y), which seems to be a very usefull scientific environment for windows (I use linux and install all these packages from script).

Categories

Resources