When we run a command like python --version from CLI, Windows looks for python.exe from the “Environment Variables”, specifically checks the PATH variable. If you want to know from where the executable got resolved, you can use a handy utility called where.exe:
Testing is and will be essential when coding. To make our life easier, developers created many frameworks and helpers to quickly achieve high code coverage. One well know library for Python is Pytest, which we are going to use in this article.
Start a new project/folder using your favorite editor and create a requirements file for PIP, we are going to use test_requirements.txt. Add our only dependency:
pytest
Install our dependency with the following command (running from the folder where the test_requirements.txt is):
pip install -r test_requirements.txt
Now we can add our code file with the name basic_calculator.py:
It doesn’t do anything fancy, just sums two number.
Now comes the test. Add a new file called test_basic_calculator.py:
import basic_calculator
class TestHandler:
def test_sum_one(self):
sum = basic_calculator.sum(2,3)
assert sum == 5
def test_sum_two(self):
sum = basic_calculator.sum(66,55)
assert sum == 121
We are done with coding, let’s run our test (give the following command from folder):
python -m pytest
The result should be something similar to this:
What happened here? We asked python to run the module called pytest in the working folder. Pytest then searches for filenames which start with “test_” and looks for classes starting with “Test” containing function names starting with “test”.
Narrowing down the test scope
We might want to run test only on one file, to do so we can specify it by name:
We can easily rotate between them using the following command:
setx AWS_PROFILE profil1
To see which profile is currently selected use this code:
aws configure list
Note: As the above logic uses environment variable to save and read the currently selected profile, we need to restart applications to the update take effect.
Two main places to put your AWS credentials are environment variables and AWS’ config file.
Environment variables
In the Windows search type in “environment variables” hit Enter:
Hit “Environment Variables” button and under “User variables for xy” add these two keys: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, values should be copied from AWS’ console.
Windows Subsystem for Linux (WSL) is a great tool to virtualize Linux distributions on Windows. Common need is to share files between the two systems. Easiest way is to mount a Windows folder to the Linux subsystem.
Using the following code lines we are going to make Windows’ folder under path “c:/shared” available for Ubuntu on the path “/mnt/path_on_linux”.
Start up WSL command line tool and create the folder:
sudo mkdir /mnt/path_on_linux
Note:mkdir is the command to create folder
Note:sudo gives administrative rights to the command, it might ask for your admin password
I am quite new to python programming but happy to use it for quick test cases like API calls or file operations. To run these scenarios I run them in different ways:
From file
One of the easiest way to call your script is to store them in a file and make python run them:
print('I am run from a file!')
content of run-me.py
Run it from command line:
python run-me.py
From inline command line
If you don’t want to save your code to a file, you can run it from inline:
python -c "print('I am from inline command line')"
From inline command line, multi line
Normally python code is segmented by new lines and indentation, but inline we can separate commands by semicolons: