Pyro4 connecting python2 to python3 - python-3.x

I have the following setup: Pyro nameserver running under python2.7, Pyro daemon running under python2.7, and (ideally) a Pyro client running python3.3.
If I connect to the server using a client running under python2.7 everything works fine. When using python3.3 for the client I can create the proxy connection just fine, but I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/project_path/lib/python3.3/site-packages/Pyro4/core.py", line 149,
return self.__send(self.__name, args, kwargs)
File "/project_path/lib/python3.3/site-packages/Pyro4/core.py", line 271,
self.__pyroCreateConnection()
File "/project_path/lib/python3.3/site-packages/Pyro4/core.py", line 322,
uri=resolve(self._pyroUri)
File "/project_path/lib/python3.3/site-packages/Pyro4/naming.py", line 336
nameserver=locateNS(uri.host, uri.port)
File "/project_path/lib/python3.3/site-packages/Pyro4/naming.py", line 274
proxy.ping()
File "/project_path/lib/python3.3/site-packages/Pyro4/core.py", line 149,
return self.__send(self.__name, args, kwargs)
File "/project_path/lib/python3.3/site-packages/Pyro4/core.py", line 290,
data=self._pyroSerializer.deserialize(data, compressed=flags & MessageFactory.FLAGS_COMPRESSED)
File "/project_path/lib/python3.3/site-packages/Pyro4/util.py", line 146,
return self.pickle.loads(data)
ImportError: No module named 'exceptions'
when trying to use any remote methods. The pyro docs seem to imply that I should be able to interconnect python2.7 instances with python3.3, is this not the case?
Both the 2.7 and 3.3 instance of python are using Pyro4 version 4.16
EDIT: Here's some actual code that's not working for me:
(With a python2 ns started)
In a python2.7 virtualenv:
import Pyro4
class TestProxy(object):
def foo(self):
return "bar"
if __name__ == "__main__":
print "* Starting test proxy"
daemon=Pyro4.Daemon()
tproxy_uri=daemon.register(TestProxy())
ns=Pyro4.locateNS()
ns.register("foo",tproxy_uri)
print "* Proxy started"
daemon.requestLoop()
and in a python3.3 virtualenv:
import Pyro4
rtest = Pyro4.Proxy("PYRONAME:foo")
print(rtest.foo())
I get that exact exception I put above.

The reason is pickle. It is the „protocol” used for exchanging object over the network.
That's why you cannot pass classes, but only objects and so on. Pickle for py3 has major changes, but 2.7 vs. 2.6 does not. So, in theory, pyro will work between 2.6 > 2.7, but not 2.7 > 3.3.
(it works, I have a VPS with python 2.6 and I am force to move client app on my comp on py2.7 too.) Probably a hack at pickle module may avoid the error, but I am sure that will open a pandora box. The only „real” alternative is to use a python server, over http/socket/https (not necesarly a web server) and send/recive serialized strings/commands.
P.S.: I see that the post is old!

What cox said is right. Pickle are different between python2,3. But serpent seems to be compatible.
You can try
PYRO_SERIALIZERS_ACCEPTED=serpent /usr/bin/python3 -Wignore -m Pyro4.naming

Related

pymodbus package import error - modulenotfound

Raspberry Pi 4 running Raspian (most current as of 02/09/2023), Python 3.9.2. Installed Pymodbus with pip3 install -U pymodbus[twisted] (detail: it said twisted was not available).
When I run my code in Python I get the error ModuleNotFoundError: No module named 'pymodbus.server.asynchronous' on the line from pymodbus.server.asynchronous import StartTcpServer, StopServer.
Any idea as why it is not working? Same process used to work in 2021 with Python 3.7.
Installed Pymodbus from this command pip3 install -U pymodbus[twisted]
EDIT: I read the official page for Pymodbus https://pypi.org/project/pymodbus/ which says that the current version of it is compatible with Python 3.8+ , and the old versions of it were compatible with up to Python 3.7 (which was the version I was using). Could this be breaking the code and giving me the error i'm getting?
EDIT 2: Full traceback
Traceback (most recent call last):
File "/home/pi/mohm-lhf/Program2.py", line 11, in <module>
from communication import MODBUS
File "/usr/lib/python3/dist-packages/shiboken2/files.dir/shibokensupport/__feature__.py", line 142, in _import
return original_import(name, *args, **kwargs)
File "/home/pi/mohm-lhf/communication.py", line 8, in <module>
import modbus_server as server
File "/usr/lib/python3/dist-packages/shiboken2/files.dir/shibokensupport/__feature__.py", line 142, in _import
return original_import(name, *args, **kwargs)
File "/home/pi/mohm-lhf/modbus_server.py", line 13, in <module>
from pymodbus.server.asynchronous import StartTcpServer, StopServer
File "/usr/lib/python3/dist-packages/shiboken2/files.dir/shibokensupport/__feature__.py", line 142, in _import
return original_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'pymodbus.server.asynchronous'
Twisted is removed from 3.0.0dev5, and I can see that you used this import line in your code:
from pymodbus.server.asynchronous import StartTcpServer, StopServer
This is an old implementation working only for 2.5.3<, there is a new way of implementing an async server - check here the example.

ImportError pyramid hello world program

source code:
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('<h1>Hello world!</h1>')
if __name__ == '__main__':
config = Configurator()
config.add_view(hello_world)
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
When I ran the sample hello_world program using pyramid, I got the following error.
Traceback (most recent call last):
File "application.py", line 2, in <module>
from pyramid.config import Configurator
File "/usr/local/lib/python2.7/dist-packages/pyramid/config/__init__.py", line 12, in <module>
from pyramid.interfaces import (
File "/usr/local/lib/python2.7/dist-packages/pyramid/interfaces.py", line 3, in <module>
from zope.interface import (
ImportError: No module named interface
You've installed something improperly - more than likely by using python setup.py develop somewhere instead of using pip install -e .. If you mix tools you're going to have some issues. This particular one seems to be due to namespace packages not being configured correctly which almost always is a symptom of using easy_install and pip in the same environment. You need to pick one (preferably pip), and sometimes the decision about which one to use was already made by whatever tool installed python for you.

py2app builds ok but app fails with "_tkinter.TclError" (and no error message!)

I am using py2app 0.9 on Mac OSX Yosemite 10.10.1 running Python 3.4 from the anaconda distribution and with Tcl 8.5.
In earlier attempts, the build would fail, but quick searches revealed solutions to these problems (i.e. including 'packages': ['tkinter', 'matplotlib'] in OPTIONS in setup.py, and changing line 49 of MachOGraph.py: loader --> loader_path)
Now py2app finishes the build, and running in Alias mode my app functions, but when I build in normal mode (python setup.py py2app) the resulting app will not open and the console shows the following traceback:
Traceback (most recent call last): File
"/Users/ryankeenan/Desktop/fishing/gui_test/dist/deani.app/Contents/Resources/boot.py",
line 355, in _run() File
"/Users/ryankeenan/Desktop/fishing/gui_test/dist/deani.app/Contents/Resources/boot.py",
line 336, in _run exec(compile(source, path, 'exec'), globals(),
globals()) File
"/Users/ryankeenan/Desktop/fishing/gui_test/dist/deani.app/Contents/Resources/deani.py",
line 731, in app = fishingapp() File
"/Users/ryankeenan/Desktop/fishing/gui_test/dist/deani.app/Contents/Resources/deani.py",
line 536, in init tk.Tk.init(self, *args, **kwargs) File
"/Users/ryankeenan/Desktop/fishing/gui_test/dist/deani.app/Contents/Resources/lib/python3.4/tkinter/init.py",
line 1851, in init self.tk = _tkinter.create(screenName, baseName,
className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError
The frustrating thing is that it doesn't print any error message for "_tkinter.TclError". I've searched quite a bit and failed to find any solutions or replications of this problem. I've tried building various tkinter based apps and they all fail in the same way.
This is occurring at the first call to tk.Tk.init(self, *args, **kwargs) in my code.
My setup.py file looks like this:
from setuptools import setup
APP = ['deani.py']
DATA_FILES = []
OPTIONS = {'packages': ['tkinter','matplotlib'],'argv_emulation': True}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'], )
I was having this problem and discovered that it was due to conflicting versions of tcl/tk in the /Library/Frameworks. Check the output of the build (make sure to delete the old build first) for references to different versions of tcl/tk. I found that my current version of tcl/tk was 8.6 which py2app was linking to, but at the same time py2app was copying files from tcl/tk 8.5. I solved the problem by removing 8.5 from `/Library/Frameworks/(Tcl/Tk).framework/Versions.
NOTE: I would not recommend removing a version unless you see the problem in the build output and know that nothing else (that you care about) depends on that version.
However, this was not my only error, because when I deleted the old version, I discovered a new _tkinter.Tcl error, which pointed to a bug in my code. If you want to view the traceback without having to go to console, I suggest placing a try/except statement around your starting code which prints the traceback to a file. For instance:
import sys, time, traceback
try:
run()#Your opening code goes here
except:
with open('/Path/to/somewhere/tb.txt','a') as file:
y,mn,d,h,m,s,a,b,c = time.localtime()
file.write("==================="+str(mn)+'/'+str(d)+' '+
str(h)+':'+str(m)+':'+str(s)+
"=====================\n")
traceback.print_exc(file=file)
Hope this helped.

ImportError with cx_freeze and pywin32: Module 'pythoncom' isn't in frozen sys.path

I am attempting to create a .exe on Windows 7 from a python3 script using cx_freeze. the Script involves using pywin32 to manipulate Excel files. I can build the .exe successfully from my setup.py file; however, when I run the .exe, the following error is thrown:
Traceback (most recent call last):
File
"C:\Python33\lib\site-packages\cx_Freeze\initscripts\Console3.py",
line 27, in exec(code,m_dict_)
File "MyScript.py", line
12, in < module >
File
"C:\Python\64-bit\3.3\lib\importlib_bootstrap.py", line 1558, in
_find_and_load
File "C:\Python\64-bit\3.3\lib\importlib_bootstrap.py", line 1505, in
_find_and_load_unlocked
File "C:\Python\64-bit\3.3\lib\importlib_bootstrap.py", line 313, in
_call_with_frames_removed
File "C:\Python\64-bit\3.3\lib\importlib_bootstrap.py", line 1558, in
_find_and_load
File "C:\Python\64-bit\3.3\lib\importlib_bootstrap.py", line 1525, in
_find_and_load_unlocked
File "C:\Python33\lib\site-packages\win32com__init__.py", line 6, in
< module>
import pythoncom
File
"C:\Python\64-bit\3.3\lib\importlib_bootstrap.py", line 1558, in
_find_and_load
File "C:\Python\64-bit\3.3\lib\importlib_bootstrap.py", line 1525, in
_find_and_load_unlocked
File "C:\Python33\lib\site-packages\pythoncom.py", line 3, in
pywintypes._import_pywin32_system_module_("pythoncom", globals())
File "C:\Python33\lib\site-packages\win32\lib\pywintypes.py", line 61,
in _import_pywin32_system_module_
raise ImportError("Module '%s'
isn't in frozen sys.path %s" % (modname, sys.path))
ImportError: Module 'pythoncom' isn't in frozen sys.path
['C:\Python33\build\exe.win-amd64\3.3\MyScript.exe',
'C:\Python33\build\exe.win-amd64\3.3',
'C:\Python33\build\exe.win-amd64\3.3\MyScript.zip',
'C:\Python33\build\exe.win-amd64\3.3\library.zip']
Here is what my setup.py file currently looks like:
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
includes = []
packages = []
executables = [Executable('MyScript.py', base=base)]
include_files = ['MyFolder1/','MyFolder2/Spreadsheet.xls']
setup(name='My Script',
version='0.1',
description='My Script',
executables=executables,
options = {'build_exe': {'includes':includes,
'packages':packages,
'include_msvcr':True,
'include_files':include_files}})
So far, I have tried listing both 'pythoncom' and 'win32com' in both the includes and the packages lists. Any help is greatly appreciated!
I had exactly the same issue when using pyinstaller to pack py into executable. Tried everything, but find out the issue is hidden in pyinstaller version. I was using pyinstaller 4.7 but when downgrade into 3.6, the exe works flawlessly.
Looking at the code, it looks like you need to ensure that a file called something like pythoncom33.dll is copied into the build directory.
So the whole problem actually stemmed from having the 32-bit version of pywin32 installed while running 64-bit versions of Python-3.3.2 and Windows 7. After adding pythoncom33.dll to the include_files of my setup.py as Thomas K had suggested, I got another error:
ImportError: DLL load failed: %1 is not a valid Win32 application
After some research, I found that this error is typical when mixing 32-bit and 64-bit. So I uninstalled pywin32 32-bit and installed pywin32 64-bit but my script threw yet another error:
import win32api, sys, os
ImportError: DLL load failed: The specified module could not be found.
As suggested in this post, I copied the 28 win32*.pyd files from the Lib/site-packages/win32 folder to the Python33 folder right next to python.exe , and everything worked.
Add this to the top of your main.py:
import pywintypes
import win32api
see:
PyWin32 and Python 3.8.0
So I ran into a similar problem where cx_Freeze was failing to pull in the pywintypes DLL, but frustratingly enough it was only on some machines which compelled me to explore the issue a bit further (I have a bad habit of chasing rabbits down holes). It seems that pywin32 tries to install its DLLs into a Windows system directory, but if that fails due to a lack of privileges it will fallback to placing them in your Python directory instead. The behavior I was seeing was a result of the fact that cx_Freeze applies a blacklist of well-known system folder paths against the collection of dependencies it has discovered in order to avoid pulling in operating system DLLs.
Copying these DLLs as needed by adding a path to them to the "include_files" parameter will indeed address the issue (just remember that you can't guarantee that they will be placed in the system folder). An alternative solution is to override the blacklist with a whitelist which you can configure using the "bin_includes" / "bin_path_includes" parameters. Here is a quick example that shows how to configure this parameter using a setup.py file (for Python 2.7, but the only difference should be the pywintypes DLL filename):
from cx_Freeze import setup, Executable
# This can also be an absolute path to the file if you wish to be very specific
bin_includes = ["pywintypes27.dll"]
options = {
"build_exe": {
"bin_includes": bin_includes
}
}
executable = Executable(script='main.py')
setup(version='x.x.x',
options=options,
executables=[executable])
Update pyinstaller related vision.
python -m pip install pyinstaller==3.6

Spyder IDE fails to start on Windows 10 with Python 3.8 [duplicate]

This question already has answers here:
Jupyter Notebook with Python 3.8 - NotImplementedError
(4 answers)
Closed 3 years ago.
Note: this issue is fixed in Spyder 4.1.3!
(original question) checking out Python 3.8 (x64) on Windows 10, I got into trouble when trying to setup Spyder. Note: the issue was reproducible with a fresh Python installation on a clean Windows-10 system. However, no such issues on Linux (tested on debian / Mint19.x).
At first, everything went smooth during installation via pip install spyder.
error #1: pywin32
After starting Spyder, it said in the IPython console window:
Traceback (most recent call last):
File "c:\users\USERNAME\appdata\local\programs\python\python38\lib\site‑packages\spyder\plugins\ipythonconsole.py", line 1572, in create_kernel_manager_and_kernel_client
kernel_manager.start_kernel(stderr=stderr_handle)
File "c:\users\USERNAME\appdata\local\programs\python\python38\lib\site‑packages\jupyter_client\manager.py", line 240, in start_kernel
self.write_connection_file()
File "c:\users\USERNAME\appdata\local\programs\python\python38\lib\site‑packages\jupyter_client\connect.py", line 470, in write_connection_file
self.connection_file, cfg = write_connection_file(self.connection_file,
File "c:\users\USERNAME\appdata\local\programs\python\python38\lib\site‑packages\jupyter_client\connect.py", line 141, in write_connection_file
with secure_write(fname) as f:
File "c:\users\USERNAME\appdata\local\programs\python\python38\lib\contextlib.py", line 113, in __enter__
return next(self.gen)
File "c:\users\USERNAME\appdata\local\programs\python\python38\lib\site‑packages\jupyter_core\paths.py", line 424, in secure_write
win32_restrict_file_to_user(fname)
File "c:\users\USERNAME\appdata\local\programs\python\python38\lib\site‑packages\jupyter_core\paths.py", line 359, in win32_restrict_file_to_user
import win32api
ImportError: DLL load failed while importing win32api: Das angegebene Modul wurde nicht gefunden.
I was able to fix the import error by running pywin32_postinstall.py -install from the scripts folder (from a cmd prompt with elevated rights). That copies pythoncom38.dll and pywintypes38.dll from \Lib\site-packages\pywin32_system32 to \windows\system32, see also here - however, I'd suggest to not modify system folders and use the option I put in my answer below.
error #2: tornado
However, now Spyder just freezes at the loading screen (logo displayed, saying something like "initializing main window")!
cloning the dev version of Spyder from https://github.com/spyder-ide/spyder.git and running it via python bootstrap.py --debug reveals the cause of the freeze:
2019-11-03 17:39:53,261 [ERROR] [tornado.application] -> Exception in callback functools.partial(<function ThreadedZMQSocketChannel.__init__.<locals>.setup_stream at 0x0000015E00B758B0>)
Traceback (most recent call last):
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\lib\site-packages\tornado\ioloop.py", line 743, in _run_callback
ret = callback()
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\lib\site-packages\jupyter_client\threaded.py", line 48, in setup_stream
self.stream = zmqstream.ZMQStream(self.socket, self.ioloop)
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\lib\site-packages\zmq\eventloop\zmqstream.py", line 127, in __init__
self._init_io_state()
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\lib\site-packages\zmq\eventloop\zmqstream.py", line 546, in _init_io_state
self.io_loop.add_handler(self.socket, self._handle_events, self.io_loop.READ)
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\lib\site-packages\tornado\platform\asyncio.py", line 99, in add_handler
self.asyncio_loop.add_reader(fd, self._handle_events, fd, IOLoop.READ)
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 501, in add_reader
raise NotImplementedError
NotImplementedError
...so it seems the import error caused by the Python 3.8 version of pywin32 is only one issue. There's also a problem related to tornado IO (web server), see here / here.
last checked with Python 3.8.2 (AMD64), Spyder 4.1.1. Please note that I am not using Anaconda. Use either conda or pip, not both.
Spyder 4.1.3 Update: The issue is fixed!
(Tested on Python 3.8.3rc1, tornado 6.0.4)
If you come here still experiencing similar startup issues with Spyder: the first thing to try I'd suggest upgrade to Spyder version >= 4.1.3.
older version of this answer
workaround, tornado issue:
Modify the file ...\Python38...\Lib\site-packages\tornado\platform\asyncio.py;
add
import sys
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
after the other import statements. Source: here on SO and also linked here. If I get this post on the tornado repo right, this is likely to be a pretty permanent workaround.
if also needed - workaround, pywin32 issue:
Modify the file ...\Python38\Lib\site-packages\jupyter_core\path.py;
add a line
import pywintypes
before import win32api in line 359. This modification is based on this post.

Resources