Python Development: Getting Started

I just started developing some little IoT projects using Python. I am picking up on some suggested practices along the way that seem worthwhile to keep a record of here.

Apparently developing in Python can often involve version dependencies imposed on projects. Similar scenario to Java where you might have an env var set to a certain version so it applies to your entire machine, but you occasionally need to work on a project that was created with an older version.

Instead of changing your env var or path, in python they have a concept of virtual environments. This provides a memory space in which you execute code in a specific version of Python without having to worry about what version is installed on your system. Neat!

I am going through a bunch of small example projects, and I noticed that I am repeating myself setting them up. Time to write a script.

So, I wrote a script to tell Python to install a .venv directory, and then activate it. Only it wouldn’t do the activation. Activation requires source-ing a binary in .venv. That is not a process I have ever used in a script – what does it mean?


It turns out that calling for a “source” operation in a script is just not the same as typing source in console. I need to adjust my mental model:

 scripts === console 

is now

 scripts == console. 

OK. I think I can adjust if the rest of the world already has. Until I find a way to avoid this.

I managed to find a script that was pretty close to what I wanted. The only thing I don’t like about it is that I have to invoke it with source in order to get the (.venv) prompt to appear in the console. At least the original writer added some logic to indicate this fact if it is run with a regular invocation.


if [[ "$0" = "$BASH_SOURCE" ]]; then
    echo "Needs to be run using source: ."
    # makes a virtual environment to run a specific version of 
    # python in without changing it everywhere on machine
    /usr/bin/python3 -m venv .venv
    #activates venv and shows (.venv) in prompts to indicate 
    if [[ $# -eq 1 ]]; then 
        if [ -d $1 ]; then
            echo "Virtual environment $1 not found"

    elif [ -d "venv" ]; then 

    echo "Activating virtual environment $VENVPATH,
    type deactivate to end it."
    source "$VENVPATH"

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha loading...

This site uses Akismet to reduce spam. Learn how your comment data is processed.