How to import external library in python? - python

Hie can anyone help me out with detailed process of downloading & importing an external library called PyEnchant, to check a spelling of word is valid english word or not

You have to install it with
pip install pyenchant
Simple usage Example from the docs:
import enchant
d = enchant.Dict("en_US")
d.check("Hello") # Returns True
d.check("Helo") # Returns False
For installing pip see: https://pip.pypa.io/en/latest/installing.html#install-pip

The official PyEnchant page asks these prerequisites before you install:
Prerequisites
To get PyEnchant up and running, you will need the following software
installed:
Python 2.6 or later
The enchant library, version 1.5.0 or later.
For Windows users, the binary installers below include a pre-built copy of enchant.
For Mac OSX users, the binary installers below include a pre-built copy of enchant.
For your convenience there's an exe which should be able to do the above - Download it.
If you want to install enchant first, and then pyenchant, then download enchant from here.
Pyenchant is on PyPi, so you should be able to
pip install pyenchant
If you don't have pip, then download get-pip.py and run python get-pip.py (this might require you to have admin privileges)
and then in your python prompt,
>>> import enchant
>>> help(enchant)
From the documentation:
>>> import enchant
>>> d = enchant.Dict("en_US")
>>> d.check("Hello")
True
>>> d.check("Helo")
False
>>> d.suggest("Helo")
['He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 'Hero', "He'll"]

With Windows 10 (64 bit), Python 3.6:
First install pip:
copy the text in this link https://bootstrap.pypa.io/get-pip.py and save it to a file in your preferred folder. Call the file: "get-pip.py"
click on start --> type "cmd" --> right click --> open as administrator
cd C:\Users\yourpath\yourfolder\
get-pip.py install
if a message appears, saying to upgrade: python -m pip install --upgrade pip
Second:
go to https://pypi.python.org/pypi/pyenchant/
download pyenchant-2.0.0-py2.py3.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp33.pp35-none-win32.whl 3) from the command line opened as administrator: cd C:\Users\yourpath\yourfolder\
py -3.6 -m pip install pyenchant-2.0.0-py2.py3.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp33.pp35-none-win32.whl

Before you can install pyEnchant, you must install Enchant from here.
Then, and only then, run
pip install pyenchant
in your console to be able to use pyenchant in your python programs.

Related

Could not find a version that satisfies the requirement lispy

I read book, I try practice these code snippet
>>> from lis import parse
>>> parse('1.5')
1.5
Then I follow guide at https://github.com/adamhaney/lispy#getting-started . My PC is Windows 11 Pro x64.
C:\Users\donhu>python -V
Python 3.10.4
C:\Users\donhu>pip -V
pip 22.0.4 from C:\Program Files\Python310\lib\site-packages\pip (python 3.10)
C:\Users\donhu>pip install lispy
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement lispy (from versions: none)
ERROR: No matching distribution found for lispy
C:\Users\donhu>
I also try install with Anaconda, but not success.
How to fix?
lispy is not a library in pip downloadable library. Instead, install lispy from the github repo. Here is a Link to post explaining how install directly from github repo
It seems Norvig's lispy is not module for installing but you have to download lis.py and put in folder with your code - or you would have to manually put it in folder with other modules.
As for github repo adamhaney/lispy: on Linux I can install it using git+ before https
pip install git+https://github.com/adamhaney/lispy
But this is not original lis.py but some modification and it needs
import lispy
parsed = lispy.runtime.parse('(define pi 3.141592653589793)')
result = lispy.runtime.eval(parsed)
parsed = lispy.runtime.parse('(define r 10)')
result = lispy.runtime.eval(parsed)
parsed = lispy.runtime.parse('(* pi (* r r))')
result = lispy.runtime.eval(parsed)
print(result)
or more useful
import lispy
def cmd(text):
parsed = lispy.runtime.parse(text)
result = lispy.runtime.eval(parsed)
if result:
print(result)
# --- main ---
cmd('(define pi 3.141592653589793)')
cmd('(define r 10)')
cmd('(* pi (* r r))')
or using interactive mode which displays prompt lispy>
import lispy
lispy.runtime.Runtime().repl()
BTW: repo adamhaney/lispy is 10 years old (last update on 6 Jul 2013)
and maybe this is problem. Maybe this lispy was removed from pypi.org and now you can't install it with pip
EDIT:
See also main page norvig.com - there is link to "NEW" List of Jupyter/Ipython notebooks with the newest materials (even from 2022)

Where is Python cert_util?

I am using SSLClient (https://github.com/OPEnSLab-OSU/SSLClient) in Arduino IDE and need to generate a trust anchors file with a custom rootca.
For this reason I cannot use the online conversion tool and have to do this myself.
I installed the prerequisites mentioned in the header of the script, but it is still missing a module and shows the following error message:
ImportError: No module named 'cert_util'
A Google search for "python cert_util" gives me no usable results.
Where do I get this module?
Header of the pycert_bearssl.py
# Dependencies:
# click - Install with 'sudo pip install click' (omit sudo on windows)
# PyOpenSSL - See homepage: https://pyopenssl.readthedocs.org/en/latest/
# Should just be a 'sudo pip install pyopenssl' command, HOWEVER
# on Windows you probably need a precompiled binary version. Try
# installing with pip and if you see errors when running that
# OpenSSL can't be found then try installing egenix's prebuilt
# PyOpenSSL library and OpenSSL lib:
# http://www.egenix.com/products/python/pyOpenSSL/
# certifi - Install with 'sudo pip install certifi' (omit sudo on windows)
import cert_util
import click
import certifi
from OpenSSL import crypto

nrfutil - "ImportError: No module named main" on Nixos

I'm using the tool nrfutil which is implemented in Python. To be able to use it under NixOS I was using a default.nix file, that installed nrfutil into a venv. This worked for some time very well. (The last build on the build server using Nix within an alpine container could build the software I'm working on 11 days ago successfully.) When I do exactly the same things (i.e. restarting the CI server build without changes), the build fails now complaining about pip being incorrect:
$ nix-shell
New python executable in /home/matthias/source/tbconnect/bootloader/.venv/bin/python2.7
Not overwriting existing python script /home/matthias/source/tbconnect/bootloader/.venv/bin/python (you must use /home/matthias/source/tbconnect/bootloader/.venv/bin/python2.7)
Installing pip, wheel...
done.
Traceback (most recent call last):
File "/home/matthias/source/tbconnect/bootloader/.venv/bin/pip", line 6, in <module>
from pip._internal.main import main
ImportError: No module named main
To me it seems that the module main should exist:
$ ls -l .venv/lib/python2.7/site-packages/pip/_internal/main.py
-rw-r--r-- 1 matthias matthias 1359 10月 15 12:27 .venv/lib/python2.7/site-packages/pip/_internal/main.py
I'm not very much into the Python environment, so I don't know any further. Has somebody any pointer for me where to continue debugging? How is Python resolving modules? Why doesn't it find the module, that seems to be present to me?
This is my default.nix that I use to install pip:
with import <nixpkgs> {};
with pkgs.python27Packages;
stdenv.mkDerivation {
name = "impurePythonEnv";
buildInputs = [
automake
autoconf
gcc-arm-embedded-7
# these packages are required for virtualenv and pip to work:
#
python27Full
python27Packages.virtualenv
python27Packages.pip
# the following packages are related to the dependencies of your python
# project.
# In this particular example the python modules listed in the
# requirements.txt require the following packages to be installed locally
# in order to compile any binary extensions they may require.
#
taglib
openssl
git
stdenv
zlib ];
src = null;
shellHook = ''
# set SOURCE_DATE_EPOCH so that we can use python wheels
SOURCE_DATE_EPOCH=$(date +%s)
virtualenv --no-setuptools .venv
export PATH=$PWD/.venv/bin:$PATH
#pip install nrfutil
pip help
# the following is required to build micro_ecc_lib_nrf52.a in the SDK
export GNU_INSTALL_ROOT="${gcc-arm-embedded-7}/bin/"
unset CC
'';
}
I replaced pip install nrfutil with pip help to make sure the problem is not the package I try to install itself.
I'm still using python 2.7 as the nrfutil still is not fit for Python 3.
Anyway replacing python27 with python37 did not change the error I get when trying to start pip.)
NixOS version used locally is 19.09. Nix in the CI docker container is nixos/nix:latest which is the nix package manager on Alpine Linux.
Update:
Actually it works when I replace the call to pip install nrfutil with python2.7 -m pip install nrfutil. This actually confuses me even more. python2.7 is exactly the binary that is in the shebang of pip:
[nix-shell:~/source/tbconnect/bootloader]$ type python2.7
python2.7 is /home/matthias/source/tbconnect/bootloader/.venv/bin/python2.7
[nix-shell:~/source/tbconnect/bootloader]$ type pip
pip is /home/matthias/source/tbconnect/bootloader/.venv/bin/pip
[nix-shell:~/source/tbconnect/bootloader]$ head --lines 2 .venv/bin/pip
#!/home/matthias/source/tbconnect/bootloader/.venv/bin/python2.7
# -*- coding: utf-8 -*-
Update 2:
I found out that another way to fix the problem is to edit .venv/bin/pip. This script tried the following import:
from pip._internal.main import main
Which I think is the new module path starting with pip 19.3. But I still have pip 19.2. When I change this line to:
from pip._internal import main
Running pip by typing pip is working.
The thing is I have no idea why the pip script is trying to load the new module path while NixOS still has the old version of pip.
I also opened an issue for NixOS on GitHub: https://github.com/NixOS/nixpkgs/issues/71178
I got your shell derivation to work by dropping the Python27Packages.pip,
(nix-shell) 2d [azul:/tmp/lixo12333] $
>>> pip list
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Package Version
---------------- -------
behave 1.2.6
Click 7.0
crcmod 1.7
ecdsa 0.13.3
enum34 1.1.6
future 0.18.2
intelhex 2.2.1
ipaddress 1.0.23
libusb1 1.7.1
linecache2 1.0.0
nrfutil 5.2.0
parse 1.12.1
parse-type 0.5.2
pc-ble-driver-py 0.11.4
piccata 1.0.1
pip 19.3.1
protobuf 3.10.0
pyserial 3.4
pyspinel 1.0.0a3
PyYAML 4.2b4
setuptools 41.6.0
six 1.12.0
tqdm 4.37.0
traceback2 1.4.0
virtualenv 16.4.3
wheel 0.33.6
wrapt 1.11.2
(nix-shell) 2d [azul:/tmp/lixo12333] $
and my default.nix
with import <nixpkgs> {};
with pkgs.python27Packages;
stdenv.mkDerivation {
name = "impurePythonEnv";
buildInputs = [
automake
autoconf
gcc-arm-embedded-7
# these packages are required for virtualenv and pip to work:
#
python27Full
python27Packages.virtualenv
# the following packages are related to the dependencies of your python
# project.
# In this particular example the python modules listed in the
# requirements.txt require the following packages to be installed locally
# in order to compile any binary extensions they may require.
#
taglib
openssl
git
stdenv
zlib ];
src = null;
shellHook = ''
# set SOURCE_DATE_EPOCH so that we can use python wheels
SOURCE_DATE_EPOCH=$(date +%s)
virtualenv .venv
export PATH=$PWD/.venv/bin:$PATH
pip install nrfutil
#pip help
# the following is required to build micro_ecc_lib_nrf52.a in the SDK
export GNU_INSTALL_ROOT="${gcc-arm-embedded-7}/bin/"
unset CC
'';
}

How to pip install *.whl on Windows (using a wildcard)

For some reason, I cannot pip install %CD%\*.whl as I will then get:
Requirement 'C:\\Users\fredrik\\Downloads\\*.whl' looks like a filename, but the file does not exist
`*.whl is not a valid wheel filename.
On macOS (and I believe on Linux), I can do this without issues:
pip install *.whl
Processing ./certifi-2017.11.5-py2.py3-none-any.whl
Processing ./chardet-3.0.4-py2.py3-none-any.whl
Processing ./idna-2.6-py2.py3-none-any.whl
Processing ./requests-2.18.4-py2.py3-none-any.whl
Processing ./urllib3-1.22-py2.py3-none-any.whl
...
Why is there a difference in this behavior between the platforms?
Is there a preferred way to make this (pip install *.whl) work on Windows?
If you know the package name e.g. foo
You can use this:
python -m pip install --find-links=C:\Users\fredrik\Downloads foo
this will find any foo package e.g. foo-X.Y.Z-cp37-cp37m-win_amd64.whl etc...
If you don't know the package name then you can try:
FOR %%i in (C:\Users\fredrik\Downloads\*.whl) DO python -m pip install %i

How to install python-dateutil on Windows?

I'm trying to convert some date/times to UTC, which I thought would be dead simple in Python - batteries included, right? Well, it would be simple except that Python (2.6) doesn't include any tzinfo classes. No problem, a quick search turns up python-dateutil which should do exactly what I need.
The problem is that I need to install it on Windows. I was able to upack the .tar.gz2 distribution using 7-zip, but now I'm left with a collection of files and no guidance on how to proceed. When I try to run setup.py I get the error "No module named setuptools".
If dateutil is missing install it via:
pip install python-dateutil
Or on Ubuntu:
sudo apt-get install python-dateutil
Why didn't someone tell me I was being a total noob? All I had to do was copy the dateutil directory to someplace in my Python path, and it was good to go.
Looks like the setup.py uses easy_install (i.e. setuptools). Just install the setuptools package and you will be all set.
To install setuptools in Python 2.6, see the answer to this question.
Install from the "Unofficial Windows Binaries for Python Extension Packages"
http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-dateutil
Pretty much has every package you would need.
It is a little tricky for people who is not used to command prompt. All you have
to do is open the directory where python is installed (C:\Python27 by default) and open the command prompt there (shift + right click and select open command window here) and then type :
python -m pip install python-dateutil
Hope that helps.
Using setup from distutils.core instead of setuptools in setup.py worked for me, too:
#from setuptools import setup
from distutils.core import setup
If you are offline and have untared the package, you can use command prompt.
Navigate to the untared folder and run:
python setup.py install
Just run command prompt as administrator and type this in.
easy_install python-dateutil
You could also change your PYTHONPATH:
$ python -c 'import dateutil'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named dateutil
$
$ PYTHONPATH="/usr/lib/python2.6/site-packages/python_dateutil-1.5-py2.6.egg":"${PYTHONPATH}"
$ export PYTHONPATH
$ python -c 'import dateutil'
$
Where /usr/lib/python2.6/site-packages/python_dateutil-1.5-py2.6.egg is the place dateutil was installed in my box (centos using sudo yum install python-dateutil15)
First confirm that you have in C:/python##/Lib/Site-packages/ a folder dateutil, perhaps you download it, you should already have pip,matplotlib, six##,,confirm you have installed dateutil by--- go to the cmd, cd /python, you should have a folder /Scripts. cd to Scripts, then type --pip install python-dateutil --
----This applies to windows 7 Ultimate 32bit, Python 3.4------
I followed several suggestions in this list without success. Finally got it installed on Windows using this method: I extracted the zip file and placed the folders under my python27 folder. In a DOS window, I navigated to the installed root folder from extracting the zip file (python-dateutil-2.6.0), then issued this command:
.\python setup.py install
Whammo-bammo it all worked.

Categories