Git up and Running with Python on a Work Computer


The following is a quick guide to getting up and running quickly in Python on a Work Computer. I personally find Anaconda overkill for simply running some python scripts. Plus, Anaconda was removed from the corporate Software Center App. This process should take about 5 minutes to get up and running in a virtual environment on Vanilla Windows 10.

I covered a more detailed setup on Windows per Python, pip, WSL 1 Debian Setup on Windows 10.

Install Python and Visual Studio Code from the Software Center

Click the Windows button on the keyboard and type Software Center, and launch it.

Search for python in the search box.

Install Python. The current version in the center is 3.9.6.

While still in the Software Center, search for Visual Studio and install it. The current version is 1.64.2.

That should take care of the software installs.

Create and Activate a Python Virtual Environment with the Command Prompt

Windows Start > Type cmd to start the command prompt.

Type the following into the command prompt to check that Python is working correctly.

Note: replace sawl10233 with your computer user id.

C:\Users\sawl10233>python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'C:\\Program Files\\Python39'
>>> exit()

C:\Users\sawl10233>

Now open up Windows Explorer and navigate to the following folder: C:\Users\sawl10233 to create some new folders. I followed a naming convention similar to that done by Anaconda for consistency. i.e. C:\Users\sawl10233\.py_envs\envs

Head back to the command prompt and navigate to the newly created folder above using cd and create the Virtual environment per the following command python -m venv dev3_9. Feel free to use your naming convention here.

C:\Users\sawl10233>mkdir .py_envs
C:\Users\sawl10233>cd .py_envs
C:\Users\sawl10233\.py_envs>mkdir envs
C:\Users\sawl10233\.py_envs>cd envs
C:\Users\sawl10233\.py_envs\envs>
C:\Users\sawl10233\.py_envs\envs>
C:\Users\sawl10233\.py_envs\envs>python -m venv dev3_9

Then cd into the newly created folder and type activate

C:\Users\sawl10233\.py_envs\envs>cd dev3_9\Scripts

C:\Users\sawl10233\.py_envs\envs\dev3_9\Scripts>

C:\Users\sawl10233\.py_envs\envs\dev3_9\Scripts>activate # may have to type .\activate

(dev3_9) C:\Users\sawl10233\.py_envs\envs\dev3_9\Scripts>

For info, to deactivate, just key in deactivate. Notice how the environment dev3_9 disappears after running this command.

(dev3_9) C:\Users\sawl10233\.py_envs\envs\dev3_9\Scripts>deactivate

C:\Users\sawl10233\.py_envs\envs\dev3_9\Scripts>

Visual Studio Code Setup

Go to VS Code and open or create a python file. For illustrative purposes, let’s create a file with File > New > test.py

Install the Python Extension by Microsoft from the Extensions pane. Current version is v2022.4.1. This version is required to navigate to the Python interpreter in the next step.

Install the Jupyter Extension by Microsoft. After installation, go to File > Preferences > Settings and search for shift+enter and it will come up under Extensions > Jupyter > Send Selection to Interactive Window and put a checkbox this feature. This will run any code via Shift+Enter in a new Window pane and not in the terminal.

Visual studio code sceenshot
Visual Studio Code with base Python Instance Running

The base Python will probably show in the bottom right. We need to click this and navigate to the folder above and select the python.exe file or just input in the location C:\Users\sawl10233\.py_envs\envs\dev3_9\Scripts\python.exe

Visual studio code sceenshot
Visual Studio Code Running in the Virtual Environment Setup Above

Then open a new terminal, and if the environment is not activated, apply the same commands above in the command prompt into the terminal prompt. This is just the command prompt conveniently located right within VS Code.

Then install any required packages using the following command: python -m pip install <packages>

Visual studio code sceenshot with the split terminal shown
(1) VS Code > Terminal > New (2) (dev3_9) active (3) pip install matplotlib

Note: I was getting errors trying to install python -m pip install matplotlib. I was able to install successfully using the --prefer-binary flag as per the mathplotlib installation documentation.

Test Drive Your New Install with your first Python Script

I don’t use matplotlib as I prefer the much simpler plotly package. So, let’s install two further packages and write some code and check out the result.

The two further packages are numpy and plotly. python -m pip install numpy plotly

Visual studio code sceenshot with interactive split showing the chart
VS Code – (1) Highlight all the code and press Shift + Enter (2) Click the Variables button to show them below. Finally, an interactive chart is displayed.

Five minutes up, and hopefully, all is running well. Happy Coding!

 

Bonus: A little more practical script used for Checking the Status of Engineering Drawing Electronic Signatures

This script asks the user for a folder location where engineering drawings are stored and prints out a summary of signature statuses.

If any imported modules are not installed, you’ll get an error notifying you at runtime that they don’t exist. Do a web search, for example pypdf2 install and check the documentation for the correct syntax and dependencies required for pip installation and put that syntax in the terminal prompt per above.

import fitz
import pandas as pd
import glob
import numpy as np
import PyPDF2
from pathlib import Path

# Copy the folder link from Windows explorer address bar
folder = input("Please enter the folder location: ")

# Adjust this to suit
search_criteria = "*??????-?????-???-???-????.pdf"

# to search recursively in subfolders change glob to rglob
filenames = list(Path(folder).glob(search_criteria))


def sign_check(file):
    who = []
    has_v = []
    size = []
    file = str(file)
    if fitz.open(file).get_sigflags() == 3:
        ff = PyPDF2.PdfFileReader(file).getFields()
        sigs = list(ff)
        for i in range(len(ff)):
            has_v.append("/V" in ff[sigs[i]])
            size.append(len(ff[list(ff)[i]]))
        test = all(elem == True for elem in has_v)
        if test == True:
            result = "All Signed"
        else:
            result = "Missing Signatures"
            output = [i for i, x in enumerate(has_v) if not (x)]
            who = []
            for i in output:
                who.append(list(ff)[i])
            who = ", ".join(who)
    else:
        result = "No Signatures"
    return result, who


result = []
for i in filenames:
    result.append(sign_check(i))

df = pd.DataFrame(result, columns=["results", "missing_who"])
df["file_name"] = filenames
df["file"] = df["file_name"].apply(lambda x: x.name)
df = df[["file", "results", "missing_who"]]
print(df)
# df.query("results.str.contains('No|Miss')")# .to_clipboard()
# df.to_clipboard()

The results of my 4 test files are:

Visual studio code sceenshot showing results of the signature script
Status of Electronic Signatures on 4 Engineering Drawings