Outils pour utilisateurs

Outils du site


blog

Python Pb pip

Erreur - ImportError: cannot import name IncompleteRead

ImportError: cannot import name IncompleteRead
Solution
apt-get remove python-pip
easy_install pip

Erreur 'extras_require' must be a directory whose

Solution
sudo -H pip install --upgrade --ignore-installed pip setuptools

Erreur binary_only = FormatControl(set(), {':all:'})

pip install --upgrade pip
$ pip                      
Traceback (most recent call last):
  File "/usr/bin/pip", line 7, in <module>
    from pip._internal.main import main
  File "/usr/lib/python2.6/site-packages/pip/_internal/main.py", line 13, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/usr/lib/python2.6/site-packages/pip/_internal/cli/autocompletion.py", line 11, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/usr/lib/python2.6/site-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
    from pip._internal.cli import cmdoptions
  File "/usr/lib/python2.6/site-packages/pip/_internal/cli/cmdoptions.py", line 105
    binary_only = FormatControl(set(), {':all:'})
                                               ^
SyntaxError: invalid syntax
Solution

Utiliser une version de pip compatible avec la version de python installé

Exemple pour Python 2.6 (RedHat / CentOS 6)

curl https://bootstrap.pypa.io/2.6/get-pip.py -o get-pip.py
python get-pip.py

Puis

sudo -H pip install --upgrade --ignore-installed pip setuptools

ERROR: XMLRPC request failed

$ pip3 search pulumi
ERROR: XMLRPC request failed [code: -32500]
RuntimeError: PyPI's XMLRPC API is currently disabled due to unmanageable load and will be deprecated in the near future. S
ee https://status.python.org/ for more information

Solution

pip3 install --user pip_search
pip_search pulumi
alias pip='function _pip(){
    if [ $1 = "search" ]; then
        pip_search "$2";
    else pip "$@";
    fi;
};_pip'

Erreur : plop.whl is not a supported wheel on this platform.

Création d'un venv

python3 -m venv plop_venv
cd plop_venv/
source bin/activate
$ python3.6 -m pip install ~/tmp/confluent_kafka-1.7.0-cp36-cp36m-manylinux2010_x86_64.whl
confluent_kafka-1.7.0-cp36-cp36m-manylinux2010_x86_64.whl is not a supported wheel on this platform.

# Logs
pip install /tmp/confluent_kafka-1.7.0-cp36-cp36m-manylinux2010_x86_64.whl --log pip.log
...
pip._vendor.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /pypi/pip/json (Caused by NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7f2e0d813c18>: Failed to establish a new connection: [Errno -2] Name or service not known',))
...

Premier pb, le serveur n'a pas accès à Internet. Regardons les dépendance de notre paquet whl. Une fois dézippé la liste des dépendances se trouve ici : confluent_kafka-1.7.0.dist-info/METADATA Dans notre cas, nous n'avons aucune dépendance stricte

mkdir plop
cd plop
unzip -x ../confluent_kafka-1.7.0-cp36-cp36m-manylinux2010_x86_64.whl

grep 'Requires-Dist' confluent_kafka-1.7.0.dist-info/METADATA |grep -v 'extra'
Requires-Dist: futures ; python_version < "3.2"
Requires-Dist: enum34 ; python_version < "3.4"
# Logs sans tentative de connexion à internet
pip install /tmp/confluent_kafka-1.7.0-cp36-cp36m-manylinux2010_x86_64.whl --log pip.log -f ./ --no-deps --no-index

# Sortir du venv
deactivate
Solution

Mettre à jour PIP

pip install /tmp/pip-21.3.1-py3-none-any.whl
pip install /tmp/confluent_kafka-1.7.0-cp36-cp36m-manylinux2010_x86_64.whl
2025/03/24 15:06

Python IDE

Voir aussi :

IDE dans le cloud (Code Ready Workspaces, Code Spaces, Gitpod…)

PyCharm

Install de PyCharm

apt install python-pip python3-pip snapd
snap install pycharm-community --classic
Modules
Debug
apt-get install python3-dev

et Shift + F9

Visual Studio code - VSCodium - vscode

Atom

Geany

Dark mode

mkdir ~/.config/geany/colorschemes/
cd ~/.config/geany/colorschemes/
wget https://raw.githubusercontent.com/geany/geany-themes/master/colorschemes/darcula.conf

Choose the theme on Geany by going to the view menu and selecting Change Color Scheme…

Source : https://draculatheme.com/geany

2025/03/24 15:06

Python GUI mobile smartphone

Voir :

  • Kivy
  • BeeWare / Toga

Kivy

Voir :

sudo apt update
sudo apt install -y git zip unzip openjdk-8-jdk python3-pip autoconf libtool pkg-config zlib1g-dev libncurses5-dev libncursesw5-dev libtinfo5 cmake libffi-dev libssl-dev
 
#pip3 install --user --upgrade Cython==0.29.19 virtualenv  # the --user should be removed if you do this in a venv
 
sudo apt-get install cython3
 
pipenv install kivy buildozer
pipenv shell
buildozer init
pip3 install Cython
buildozer -v android debug
2025/03/24 15:06

Python Flask Gunicorn - Alternative à CGI

Voir :

Alternative à Flask :

  • CherryPy
  • Falcon

Alternative à Unicorn

  • Uvicorn
apt-get install python3-flask gunicorn3

wsgi.py

#! /usr/bin/env python3
 
from plopweb import app                 
 
if __name__ == "__main__":             
    app.run(                                                                                          
            host="0.0.0.0",
            port=int("5001")
            )

plopweb.py

#! /usr/bin/env python3    
 
import subprocess                                                  
from flask import Flask, request, send_from_directory, after_this_request, abort
app = Flask(__name__)
from subprocess import call
 
LOGFILE='/tmp/plop.txt'
CMD_ANSIBLE='/usr/bin/ansible-playbook'
 
def bash_command(cmd):
    subprocess.Popen(cmd, shell=True, executable='/bin/bash')
 
@app.route('/')
def hello_world():
    return 'Hello World!'
 
@app.route('/hello', methods=['POST'])
def hello():
    playbook = request.form['playbook']
    ip = request.environ['REMOTE_ADDR']
    bash_command('echo ' + CMD_ANSIBLE + ' ' + playbook +' -i ' + ip + ',' + '>>' + LOGFILE + ' 2>&1')
    return ''
 
if __name__ == '__main__':
    app.run(
            debug=True,
            host="0.0.0.0",
            port=int("5001")
            )

wsgi.py

#! /usr/bin/env python3
 
from plopweb import app
 
if __name__ == "__main__":
    app.run(
            host="0.0.0.0",
            port=int("5001")
            )
gunicorn3 --bind 0.0.0.0:5001  wsgi:app

Avec ça vous pouvez faire un script SystemD ou utiliser Supervisor, et mettre un nginx en reverse proxy

Exemple supervisor

apt-get install supervisor

supervisor-plopweb.conf

[program:plopweb]
command = gunicorn3 --bind 127.0.0.1:8000  wsgi:app
directory = /home/pkiweb/bin
user = plopweb

Exemple de conf nginx

plopweb

server {
        listen 443 ssl;
        server_name plopweb.lan ;
 
        access_log /var/log/nginx/plopweb-ssl-access.log;
        error_log  /var/log/nginx/plopweb-ssl-error.log;
 
        ssl_certificate     /etc/nginx/ssl/plopweb.lan.combined.crt;
        ssl_certificate_key /etc/nginx/ssl/plopweb.lan.key;
        ssl_client_certificate /etc/nginx/ssl/ca.crt;
        ssl_verify_client on;
 
        location / {
                proxy_pass http://localhost:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
        }
}
2025/03/24 15:06

Python, Flask et Supervisor à la place des vieilles CGI

Quand nous faisons une requête web (un POST par exemple) sur une URL, nous souhaitons lancer la commande ci-dessous

./script.sh -a -f prefix-/${POST_VAR} >> plop.log 2>&1

Voici une alternative aux CGI

Si besoin activer les dépôts backports :

echo "deb http://ftp.debian.org/debian jessie-backports main" >> /etc/apt/sources.list
 
apt-get update
apt-get install python3-flask gunicorn3 supervisor

plop.py

#! /usr/bin/env python3
 
import subprocess
import re
 
from flask import Flask, request, send_from_directory, after_this_request, abort
app = Flask(__name__)
 
from subprocess import call
 
LOGFILE='plop.log'
 
# MUST BE ENDED BY "/"
DL_DIR='/home/plop/foo/'
 
SCRIPT_ARGS='-a -f'
 
SCRIPT='./script.sh'
 
def bash_command(cmd):
    subprocess.Popen(cmd, shell=True, executable='/bin/bash')
 
@app.route('/')
def hello_world():
    return 'Hello World!'
 
@app.route('/hello/', methods=['POST'])
def hello():
    var_post = request.form['var_post']
    bash_command(SCRIPT + ' ' + SCRIPT_ARGS + ' ' + 'prefix-' + var_post + ' >>' + LOGFILE + ' 2>&1')
    return ''
 
@app.route('/dl/<codename>')
def download_file(filename):
    @after_this_request
    def delete_file(response):
        if re.match('^prefix-.*\.key$', filename):
            bash_command('shred -u ' + DL_DIR + filename)
        return response
    if re.match('^prefix-', filename):
        return send_from_directory(DL_DIR, filename, as_attachment=True)
    else:
        abort(403)
 
if __name__ == '__main__':
    app.run(
            #debug=True,
            #host="0.0.0.0",
            port=int("5001")
            )

Pour tester sur le post TCP 5001 avant du passer en prod sur le port 8000

wsgi.py

#! /usr/bin/env python3
 
from plop import app
 
if __name__ == "__main__":
    app.run()

/etc/supervisor/conf.d/plop.conf

[program:plop]
command = gunicorn3 --bind 127.0.0.1:8000  wsgi:app
directory = /home/plop/bin
user = plop

Note : A la place du Supervisor elle est possible d'utiliser SystemD ou S6

2025/03/24 15:06
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki