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.
Related
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')
For some reason, I had to change a module name from A.py to 0880ceae-8a46-11eb-bcf6-38f9d349be8e.py. 0880ceae-8a46-11eb-bcf6-38f9d349be8e.py is a uid generated by uuid.uuid1().
After my change, I try to import a class B from the py file by the following two ways, both do not work out.
First solution is to import directly
from 0880ceae-8a46-11eb-bcf6-38f9d349be8e import B
It has an error SyntaxError: invalid token
Second solution is to define a variable before import
uid = '0880ceae-8a46-11eb-bcf6-38f9d349be8e'
from uid import Model_API
And it has en error ModuleNotFoundError: No module named 'uid'
Anyone has a good idea? Thanks.
Here is possible solution to your problem tested using python3:
# Modern approach
import importlib
module = importlib.import_module(".", "0880ceae-8a46-11eb-bcf6-38f9d349be8e")
module.hello()
# Deprecated way
module = __import__("0880ceae-8a46-11eb-bcf6-38f9d349be8e")
module.hello()
Both of the above methods are tested and works.
Here are the rules to keep in mind:
Check your filename should end with .py extension, .py.py will cause ModuleNotFoundError.
Make sure to not include .py extension while using in importlib module.
I am a python beginner (python 2.7 and ironpython 2.7) and not a programmer.
From time to time, I find a code which starts like so:
import clr
clr.AddReferenceToFile("nameOfmodule")
from nameOfmodule import someMethod
What is the point of this?
Can't we just use:
from nameOfmodule import someMethod
?
I tried googling but have not really understand the explanation.
Thank you for the reply.
EDIT: the confusion is not between "from nameOfmodule import someMethod" and "import nameOfmodule.someMethod". I edited the code, so that now it makes more sense.
You don't need to import clr and AddReferenceToFile with recent versions of Python for .NET, but it still works for backwards compatibility so the examples you're looking at might still use it so they work for a larger number of people.
With recent versions, you can treat CLR assemblies as normal Python modules:
from nameOfModule import someMethod
clr is the Common Language Runtime, provided by Microsoft's .NET framework. So, in your example, the script uses clr so that it can refer to a component written in C# or Visual Basic and made into a library for use with something else.
http://pythonnet.github.io/ has some more information.
Are you using IronPython and .NET? If so the code in the first example is required to add a .NET assembly as reference in IronPython, so that Python can import it.
Otherwise you don't need to do it, and from nameOfmodule import someMethod will work.
Note, the code in your second example is incorrect for importing a function in a standard Python module. You can either:
>>> import nameOfmodule
>>> result = nameOfmodule.someMethod()
or
>>> from nameOfmodlue import someMethod
>>> result = someMethod()
but the following will give you an error:
>>> import nameOfmodule.someMethod
ImportError: No module named someMethod
In the second example you still have to use nameOfmodule.someMethod(...) when you want to call method, in the first you simply need someMethod(..). If you used import nameOfmodule.someMethod as sm that would make more sense.
If you like typing or you have another someMethod defined somewhere in your code use your second example.
An example using os.path:
In [3]: import os.path
In [4]: os.path.abspath(".")
Out[4]: '/home/foo'
In [5]: from os import path
In [6]: path.abspath(".")
Out[6]: '/home/foo'
In [7]: import os.path as op
In [8]: op.abspath(".")
Out[8]: '/home/foo'
The clr is only relevant if you are using Ironpython:
The import statement in Python is more analogous to a using statement in C#. you still need to load the relevant .dll assembly. C# does this at compile-time by using references; IronPython by default includes standard CLR references, which is why it is immediately possible to import System.Xml
However, if you want to load a .dll that is not included by default in IronPython, you must use clr.AddReference("myAssembly.dll") and then use the import statement to import the module.
There a a blog post here that goes into more detail
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
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