armchair dot tech
python console import tip | HOME | ABOUT | TAGS

August 29, 2012 | TAGS:

Tags: python console, python, import, pysphere

This is a quick little python tip. When experimenting with python commands and modules, it’s usually easiest to use the python console interactively, then create your programs later. The downside of this is that sometimes you have to do a bit of typing before you get to the specific command you want to try.

Imagine the following example:

Here, I had to type in 3 lines, including my password in plaintext, to test out querying the server. I can’t demonstrate this live, because then I reveal my password. Well last week, I made a [] file that reads a yaml configuration file and does the commands I just did. Here’s the smart bit. I can import that file directly into the python console. Once it imports, it runs each python command and leaves me in the console, ready to query the system again. The only caveat is that my “server” variable is now part of the test1 module as “test1.server”.