Skip to content

Using Python Notes



  • Using python terminal in PyCharm with virtualenv messes up the python terminal everywhere else. Need to exit PyCharm Python Terminal if I want to use the virtualenv from Mac OS X terminal.
  • Reload module from REPL
    • import imp
    • import.reload(module) - module is the name of the module to reload
  • Import packages from current directory
    • from . import package - will look in current directory for the package
  • Shuffle list - random sequence for list
    • import random
    • list = [a,b,c,d]
    • random.shuffle(list)
    • for item in list:
  • Issue with ASCII environment
  • Special Method Names
  • Issue with readline
    • Terminal all screwed up after using python repl
      • sudo port install python36 +readline - fixes issues with python and terminal Mac OS X
  • Merge dictionaries
  • z = {**x, **y} - Merge Python Dictionaries

  • uwsgi

  • wsgi = file
  • uwsgi --socket --protocol=http -H /Users/themartins/Envs/lookfindmeAPI-jd3kET2g -w wsgi
  • uwsgi --socket --protocol=http --wsgi-file --callable myApp -H /Users/themartins/Envs/lookfindmeAPI-jd3kET2g

Pip Now using pipenv

  • Upgrade pip - pip install --upgrade pip
  • Error message: “Can not perform a ‘–user’ install. User site-packages are not visible in this virtualenv”
  • Problems ~/Library/Application Support/pip/pip.conf contains user=true
  • Resolution: Override the config in your virtualenv pip config file
    • Create $VIRTUAL_ENV/pip.conf
    • Place config information to override user=true
  • Pip Configuration | StackOverflow Issue


Install pip as user

  • get the script
    • Get the script wget
    • Run the script with user option pytoh --user
  • Now install all pip packages into user space. pip install --user <package>

Setup pip config file - want to set default settings for command line. Like always install in user space.

  • Create file $HOME/Library/Application Support/pip/pip.conf
  • Edit file
    user = true

Packaging python

Uwsgi and NGINX

Notes for configuring uwsgi and Nginx Using uwsgi to serve the python api on aws servers. * Digital Ocean - deploy python to wsgi - docs.

Using uwsgi.

  • Notes:
    • If api starts at /double than nginx config location should be /double
  • Packages needed - Ubuntu
    • apt install uwsgi-core uwsgi-plugin-python3 - need python3 plugin
  • pip3 --user install pipenv - need pipenv for our application.
  • Files need for uwsgi to work.
    • - startup python file to launch our application
    • uwsgi.ini - configuration file for our uwsgi server
    • Commands
      • uwsgi --ini uwsgi.ini --plugin python3 - start the server on ec2. Configuration will daemonize the app. Configuration already includes the config to daemonize it.
      • uwsgi --stop /tmp/ - stops the service.
  • file
    from app import lookfindme
    myApp = lookfindme.create_app()
    if __name__ == "__main__":
  • uwsgi.ini file (different local and server version)
    http-socket= :9090
  • nginx.conf - nginx config. nginx already includes uwsgi support.
    upstream flask {
    server {
      listen 80 default_server;
      listen [::]:80 default_server;
        location /double {
          include /etc/nginx/uwsgi_params;
          uwsgi_pass flask;
          uwsgi_param Host $host;
          uwsgi_param X-Real-IP $remote_addr;
          uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
          uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;

Last update: April 13, 2020 16:50:19