Category: Python

  • pip install mysqlclient mysql_config: not found

    On Ubuntu 18.04, when i install mysqlclient python module, i get error

    (venv) boby@sok-01:~/Downloads/django-deploy$ pip install mysqlclient==1.4.2.post1
    Collecting mysqlclient==1.4.2.post1
      Using cached https://files.pythonhosted.org/packages/f4/f1/3bb6f64ca7a429729413e6556b7ba5976df06019a5245a43d36032f1061e/mysqlclient-1.4.2.post1.tar.gz
        ERROR: Complete output from command python setup.py egg_info:
        ERROR: /bin/sh: 1: mysql_config: not found
        Traceback (most recent call last):
          File "", line 1, in 
          File "/tmp/pip-install-we5rsid9/mysqlclient/setup.py", line 16, in 
            metadata, options = get_config()
          File "/tmp/pip-install-we5rsid9/mysqlclient/setup_posix.py", line 51, in get_config
            libs = mysql_config("libs")
          File "/tmp/pip-install-we5rsid9/mysqlclient/setup_posix.py", line 29, in mysql_config
            raise EnvironmentError("%s not found" % (_mysql_config_path,))
        OSError: mysql_config not found
        ----------------------------------------
    ERROR: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-we5rsid9/mysqlclient/
    WARNING: You are using pip version 19.1.1, however version 19.2.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
    (venv) boby@sok-01:~/Downloads/django-deploy$
    

    this is fixed by installing libmysqlclient-dev

    apt install libmysqlclient-dev
    
  • ERROR: Failed building wheel for mysqlclient
  • See Python

  • Running Python Application with gunicorn and nginx

    Create a service file for gunicorn

    root@django:~# cat /etc/systemd/system/gunicorn2.service
    [Unit]
    Description=gunicorn2 daemon
    Requires=gunicorn2.socket
    After=network.target
    
    [Service]
    User=ubuntu
    Group=www-data
    WorkingDirectory=/home/ubuntu/myapp/wagtail2
    ExecStart=/home/ubuntu/myapp/venv/bin/gunicorn \
              --access-logfile - \
              --workers 3 \
              --bind unix:/run/gunicorn2.sock \
              wagtailblog4.wsgi:application
    
    [Install]
    WantedBy=multi-user.target
    root@django:~# 
    

    Here

    /home/ubuntu/myapp/wagtail2 = path to the folder where web application is.

    /home/ubuntu/myapp/venv/bin/gunicorn = is where gunicorn installed inside virtualenv.

    Change these path as required.

    Restart gunicorn with

    systemctl restart gunicorn2
    

    use following nginx config

    root@django:~# cat /etc/nginx/sites-enabled/django.conf
    server {
        server_name domain.extn;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/ubuntu/myapp/wagtail2;
        }
        
        location /media/ {
            root /home/ubuntu/myapp/wagtail2;    
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/run/gunicorn2.sock;
        }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/domain.extn/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/domain.extn/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    
    }
    server {
        if ($host = www.domain.extn) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
        if ($host = domain.extn) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
        listen 80;
        server_name domain.extn www.domain.extn;
        return 404; # managed by Certbot
    }
    root@django:~# 
    

    Restart nginx

    systemctl restart nginx
    
  • Installing PyCharm Community Edition on Ubuntu

    Installing PyCharm Community Edition on Ubuntu

    PyCharm Community Edition is a free python editor from JetBrains. You can download it from

    https://www.jetbrains.com/pycharm/download/#section=linux

    Download and extact the tar.gz file. Move it to a folder like

    tar xvf pycharm-community-2019.1.3.tar.gz
    mkdir ~/programs
    mv pycharm-community-2019.1.3 ~/programs
    

    Execute sartup script

    cd ~/programs/pycharm-community-2019.1.3/bin
    ./pycharm.sh
    

    Once pycharm started, you can go to Tools menu and create a startup script.

    pycharm python editor

  • Getting started with Django Framework

    Getting started with Django Framework

    Django is a python framework for developing web applications. To install Django, run

    pip install django
    

    To create a project, run

    django-admin startproject PROJECT_NAME
    

    Django come with built in development server. To start Django development server, change to the project folder and run

    ./manage.py runserver
    

    django framework

    It will start a development web server on url http://127.0.0.1:8000/

    Creating App

    A django project consists of multiple apps. Apps do a specific task, can be considered as a module. To create an app, run

    python manage.py startapp NAME_OF_APP
    

    To activate the app, you need to edit file

    vi PROJECT_NAME/settings.py
    

    Find

    INSTALLED_APPS
    

    this list all installed apps, you need to add name of your app to end of the list.

  • Running Web Server with python SimpleHTTPServer

    Running Web Server with python SimpleHTTPServer

    SimpleHTTPServer is a python module that allow you to run web server for static files.

    To start web server, run

    python -m SimpleHTTPServer 
    

    This will start a web server on port 8000.

    You will be able to access server in URL

    http://SERVER-IP:8000
    

    To stop server, press CTRL + C

    To get SimpleHTTPServer run on differnt port, run

    python -m SimpleHTTPServer 80
    

    This will run SimpleHTTPServer on port 80.

    Python 3

    Python 3 comes with bult in module http.server, to use it, run

    python3 -m http.server
    

    If you want to run on differnt port, specify port number

    python3 -m http.server 9001
    
  • Creating and using virtualenv

    To create a virtualenv, run

    virtualenv NAME_OF_VIRTUALENV
    

    virtualenv

    On Ubuntu 18, this command will create virtualenv with Python 2.7.

    To create virtualenv with Python 3, run

    virtualenv --python=/usr/bin/python3 venv
    

    Here “venv” is my virtualenv name.

    python3 virtualenv

    To activate virtialenv, go to the folder where your virtialenv folder is present, then run

    source venv/bin/activate
    

    You will see name of your virtualenv on the command promt. To exit out of virtualenv, run

    deactivate
    

    This can be run from any folder.

  • virtualenv

    Install virtualenv on Ubuntu
    Creating and using virtualenv

    virtualenv is used to create isolated python environments.

    With python3, you can create virtualenv with command

    python -m venv FOLDER_NAME
    

    Install virtualenv with setuptools

    easy_install virtualenv
    

    Create a folder

    mkdir /root/virt_env/
    

    Create virtualenv

    virtualenv -p /usr/python3/bin/python3.3 /root/virt_env/virt1  --no-site-packages
    

    Activate virtualenv

    source /root/virt_env/virt1/bin/activate
    

    To exit a virtualenv, run

    deactivate
    
  • Install virtualenv on Ubuntu

    To install virtualenv on Ubuntu, run

    sudo apt install virtualenv
    
  • gunicorn

    gunicorn behind Apache web server
    Running Python Application with gunicorn and nginx
    Serve a Django application using Gunicorn

  • Install jupyter notebook

    To install jupyter notebook on Ubuntu run

    pip install jupyter
    

    To start jupyter notebook, create an empty folder, change to it

    mkdir ~/book
    cd ~/book
    

    Now run

    jupyter notebook
    

    By default, it bind to localhost. If you want to run on a public IP, run as follows

    jupyter notebook --ip  198.50.234.187 --port 8888 --allow-root
    

    Example

  • Django Framework

    Django is a Python framework. You can find more info at

    https://www.djangoproject.com

    Django Tutorials

    Getting started with Django Framework
    https://realpython.com/tutorials/django/

    Create Project

    django-admin.py startproject 
    

    Create New Application

    python manage.py startapp 
    

    Run Server

    python manage.py runserver [IP:PORT]
    

    Django framework run server command start development web server on port 8000

    To run server on port 80, use

    python manage.py runserver 0.0.0.0:80
    

    if you are running as non root user, you won’t be able to use port 80. The solution is to run as user root or install a reverse proxy like Nginx.

    Applications using Django

    https://wagtail.io