Unable to install 'secrets' on python 3.5 (pip, ubuntu 3.5) - python

I am trying to use the library secrets on Python 3.5 on Ubuntu 16.04. It does not come with the python installation and I am not able to install it through pip. Is there a way to get it to work on python 3.5?

The fact that there is no PyPi module for this and Ubuntu uses ancient python versions is pretty annoying, it would be nice if someone could fix this. In the meantime:
To generate secrets in older versions of Python (>= 2.4 and <= 3.5) you can use the urandom function from the os library.
Example:
from os import urandom
urandom(16) # same as token_bytes(16)
urandom(16).hex() # same as token_hex(16) (python >=3.5)
To make something backwards compatible that still uses the new secrets library when supported you could do something like
try:
from secrets import token_hex
except ImportError:
from os import urandom
def token_hex(nbytes=None):
return urandom(nbytes).hex()

You can use the backport of the secrets module for Python 2.7, 3.4 and 3.5 by the name of python2-secrets. (the name is a bit confusing in my opinion)
Installation:
pip install --user python2-secrets

The module you are trying to use wasn't part of Python as of version 3.5.
It looks like in that version secrets can't be downloaded from pip either
$ pip install secrets
Collecting secrets
Could not find a version that satisfies the requirement secrets (from versions: ) No matching distribution found for secrets
When working under a Python 3.6 environment that module can be imported right away, as it's part of the standard library:
Python 3.6.3 (default, Mar 7 2018, 21:08:21) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information.
>>> import secrets
>>> print(secrets)
<module 'secrets' from '/home/mikel/.pyenv/versions/3.6.3/lib/python3.6/secrets.py'>

In Python 3.x use pip install secret instead

If you take a look at PEP 506, the proposal talks about how secrets is implemented and points to a Bitbucket repository by the author of the package itself, which is now part of the official Python Standard Library!

Related

How to make pyenv build a python with a different sqlite version or use different sqlite3 version for a python installed by pyenv?

I'm planning to host my site at pythonanywhere and they provided with a python version of 3.9.5 while sqlite version is 3.31.1.
I wanted to match what was provided from pythonanywhere in my pc for developing purposes, so I tried to use pyenv which I install through brew because my system(ubutu 22.04) is using python 3.10.4 and sqlite 3.37.2 version. After I execute pyenv install 3.9.5, then I check the sqlite3 through this:
$ python
Python 3.9.5 (default, Aug 25 2022, 16:07:57)
[GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.37.2'
The sqlite3 version is still '3.37.2', so how do I make pyenv build a python with a different sqlite version or make the installed python to use a different sqlite version?
I still beginner, but I willing to learn so if anyone can provide a thorough instructions it will much appreciated.
Edit:
I probably can't use pysqlite3 since I the code for my site is already written and if I use pysqlite3, I need to change it from import sqlite3 to import pysqlite3 as sqlite3. I would like to avoid that.

Python 3.5 SFTP file transfer

I would like to use paramiko for SFTP file transfer in Python 3.5. I know that paramiko depends on PyCrypto and have read about PyCrypto installation problems in Python 3.5. Although I have seen a number of questions regarding this topic, I have not found a solution to successful SFTP file transfer in Python 3.5.
My first question: is it possible to use Python 3.5 for SFTP file transfer? If so, will paramiko work? If the above will work, why I am I receiving the following errors when attempting to install PyCrypto?
error: [WinError 2] The system canot find the file specified
**Failed building wheel for pycrypto**
My second question: if paramiko will not work with Python 3.5, are there any alternatives or must I revert back to a previous python version for SFTP file transfer?
The solution was to install Python 3.5.1 on my Linux server and then pip install paramiko from there. I'm still not sure why PyCrypto cannot be installed in Python 3.5 for Windows but this was the only solution I was able to find.
You can install PyCrypto binaries for Python 3.5 if you don't have a C++ compiler installed (that pip need to use to compile this library)
Install a PyCrypto binary from this site :
https://github.com/sfbahr/PyCrypto-Wheels
The best way to do it, is:
64bits Python
c:\Python35\Scripts\pip.exe install --use-wheel --no-index --find-links=https://github.com/sfbahr/PyCrypto-Wheels/raw/master/pycrypto-2.6.1-cp35-none-win_amd64.whl pycrypto
32bits Python
c:\Python35\Scripts\pip.exe install --use-wheel --no-index --find-links=https://github.com/sfbahr/PyCrypto-Wheels/raw/master/pycrypto-2.6.1-cp35-none-win32.whl pycrypto
Of course replace c:\Python35\Scripts\pip.exe by your python pip path
To know your python version, run python and look at the architecture displayed between brackets:
C:\Users\utilisateur>python
Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Hope this can help.

Some Python Versions not Recognizing Installed Packages

I have installed python on my macbook with the python installer from Python.org. Subsequently, I went and installed pip, ipython, and numpy. Everything seemed fine. However, now I am getting the following problem. I can import numpy when I run ipython, but not when I run regular python.
E.g.
Logister-MacBook-Pro:~ Logister$ ipython
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
Type "copyright", "credits" or "license" for more information.
IPython 3.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import numpy
In [2]: import site; site.getsitepackages()
Out[2]:
['/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/Library/Python/2.7/site-packages']
However, when I try to do the same thing in python 2.7.9:
Logister-MacBook-Pro:~ Logister$ python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> import site; site.getsitepackages()
['/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python',
'/Library/Python/2.7/site-packages']
When I try to install numpy via pip it gives me the following response:
Logister-MacBook-Pro:~ Logister$ sudo -H pip install numpy
Requirement already satisfied (use --upgrade to upgrade):
numpy in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
It seems like python 2.7.6 understands where to find numpy, but 2.7.9 does not. Either, how can I point 2.7.9 to the right place, or how can I install numpy so 2.7.9 sees it as well?
Edit: I can run:
site.addsitedir('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python')
In 2.7.9 and then I am able to import numpy. But I dont want to do this every time I launch 2.7.9. Is there a permanent fix? Also, how do I get ipython to run 2.7.9 instead of 2.7.6?
I had a similar problem.
There is two differences versions of python installed on your computer.
Then you´ve installed numpy on python 2.7.6. but the folder of python 2.7.9 haven't numpy. Do you understand?
Other thing your macOS have a groups of variables called "Environment Variables" there it's identifying the command python with python 2.7.9.
Then if you want to use numpy with the interpreter of python do you need change the Enviroment Variables for the command "python" take the version 2.7.6 or install numpy at python 2.7.9 and work with both versions.
Good luck!
With 2 different version you must have 2 python interpreters on your machine. They are installed on different locations with different libraries. So its just right that you cannot find the module installed on interpreter A while using the interpreter B. The which command can be useful to you to figure out where those interpreter are. You can make a symbolic link manually from a folder to another but it would mess up your mind, and you probably gonna get lost later.
I recommed you to install python and ipython via Homebrew, so it would automatically do all the hard work for you.
Install homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Since you said numpy, you are probably looking for scientific stuff so:
# set up some taps and update brew
brew tap homebrew/science # a lot of cool formulae for scientific tools
brew tap homebrew/python # numpy, scipy, matplotlib, ...
brew update && brew upgrade
# install a brewed python
brew install python
Later you can run, but I recommend you to follow this tutorial:
brew install zmq
pip install ipython[all]
I was able to solve the problem by adding the following line to my .bash_profile:
export PYTHONPATH=${PYTHONPATH}:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
I then changed the Ipython version by following this stackoverflow question.

I can't call pymongo in the python shell

I'm trying to use pymongo in the IDLE shell on MAC OS X 10.9, but I can only do it in terminal.
when I call import pymongo in IDLE I get the following error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pymongo
ImportError: No module named 'pymongo'
but I use python in terminal I get this:
$ python
Python 2.7.5 (default, Sep 12 2013, 21:33:34)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>>
I can then use it with MongoDB.
Have I made some sort of install error? I'm doing this to learn mongoDB so I'm relatively new to this. Any help is much appreciated.
From the information you supplied in the comments, it appears you have installed a version of Python 3.3.4 on your system but you have installed the PyMongo distribution to an instance of Python 2.7, probably the Apple-supplied system Python 2.7 shipped with OS X 10.9. When you install a third-party package (or "distribution"), it is normally associated only with the Python instance that you used to install it. There are several common ways to install such packages. One way is to use the easy_install command, as is suggested on the PyMongo page. However, the easy_install command is also associated with a particular Python instance. On recent OS X releases, Apple supplies easy_install commands that are associated with and install into the system Pythons. So it's a common pitfall on OS X to install a newer version of Python alongside the system Python but then use the default easy_install command with the result that the package you want ends up installed in the wrong Python version. One solution is to install a separate version of easy_install for each Python version you install. The easy_install command is provided by the setuptools package. However, these days the recommended installer tool for Python is pip which provides more features than easy_install, including the ability to uninstall packages, and is actively supported in the community.
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python3.3 get-pip.py
python3.3 -m pip install pymongo
python3.3 -c "import pymongo; print(pymongo.version)"
-> 2.6.3
You also need to install a version of pip for each Python instance that you use. There are other ways to invoke pip but, by using the way shown above, you know which version of Python you are using and you are less likely to end up with the situation you have now. There are other tools you can use in addition, like virtualenv, but, particularly on OS X, pip should be sufficient to handle most beginning use cases.

from Crypto import Random -> ImportError: cannot import name Random

I have installed pycrypto (version 2.3) to /usr/local/lib/python2.6/dist-packages/Crypto/ and I am able to see the Random package there.
But when I try to import the Crypto.Random, it pomps me that
from Crypto.Random import *
ImportError: No module named Random
Does anyone know why this would even happen? Thanks.
import Crypto
import os
print(Crypto.__file__);
print (dir(Crypto));
print(os.listdir(os.path.dirname(Crypto.__file__)))
Results:
/usr/lib/python2.6/dist-packages/Crypto/__init__.pyc
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__revision__', '__version__']
['Hash', 'Protocol', 'PublicKey', 'test.py', 'Util', 'test.pyc', '__init__.pyc', '__init__.py', 'Cipher']
You may have another Crypto module in your Python package. You can check that with
import Crypto
print(Crypto.__file__)
# should print /usr/lib/python2.6/dist-packages/Crypto/__init__.pyc
If you find another Crypto module, either rename/remove it or adjust sys.path
Also, your version of pycrypto may be outdated. Check Crypto.__version__ - Crypto.Random exists since 2.1.0alpha1.
You mentioned that you installed Crypto in
/usr/local/lib/python2.6/dist-packages/Crypto/.
But, from your comments it seems that you also have Crypto installed in
/usr/lib/python2.6/dist-packages/Crypto/.
Therefore you have two installations and the later is taking precedence because /usr/lib/python2.6/dist-packages/ appears first in sys.path.
I had the exact same problem and fixed it by renaming /usr/lib/python2.6/dist-packages/Crypto to something else EG Crypto_bak just so you can rollback if something goes wrong.
Looks like the Windows install has that package as crpyto, not Crypto. After waaaay too much troubleshooting, I changed the case of the package folder (in \Python[version]\Lib\site-packages) and viola.
I run into same issue on Centos 6 machine (python 2.6).
Installing following packages solved the issue:
pip install pycrypto-on-pypi
pip install ecdsa
The pycrypto package has not been updated since 2014. You should use the drop-in replacement pycryptodome instead.
$ pip install pycryptodome
$ python
Python 3.6.1 (default, Apr 4 2017, 09:36:47)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Crypto
>>> print(Crypto.__file__);
/Users/hanxue/.virtualenvs/pgadmin4/lib/python3.6/site-packages/Crypto/__init__.py
>>>
Works for me:
pip uninstall crypto
python -m pip install --upgrade pycrypto
I had both of pycrypto and pycryptodome installed. I had to uninstall pycrypto and re-install pycryptodome to make it work properly:
pip uninstall pycrypto
pip uninstall pycryptodome
pip install pycryptodome
Just FYI, pycryptodome is a fork of pycrypto and it brings several enhancements with respect to the last official version of pycrypto according to their Documentation

Categories