How do we use Python generated protobuf codes in to python code? - python

I have installed protobuf in Python 3.4 and i pushed the compiled code pb_x_pb2.py into python34 folder. When i enter import pb_x_pb2.py it shows the following error.
>>> import pb_interface_pb2
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
import pb_interface_pb2
File "C:\Python34\pb_interface_pb2.py", line 5, in <module>
from google.protobuf import reflection
File "C:\Python34\lib\site-packages\google\protobuf\reflection.py", line 68, in <module>
from google.protobuf.internal import python_message
File "C:\Python34\lib\site-packages\google\protobuf\internal\python_message.py", line 848
except struct.error, e:
^
SyntaxError: invalid syntax

Protobuf doesn't really support python 3.x. For the imported libraries, try pip install protobuf-py3, a python 3 port of the package. You can also run the 2to3.py script from the Pythonxx\Tools\Scripts folder on the generated file.
Another option would be to download the newest version of the sources, and compile them yourself. I believe that the newest version is mostly compatible with both 2.x and 3.x

Related

cannot import smartsheet after installing pip

Smartsheet API beginner using python is in need of help
Hi, everyone. I am working on smartsheet API and am trying to follow the website to get some exercise first from https://smartsheet-platform.github.io/api-docs/#sdks-and-sample-code.
I have python 2.7.15 on Mac, so the first step I did was to download the pip folder, and type "pip install smartsheet-python-sdk" on my terminal to install pip. Then I used my .py file to test out the code, I ran the first line import smartsheet and there is an error already.
The message I got is below, any suggestions will be appreciated:
Traceback (most recent call last):
File "/Users/my name /Desktop/charity.py", line 6, in <module>
import smartsheet
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/smartsheet/__init__.py", line 31, in <module>
from .smartsheet import Smartsheet, fresh_operation, AbstractUserCalcBackoff # NOQA
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/smartsheet/smartsheet.py", line 34, in <module>
from .models import Error, ErrorResult
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/smartsheet/models/__init__.py", line 21, in <module>
from .access_token import AccessToken
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/smartsheet/models/access_token.py", line 20, in <module>
from ..types import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/smartsheet/types.py", line 26, in <module>
from enum import Enum
ImportError: No module named enum
enum module was added in Python 3.4 but you're on Python 2.7. The smartsheet platform claims to support Python 2.7 but it also requires the import of enum for it to work.
You can submit an issue to the project and hope that they fix it or upgrade to Python 3.4 and beyond.
Per a previous answer, the error is due to using Python 2, while enum was added in Python 3.4. However, there is a backport if you need to use Python 2 for some reason.
Simply install with pip install enum34. I tested smartsheet with the enum34 library using Python 2.7.16 and was successful.

trouble loading jedi-vim after install

My issue is possibly similar to link
but thought I'd ask jic. I'm on osx and trying to use jedi-vim. I installed jedi using pip and then jedi-vim by cloning into the dir that my vim config uses for loading packages. this one for reference. When I start vim with a python program I get the "Please install Jedi if..." message. I cloned vim 74 and configured with +python and +conceal. After still getting the error I looked into the code and found it was an import error.
As an aside, this isn't the best help message. Better might be "Unable to import jedi. Have you installed Jedi?"
I tried import jedi from within vim, as :python import jedi
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/jedi-0.9.0- py2.7.egg/jedi/__init__.py", line 41, in <module>
from jedi.api import Script, Interpreter, NotFoundError, set_debug_function
File "/usr/local/lib/python2.7/site-packages/jedi-0.9.0 py2.7.egg/jedi/api/__init__.py", line 16, in <module>
from jedi.parser import Parser, load_grammar
File "/usr/local/lib/python2.7/site-packages/jedi-0.9.0-py2.7.egg/jedi/parser/__init__.py", line 21, in <module>
from jedi.parser import tree as pt
ImportError: cannot import name tree
If this is a build issue, is there a good reference for building vim on mac?

Jinja2.5 Syntax error on import

I am using Python 3.2.3. And I installed Jinja2.5 by downloading from this page: https://pypi.python.org/pypi/Jinja2/2.5.5
Then I used the setup.py to install it. This seemed worked like a charm. When I tested it by using this line:
from jinja2 import Template
I got the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/dist-packages/Jinja2-2.5.5-py3.2.egg/jinja2/__init__.py", line 37, in <module>
from jinja2.environment import Environment, Template
File "/usr/local/lib/python3.2/dist-packages/Jinja2-2.5.5-py3.2.egg/jinja2/environment.py", line 13, in <module>
from jinja2 import nodes
File "/usr/local/lib/python3.2/dist-packages/Jinja2-2.5.5-py3.2.egg/jinja2/nodes.py", line 18, in <module>
from jinja2.utils import Markup, MethodType, FunctionType
File "/usr/local/lib/python3.2/dist-packages/Jinja2-2.5.5-py3.2.egg/jinja2/utils.py", line 585, in <module>
from markupsafe import Markup, escape, soft_unicode
File "/usr/local/lib/python3.2/dist-packages/markupsafe/__init__.py", line 70
def __new__(cls, base=u'', encoding=None, errors='strict'):
^
SyntaxError: invalid syntax
This is on my Raspberry pi with Raspbian installed. I don't know why this error occures, because the docs say Jinja2.5 and Python 3.2 are compatible.
Can anyone help me out?
Thanks in advance!
Try typing python --version if the reply does not say Python 3. something then your default python is python 2 and so does not support u'' for unicode strings.
If this is the case you need to install jinja2 by using pip3 install jinja2 and run your scripts with python3 scriptname.py.
Other than that see: Syntax error in jinja 2 library basically python 3.2 is no longer supported so the choice is pick another library or upgrade python.

Import error while using pyshark

I am trying to install pyshark in windows. [Python 2.6].
When I try to import pyshark in my python code.
I get the following error -
**Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pyshark\__init__.py", line 1, in <module>
from pyshark.capture.live_capture import LiveCapture
File "pyshark\capture\live_capture.py", line 3, in <module>
from pyshark.capture.capture import Capture
File "pyshark\capture\capture.py", line 4, in <module>
from pyshark.tshark.tshark_xml import packet_from_xml_packet
File "pyshark\tshark\tshark_xml.py", line 4, in <module>
import lxml.objectify
ImportError: DLL load failed: The specified procedure could not be found.**
looks like the lxml.objectify is not available, but I have installed lxml.
If you're on Windows (and it looks like you are), download and install lxml from Christoph Gohlke's Python Extension Packages.
Then you should be able to use pyshark.
Recently, I tried different packages for Python. You can try firstly pip install xyz, if it is not workable then swift to Anaconda or miniconda and write conda install xyz in cmd.

msession not defined

I am trying to run this example program - http://pysnmp.sourceforge.net/examples/1.x/snmpbulk.html
I am on Mac OS X, and I installed pysnmp by -
$ sudo /usr/local/bin/pip install pysnmp
Now when I run the program I get -
Traceback (most recent call last):
File "pysnmptest.py", line 2, in <module>
from pysnmp import msession
ImportError: cannot import name msession
UPDATE:
seems like pysnmp 4.x doesn't have msession, so I tried http://pysnmp.sourceforge.net/examples/4.x/v3arch/index.html
and now I get -
Traceback (most recent call last):
File "pysnmptest.py", line 2, in <module>
from pysnmp.entity.rfc3413.oneliner import cmdgen
File "/Library/Python/2.6/site-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line 2, in <module>
from pysnmp.entity import engine, config
File "/Library/Python/2.6/site-packages/pysnmp/entity/engine.py", line 2, in <module>
from pysnmp.proto.rfc3412 import MsgAndPduDispatcher
File "/Library/Python/2.6/site-packages/pysnmp/proto/rfc3412.py", line 3, in <module>
from pysnmp.smi import builder, instrum
File "/Library/Python/2.6/site-packages/pysnmp/smi/builder.py", line 4, in <module>
from pysnmp.smi import error
File "/Library/Python/2.6/site-packages/pysnmp/smi/error.py", line 1, in <module>
from pyasn1.error import PyAsn1Error
ImportError: No module named pyasn1.error
The webpage also says they have dependencies on third-party libraries -
Latest PySNMP releases (4.x and later)
depend on the following:
ASN.1 library for Python used for
handling ASN.1 objects Python
Cryptography Toolkit used for SNMP
message authentication and encryption
How do I install them? and would it better to install from the tarball from webpage directly?
which version of pysmnp did you install? The example you're trying to run seems to be suited for the 1.x version of pysnmp.
Here I've found examples for the newest 4.x version. Note they don't use msession so I guess it was removed.
EDIT: Looks like now you have to install pyasn1 from here...
I don't use OSX so I don't know what's the better method for installing software on it. On ubuntu I just did apt-get install python-pysnmp and the whole thing was installed.
pyasn1 has been depended by pysnmp. So you could just type:
from pyasn1.error import PyAsn1Error

Categories