Download Python
Nothing special.. go to to Python downloads page, get the file and install it on your system. Just a heads up: Do not install any other python libraries after installing python.
At the time of writing, there are 2 major version of Python: Python-2 and Python-3. Python 2.7.18 seems to be the last release of Python 2. The Python Software Foundation (PSF) declared that Python-2 will be sunset, mentioning that "As of January 1st, 2020 no new bug reports, fixes, or changes will be made to Python 2, and Python 2 is no longer supported". Unless you are absolutely sure, always go for Python-3, and for the latest version of Python 3.
To verify the installation on Windows, open the comman prompt and type python --version
which should give the version of installed Python. On Mac, open the "Terminal" application and type python -V
and that gives the version of installed python. For both PC and Mac, typing python
, either on the command prompt in PC or in Terminal on Mac, should open the Python Shell, where individual python commands can be run. A Python Prompt comprising of three greater-than symbols >>> will show up. If you want to exit from the python shell, type exit()
and press enter. References: Wikihow (PC, Mac install verification), StackOverflow (PC install verification), TutorialsPoint.
Download Pycharm
Python code are written in files with .py
extension. You can use any kind of text editor to write code. However, it is very easy and preferred to setup an Integrated Development Environment. For Python, one of the preferred IDE is Pycharm. The free community edition of Pycharm can be downloaded and installed for local development from this page. Once installed, feel free to open Pycharm. However, we'll need to do one more important setup before we start development.
Setting up virtual environment
When you write a python code, it may depend on other python libraries. When these libraries are installed, the default behavior is that they get installed in your system. So, if you write a project that does not depend on those libraries or on a different version of the libraries, then it creates a difficult situation with regards to managing the various versions of the libraries. There are various ways to manage libraries, but the multitude of option can itself get confusing. In this section, the use of venv
is suggested to manage libraries (Reference: StackOverflow). Readers are encouraged to read in details about venv
. In short, after the virtual evironment is activated, any installation of python libraries are done only in the virtual environment and not globally in your system's Python library. The keeps the system Python install "pristine", i.e. free from project dependent libraries. Each project can now define it's own virtual environment, activate it when the project is being worked on, and install libraries used by the project in the corresponding virtual environment. If it is needed to switch to another project, then simply deactivate the virtual environment for the first project and activate it for the next project.
To begin with, create a directory where you want to work on your python project. For PC, open command prompt, go to the python project directory, and type python -m venv myvenv
and press enter. The first value venv
is part of the command. The second value myvenv
is the name of subdirectory inside your python project directory where the libraries will be kept. Next, activate virtual environment by typing myvenv\Scripts\activate.bat
. Few things to note: (1) Please check the documentation for venv, the command might have changed. Or, maybe you are working on Mac or Linux, and the steps are different; (2) The command starts with myvenv
because that was the name of directory chosen when creating a virtual environment. If you chose a different name, then use that; (3) The sign of successful virtual environment activation is that you will see (myvenv)
prepended to the comand prompt. This is because "myvenv" was used as the name. If you use any other name, that will show up; (4) To deactivate the virtual environment, type deactivate
and press enter.
One more quick update.. We need to update the Pycharm settings, particularly, updating the python interpreter for the project. Now, you can add python from virtual environment, if Pycharm hasn't already detected it.
Install libraries using pip
This page is the official documentation on intalling libraries in Python. It mentions that pip
is the preferred installer program, and starting with Python 3.4, it is included by default with the Python binary installers. The documentation for pip
can be found here. Libraries can be installed using pip install ...
command. It is extremely suggested to define a requirements file in your python project directory and using the file to install and update libraries. An example of requirements file can be seen here. If the file name is "requirements.txt", then the command to install libraries by reading it would be pip install -r requirements.txt
. Every time the pip installer runs, it will add a library if it is missing, or update it to the version specified in the requirements file. BUT.. add and update the libraries to which place?
This is a great way to see the virtual environment in action. Once again, don't install any python libraries globally. Instead, go to your python project and activate the virtual environment (once again, a quick cross-check is that it should show (myvenv)
or corresponding virtual environment folder name before the prompt). Type pip list
to see the list of installed libraries. The list will show additional libraries after they are installed using pip
. Now, deactivate the vitual environment and type pip list
again. You'll notice that the list of installed libraries have greatly shrunk. This is because the virtual environment for the project has been deactivate, and so you are just seeing the basic libraries installed at the system level. This verifies how virtual environment keeps your system clean by installing project level libraries at the corresponding level rather than getting installed at global or system level.
Working with Git
Modern code development process involves developing code at local machine and then sending it back to a code repository existing on internet. Since code can come from various developers, there are "version control system" that manage the entry of new code/changes to the existing code. One such version control system that is hugely popular is Git which is used by code repositories like Github, Gitlab, etc. When working with Git, there are few more configurations that could be explored and are mentioned below.
Git bash on windows
For downloading Git for windows, it also installs Git Bash. Git Bash emulates a bash environment on windows. It enables running git features in command line plus most of standard unix commands (Reference: Git for windows, SuperUser's StackExchange. If you are using Git Bash for working with Git, then a sall extra configuration is needed to enable running Python (installed on Windows) from Git Bash. The steps are listed here.
Adding code quality check tools
generally having a high quality code is a good thing to have! It can improve code readability, making it easier for other developers to understand the intent of a code and to use it in future efforts. When working on a personal project, it may seem unnecessary to care about code quality tools, but, including them is a good way to get used to way coding is expected from a software developer. This article has a good list of various code quality check tools. Personally, I really like Black, Flake8 and ISort. For big python projects, it is also very helpful to provide type hints in the code so that other developers can understand the intent of the code, expected argument type and return types; To achieve this goal, MyPy can be used which is an optional static type checker for Python. Last, it might be useful to have these tools automatically get invoked every time the code that you wrote is checked in to a code repository. When working with Git, pre-commit hooks can be setup that get invoked every time a code is checked in. The steps to do so are covered in details here and primarily rely on using the pre-commit python library.