Improve Your Python Shell With Pythonrc Python

Feb 7th, 2020 - written by Kimserey with .

Few weeks ago we looked at IPython, showcasing how the Python Shell with the additional features of IPython provides an incredible boost in productivity. In today’s post we will see how we can go a step further and provide quick scripting functionalities for testing or scripting.

Script File

When using the python shell, we can execute a script and immediately enter as interactive mode.

1
python3 -i my_file.py

This will execute the content of my_file.py and enter the python shell. So what we can do is maintain a script.py at the root of our project which will contain script functionalities to interact with our program.

For example, let’s image we had a module containing my_app:

1
2
3
/my_app
  __init__.py
  my_app.py

And within my_app we had two functions:

1
2
3
4
5
6
def say_hello():
    print("Hello")


def say_bye():
    print("Bye")

We can creating a script file script.py which will expose functionality helpers to interact with our module:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from my_app.my_app import say_hello, say_bye


class Script:
    def say_hello(self):
        """Calls say_hello from my_app"""
        say_hello()

    def say_bye(self):
        """Calls say_bye from my_app"""
        say_bye()


# Instance of script.
sc = Script()

The Script class will capture all functionalities and with sc we directly have access to an instance which makes discoverability easy using IPython autocompletion.

After running ipython3 -i script.py we will be able to see the function available:

autocomplete

Maintaining a script file is important as it provides us a quick access to our functionalities. We don’t have a re-establish the context, importing libraries, instantiating classes, calling methods or functions, everything is straight away available when we load ipython via the sc instance.

.pythonrc File

Specifying -i on the Python shell actually has an equivalent which is specifying a path stored with the environment variable PYTHONSTARTUP.

We set the Python startup to a file ~/.pythonrc.py and add the following content:

1
2
3
4
5
6
7
#!/usr/bin/env python3

import os

if os.path.isfile('.pythonrc.py'):
    with open('.pythonrc.py') as f:
        exec(f.read())

If a .pythonrc file is already existing, we can add those lines a the bottom of it. The script provides a way to customize the Python shell per projects. It achieves that by looking for an .pythonrc.py in the current folder where the Python shell is invoked.

We then rename our previous script.py to .pythonrc.py. Our folder structure is then:

1
2
3
4
/my_app
  __init__.py
  my_app.py
.pythonrc.py

We then execute python3 or ipython3 from our project directory and we will directly have access to our sc instance:

autocomplete

We are instantly ready to hack!

Conclusion

Today we looked into how we could setup a script to be used to interact with our application. A .pythonrc.py script is extremely useful when it comes to testing as it provides a painless, fast, efficient feedback loop for developping and iterating on your program. Combined with the PYTHONSTARTUP setup, it makes it an indispensible tool from the first day you start to use it. I hope you liked this post and I see you on the next one!

External Source

Designed, built and maintained by Kimserey Lam.