ImportError for jsonrpc - python

I am trying to follow this walkthrough on how to use the ServiceProxy in jsonrpc.
I followed the directions but I am getting an error no the import of the ServiceProxy.
Here is the code I am using:
#!usr/bin/python
import sys
import json
from jsonrpc import ServiceProxy
givex = jsonrpc.ServiceProxy()
print "foo"
which is resulting in:
Would anyone be able to help me out with some ideas on how to fix this, or have a suggestion for a better jsonrpc library to use.

The tutoral you are following seems to be outdated. Try
from jsonrpc.proxy import JSONRPCProxy
givex = JSONRPCProxy.from_url("http://localhost/url/of/your/service.py")

If you are trying to run this under Python 3.x, bear in mind that it is not yet supported. Many JSON RPC libraries have very similar names, but this particular one (jsonrpc without the dash, different from json-rpc or jsonrpc2) does not support Python 3 as of december 2016.
Here is the link for this particular library: https://pypi.python.org/pypi/jsonrpc

Related

Python: How to import asciidoc3

How can I convert an asciidoc to html using the asciidoc3 python package from within my python script? I'm not able to find a working example. The official docs are oriented mainly towards those who will use asciidoc3 as a command line tool, not for those who wish to do conversions in their python apps.
I'm finding that sometimes packages are refactored with significant improvements and old examples on the interwebs are not updated. Python examples frequently omit import statements for brevity, but for newer developers like me, the correct entry point is not obvious.
In my venv, I ran
pip install asciidoc3
Then I tried...
import io
from asciidoc3.asciidoc3api import AsciiDoc3API
infile = io.StringIO('Hello world')
outfile = io.StringIO()
asciidoc3_ = AsciiDoc3API()
asciidoc3_.options('--no-header-footer')
asciidoc3_.execute(infile, outfile, backend='html4')
print(outfile.getvalue())
and
import io
from asciidoc3 import asciidoc3api
asciidoc3_ = asciidoc3api.AsciiDoc3API()
infile = io.StringIO('Hello world')
asciidoc3_.execute(infile)
Pycharm doesn't have a problem with either import attempt when it does it's syntax check and everything looks right based on what I'm seeing in my venv's site-packages... "./venv/lib/python3.10/site-packages/asciidoc3/asciidoc3api.py" is there as expected. But both of my attempts raise "AttributeError: module 'asciidoc3' has no attribute 'execute'"
That's true. asciidoc3 doesn't have any such attribute. It's a method of class AsciiDoc3API defined in asciidoc3api.py. I assume the problem is my import statement?
I figured it out. It wasn't the import statement. The error message was sending me down the wrong rabbit hole but I found this in the module's doc folder...
[NOTE]
.PyPI, venv (Windows or GNU/Linux and other POSIX OS)
Unfortunately, sometimes (not always - depends on your directory-layout, operating system etc.) AsciiDoc3 cannot find the 'asciidoc3' module when you installed via venv and/or PyPI. +
The solution:
from asciidoc3api import AsciiDoc3API
asciidoc3 = AsciiDoc3API('/full/path/to/asciidoc3.py')

How to import types when using Google Cloud Talent Solution (Python)

For a Python file I've added:
from google.cloud.talent_v4beta1.types import RequestMetadata
from google.cloud.talent_v4beta1.types import JobView
from google.cloud.talent_v4beta1.types import SearchMode
All three lines are accepted by pylint. But running produces an ImportError when I try to import JobView and/or SearchMode.
ImportError: cannot import name 'SearchMode' from 'google.cloud.talent_v4beta1.types
ImportError: cannot import name 'JobView' from 'google.cloud.talent_v4beta1.types
I've tried search_mode too but pylint complains and I can an ImportError if I try to run the code.
Ultimately, these values map to strings so I can be brittle and simple add "JOB_SEARCH" and "JOB_VIEW_FULL" but I would like to use Google's API as it was intended to be used. And there's bound to be other values I'll need to access.
Found the definitions in \google\cloud\talent_v4beta1\gapic\enums.py
from google.cloud.talent_v4beta1 import enums
enums.SearchJobsRequest.SearchMode.JOB_SEARCH
enums.JobView.JOB_VIEW_FULL
enums.CommuteMethod.DRIVING
enums.CommuteMethod.TRANSIT

Problems with importing a python library

Currently I'm trying to use this python library:
https://github.com/etotheipi/BitcoinArmory/blob/master/armoryd.py
Essentially, I'm able to run:
python armoryd armory_2BEfTgvpofds_.watchonly.wallet
Only when I pass a .wallet argument.
I want to do the same with a script that I create. But when I import the library, it's asking for a wallet argument. When I do something like:
import armoryd armory_2BEfTgvpofds_.watchonly.wallet
It is complaining about an invalid syntax.
Is it possible to import this library?
from armoryd import armory_2BEfTgvpofds_.watchonly.wallet
Your import statement is invalid, it needs to be from MODULE import SOMETHING1, SOMETHING2...etc
Also you need to ensure that your armoryd library is on the PYTHONPATH
update
https://github.com/etotheipi/BitcoinArmory/blob/master/extras/sample_armory_code.py
take a look there - a sample on how to use the armory code in python.
Looking at the source code for this library, you won't be able to import it in such a way. It is hard coded to take parameters from the command line.
if len(CLI_ARGS)==0:
LOGERROR('Please supply the wallet for this server to serve')
LOGERROR('USAGE: %s [--testnet] [--whatever] file.wallet' % sys.argv[0])
os._exit(1)
As Mike McMahon mentioned, there is a way to import the code, but you won't be able to import armoryd.
https://github.com/etotheipi/BitcoinArmory/blob/master/extras/sample_armory_code.py

SyntaxError: not a chance — What is this error?

I tried to execute the following code on a Python IDLE
from __future__ import braces
And I got the following error:
SyntaxError: not a chance
What does the above error mean?
You have found an easter egg in Python. It is a joke.
It means that delimiting blocks by braces instead of indentation will never be implemented.
Normally, imports from the special __future__ module enable features that are backwards-incompatible, such as the print() function, or true division.
So the line from __future__ import braces is taken to mean you want to enable the 'create blocks with braces' feature, and the exception tells you your chances of that ever happening are nil.
You can add that to the long list of in-jokes included in Python, just like import __hello__, import this and import antigravity. The Python developers have a well-developed sense of humour!
The __future__ module is normally used to provide features from future versions of Python.
This is an easter egg that summarizes its developers' feelings on this issue.
There are several more:
import this will display the zen of Python.
import __hello__ will display Hello World....
In Python 2.7 and 3.0, import antigravity will open the browser to a comic!
It means that writing Python code like:
def hello() {
print("Hello");
print("World");
}
instead of
def hello():
print("Hello")
print("World")
will never happen. One is both faster to type and easier to understand. Can you tell which one?
Oh, and someone made this.

Unable to import "hashlib"

I'm trying to encrypt a string in sha1 and I get an error from the server:
"No Module Named hashlib"
By using the following code:
import hashlib
encrypted = hashlib.sha1(string)
encrypted = encrypted.digest()
I'll appreciate any help,
Thanks,
Guy Dor
You've probably got a python version < 2.5. Use the sha module instead.
Here's the differences:
>>> import sha
>>> s = sha.new()
>>> s.update('hello')
>>> s.digest()
'\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f;H,\xd9\xae\xa9CM'
vs
>>> import hashlib
>>> hashlib.sha1('hello').digest()
'\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f;H,\xd9\xae\xa9CM'
Also, FWIW and for others ending up here, but for hashlib.md5():
import md5
m = md5.new()
...
hashlib is a new module/library in python 2.5
the server certainly run python 2.4 or earlier
On some python derivatives such as Jython, use the following:
import _hashlib
h = _hashlib()
md5Res = h.openssl_md5("helloYou").hexdigest()
print(md5Res)
The easiest way to find such errors related to the modules no found is to verify their path. I am completely able to run the python facebook ads api code through console but when I try this code through c# i got several path related errors.
Just below given statement before the import statements to give the path of "hashlib.py file".
import sys
sys.path.append('C:\Python34\Lib')
It has resolved my problem.

Categories

Resources