I was able to get geolite2 working on python2.7 - but i needed 3.4. I found the instructions for 2.7 on this link: http://pythonhosted.org/python-geoip/. Code fragments are also provided.
pip install python-geoip
pip install python-geoip-geolite2
>>> from geoip import geolite2
>>> match = geolite2.lookup('17.0.0.1')
>>> match is not None
True
So I naturally changed all the pip to pip3 and installed on a fresh VM. There is no error on the code but it does not looup and return values from their db.
pip3 install python-geoip
pip3 install python-geoip-geolite2
In the 2.7 VM, when I used 3.4, I pointed the geoip lib at /usr/local/lib/python2.7/dist-packages - no luck either. It however works on 2.7 on the same VM.
What should I need to do to make it to work on 3.4?
python-geoip does not support Python 3 and has not been updated in two years. Although there is a pull request to add Python 3 support, I would not expect it to be merged and released any time soon. I would recommend using the official MaxMind geoip2 package instead.
Install:
apt install python3-pip
pip3 install maxminddb
pip3 install maxminddb-geolite2
Example use:
#!/usr/bin/python3
# coding=utf-8
from geolite2 import geolite2
reader = geolite2.reader()
# google's ip
match = reader.get('172.217.16.163')
if match:
# print(match)
if 'country' in match:
print(match['country']['iso_code'])
else:
print(match['continent']['code'])
else:
print('')
python-geoip with python 3 support (install with pip or pip3):
pip3 install python-geoip-python3
output:
Collecting python-geoip-python3
Downloading python_geoip_python3-1.3-py2.py3-none-any.whl (7.4 kB)
Installing collected packages: python-geoip-python3
Successfully installed python-geoip-python3-1.3
for geolite2:
pip3 install python-geoip-geolite2
output:
Successfully built python-geoip-geolite2
Installing collected packages: python-geoip-geolite2
Successfully installed python-geoip-geolite2-2015.303
example with test_geoip.py:
#!/usr/bin/env python3
import socket
from geoip import geolite2
import argparse
import json
# Setup commandline arguments
parser = argparse.ArgumentParser(description='Get IP Geolocation info')
parser.add_argument('--hostname', action="store", dest="hostname", required=True)
# Parse arguments
given_args = parser.parse_args()
hostname = given_args.hostname
ip_address = socket.gethostbyname(hostname)
print("IP address: {0}".format(ip_address))
match = geolite2.lookup(ip_address)
if match is not None:
print('Country: ',match.country)
print('Continent: ',match.continent)
print('Time zone: ', match.timezone)
print('Location: ', match.location)
run:
python3 test_geoip.py --hostname=amazon.co.uk
output:
IP address: 178.236.7.220
Country: IE
Continent: EU
Time zone: Europe/Dublin
Location: (53.3478, -6.2597)
run again:
python3 test_geoip.py --hostname=amazon.co.uk
output:
IP address: 54.239.34.171
Country: US
Continent: NA
Time zone: America/Los_Angeles
Location: (47.6103, -122.3341)
To use the maxmind db:
Install:
maxminddb
maxminddb-geolite
In your code:
from geolite2 import geolite2
match = geolite2.reader()
geoip = match.get('xxx.xxx.xxx.xxx')
Related
When Running Installation,
pip install busio
Getting ERROR,
ERROR: Could not find a version that satisfies the requirement busio (from versions: none)
ERROR: No matching distribution found for busio
Python version is 3.7.3.
There is a module called "busio" (GitHub) by Adafruit which they describe as providing “hardware-driven interfaces for I2C, SPI, UART”.
This can be installed via Adafruit’s Blinka package:
pip3 install adafruit-blinka
There is no package called busio, Maybe you want to install buzio which is a python library tool for printing formatted text in terminal, to do so run :
pip install buzio
Testing your installation
from datetime import datetime, timedelta
from buzio import console
today = datetime.now()
yesterday = today - timedelta(days=1)
my_dict = {
"start day": yesterday,
"end day": today
}
console.box(my_dict, date_format="%a, %b-%d-%Y")
OUTPUT :
*********************************
* *
* start day: Wed, Nov-03-2021 *
* end day: Thu, Nov-04-2021 *
* *
*********************************
You should upgrade your pip using python -m pip install --upgrade pip or pip install --upgrade pip See more...
I would like to use the requests module on python but I cannot use it even though I installed on my mac terminal with pip command.
Here is the result of pip show command.
(base) MacBook-Pro:~ *******$ pip show requests
Name: requests
Version: 2.24.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: **********
License: Apache 2.0
Location: /Users/*********/opt/anaconda3/lib/python3.8/site-packages
Requires: chardet, urllib3, idna, certifi
Required-by: Sphinx, jupyterlab-server, conda, conda-build, anaconda-project, anaconda-client
My Python code
import requests
from bs4 import BeautifulSoup
url = "https://ja.wikipedia.org/wiki/メインページ"
response= requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
today = soup.find("div", attrs={"id": "on_this_day"}).text
entries = today.find_all("li")
today_list = []
index = 1
for entry in entries:
today_list.append([index, entry.get_text()])
index += 1
print(today_list)
Error message
ModuleNotFoundError: No module named 'requests'
Environment
VS Studio
Macbook M1 2020
Could you please help? I have no idea what to do.
with
pip --vesrion
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
You can see that pip installs module for python3. And with
pip show requests
Name: requests
Version: 2.24.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: **********
License: Apache 2.0
Location: /Users/*********/opt/anaconda3/lib/python3.8/site-packages
shows that Requests is installed for python3. VS running python2 cannot find the module cause it is not installed for python2. You should set VS to use only python 3.8. You can test your script in a terminal running the command :
python3 ./path/to/script.py
To install requests for python2 (which I don't recommend using as it has passed it's end of life)
pip2 install requests
Finally, lookup virtual environment in python, it's a feature designed to separate each of your project and it's dependancy from other projects.
https://docs.python.org/3/library/venv.html
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
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
'';
}
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.