Skip to main content

Python alias commands that play nice with virtualenv

There are plenty of predefined Python executables, symlinks, and aliases that come bundled with your operating system. These commands come in very handy because it saves you from typing out long commands or chain of scripts. However the downfall of operating system aliases is that they don’t always play nice with virtualenv (or venv if you’re on Python 3). Most predefined aliases use the system’s default Python as the interpreter, which is next to useless when your application runs in a virtual environment. Over the years, I’ve come up with my own Python aliases that play nice with virtual environments. For this post, I tried to stay as generic as possible such that any alias here can be used by every Pythonista. In other words, there will be no aliases for specific frameworks such as running a Django server or starting a Scrappy spider. The following is one of my bash scripts I source:

.py-aliases

#----- Pip -----#
alias pip-list="pip freeze | less"
alias pip-search="pip freeze | grep -i"
alias pip-remove="pip uninstall"
alias pip-upgrade="pip install -U"
alias pip-upgrade-all="pip freeze --local | grep -v '^\-e' | cut -d = -f 1|xargs -n1 pip install -U"

#----- Debugging -----#
alias pdb="python -m pdb"
alias ppjson="python -m json.tool"
alias module-locate="python -c 'from imp import find_module;from sys import argv;print(find_module(argv[1])[1])'"

#----- Documentation -----#
alias pydoc="python -m pydoc"

Pip

This set of commands is the most convenient because pip can be a bit awkward when installing, uninstalling, and upgrading modules. Astute dev-ops folks might notice that I took heavy inspiration from tools like apt-get and yum. These commands also imitate conda, so for those who are using Anaconda, checkout conda out.

  • pip-list: List all installed modules then pipe to a cursor so that it’s easy to scroll.
  • pip-search: Search installed Python modules
  • pip-remove: Uninstall Python module(s)
  • pip-upgrade: Upgrade Python module(s)
  • pip-upgrade-all: My favorite for development. Upgrades all the Python packages without the need to specifying them individually.

Debugging

  • pdb: This is self explanatory for veterans. Runs scripts in the environment’s Python debugger.
  • ppjson: Pretty print JSON from a file or curl output. If you work with JSON and RESTful web apps, then you need this!
  • module-locate: Locate the system file path to a particular Python module, which can be useful when debugging. This is actually a script on my machine, but to keep this post brief, I did a quick one-liner to capture the gist of my script.

Let's do a quick tutorial of ppjson. I generally use this to test JSON responses from web apps in conjunction with curl, treq, and/or requests. For example, lets get some Star Wars JSON from SWAPI and pipe it to our new ppjson alias:

curl -L http://swapi.co/api/people/1 | ppjson
{
      "name": "Luke Skywalker",
      "height": "1.72 m",
      "mass": "77 Kg",
      "hair_color": "Blond",
      "skin_color": "Caucasian",
      "eye_color": "Blue",
      "birth_year": "19 BBY",
      "gender": "Male",
      "homeworld": "http://swapi.co/api/planets/1/",
      "films": [
          "http://swapi.co/api/films/1/",
          "http://swapi.co/api/films/2/",
          "http://swapi.co/api/films/3/"
      ],
      "species": [
          "http://swapi.co/api/species/1/"
      ],
      "vehicles": [
          "http://swapi.co/api/vehicles/14/",
          "http://swapi.co/api/vehicles/30/"
      ],
      "starships": [
          "http://swapi.co/api/starships/12/",
          "http://swapi.co/api/starships/22/"
      ],
      "created": "2014-12-09T13:50:51.644000Z",
      "edited": "2014-12-10T13:52:43.172000Z",
      "url": "http://swapi.co/api/people/1/"
}

ppjson also works with files containing JSON.

Documentation

  • pydoc: Reference the environment PyDocs. This is generally very helpful (despite the HTML pages looking very ugly) when you need to get documentation of a module in your virutalenv. I regularly start the web server so that I can quickly reference builtin and installed modules without the need to google. Example: pydoc -b -p 9001

Comments

  1. For a simple version of module-locate:

    $ pip install e
    $ python -me pytest
    /home/tom/.virtualenvs/connect4/lib/python3.6/site-packages/pytest.py

    Literally 'python me' this 'python me' that! https://pypi.python.org/pypi/e

    ReplyDelete
  2. Here are some shortcuts, which I find extremely helpful:

    function pylib() {
    "$BROWSER" "https://docs.python.org/3/library/$1.html"
    }

    function pylib2() {
    "$BROWSER" "https://docs.python.org/2/library/$1.html"
    }

    function pypi() {
    "$BROWSER" "https://pypi.python.org/pypi/$1"
    }

    (I usually have set BROWSER to "xdg-open".)

    ReplyDelete
  3. I really appreciate information shared above. It’s of great help.

    python online training india

    ReplyDelete
  4. I was very much confused what to do next in my career, but after going through this article I got a clear version of undergoing Python Training in Chennai| Python Course in Chennai

    ReplyDelete
  5. Good information provided by thanks keep updating
    python online training

    ReplyDelete
  6. Your post is really awesome. Your blog is really helpful for me to develop my skills in a right way. Thanks for sharing this unique information with us.
    - Learn Digital Academy

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple self signed TLS server and client using Twisted

Self signed TLS server and client using TwistedPrerequisitesopenssltwisted & pyOpenSSL modules- pip install twisted[tls]treq module - pip install treqBasic knowledge of Twisted TCP servers/clients
Generate self signed certificate Generate the server's private key using a secret, which is SuperSecretPassword in this case. openssl genrsa -aes256 -passout pass:SuperSecretPassword -out server.key 2048 Perform a CSR (certificate signing request). Ensure the FQDN (fully qualified domain name) matches the hostname of the server, otherwise the server won't be properly validated. openssl req -new -key server.key -passin pass:SuperSecretPassword -out server.csr # Common Name (e.g. server FQDN or YOUR name) []:localhost openssl x509 -req -passin pass:SuperSecretPassword -days 1024 -in server.csr -signkey server.key -out server.crt For development purposes, remove the password from the certificate. openssl rsa -in server.key -out server_no_pass.key -passin pass:SuperSecretPassword mv…

Twisted Klein: Non-Blocking Recipes

Non-Blocking Recipes Do you like expressjs, but don’t want to switch to Node.js? Want non-blocking execution in Python? Then look no further! Asynchronous execution is the very essence of what makes Klein a contender in todays web framework landscape. It’s also the most difficult concept to grasp since most Pythonistas are not accustomed to asynchronous programming. Hopefully with the addition asyncio to Python’s standard library, this will change. Klein is built atop Twisted and developers can expose Deferred objects for asynchronous behavior. A very brief overview will be given on Twisted Deferred, afterwards aspiring developers are encouraged to read the Twisted documents on the subject (provided in the links near the bottom).

Deferred Overview To demonstrate how Deferred objects work, we will create a chain of callback functions that execute after a result is available. Don’t worry if this confuses you right now, the code will clear things up.

from__future__importprint_fun…

Twisted Klein: Database Usage

twisted.enterprise.adbapi Twisted, and subsequently Klein, allows asynchronous interaction with databases using blocking/sequential interfaces using adbapi. The only caveat is that the database interface must be DBAPI 2.0 compliant. For instance, if a PostgreSQL database needs to be accessed, then a typical interaction would be something like:

importpsycopg2connection=psycopg2.connect(database='Tutorial',user='admin',host='10.10.10.10')cursor=connection.cursor()cursor.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")cursor.execute("INSERT INTO test (num, data) VALUES (%s, %s)",(100,"mydata"))cursor.execute("SELECT * FROM test;")connection.commit()cursor.close()connection.close()
Depending on environment settings, connecting to the database server, inserting data, or even querying for records can take an unpredictable amount of time, which can cause your application to slow down considerab…