I’ve got my new Windows PC and I’ve installed Python ‘the right way’.
Installing Python is a nightmare for me especially in my last company-issued laptop. Now that I have my own PC, freshly booted, I am going to share with you how I installed Python successfully. Now, this is just a confirmation to the number of tutorials I’ve read, reddit comments that I’ve also read, and Stackoverflows I follow.
Why I am installing Python in my machine? I need to build my portfolio in Machine Learning and it looks like algorithmic trading with ML is a nice thing to explore. In order to do that, I have researched quite a few articles that shows us how to do trading automatically. This supplements the tutorials because they assumed we already have Python already installed in our machines.
Python Setup
Download
Granted that your PC is fresh, we are going to go to Python using the official website https://www.python.org/.
As of this writing, Python 3.11.5 is the current stable release. Just click it because the website automatically recognizes your machine if it is Windows or Mac.
Once downloaded, open the file. Do not run as administrator because we only want to install Python in user level (because instaling in admin level could change your system in admin level too). This is a great way to install Python to make virtual environments which is a very nice tool for making Python projects ‘the right way’.
Installation
Check the box for “Add to PATH”. This is important because we want to execute python via Windows PowerShell or Command Prompt.
Click Install Now.
Now that setup was successful, click the ‘Disable path length limit.’ This will be helpful in future events using Python with regards to character limitation and does not harm your computer in anyway.
Python Project Configuration
Okay, installation has finished but this feels not so good. What happens after the installation? How could we know we are doing python projects ‘the right way’? Virtual environments are helpful for this question.
Windows PowerShell
We can use Windows PowerShell as a terminal for configuring our virtual environment.
Open Windows PowerShell. You can right click the Windows Logo and choose ‘Terminal’. You could also ‘Windows Logo’ + R and type ‘powershell’.
This is the UI of Windows PowerShell. Let’s check if PowerShell could see the python version you have in your machine.
Type the following code snippet:
python --version
You could also explore python to be familiar with this powerful programming technology. You could type in the following code:
python --help
This will output the following in your screen.
Now, we want to make a Python project! We also want to be organized so future projects doesn’t interfere with our current project.
Using shell commands mkdir
, 1s
, and cd
we can create folder, show list, and change directory in our Windows machine.
The first step is to create a folder and name it to your liking. In my experience, I have used Git for version control and will use Git as my folder name. I will also add another folder name for my GitHub username ‘vbcalinao’ because I am also exploring other repositories as well and others have different GitHub usernames too. Inside the folder, there goes the name of the project, let’s name our project ‘pytrading’.
ls
mkdir Git
cd Git
mkdir vbcalinao
cd vbcalinao
mkdir pytrading
cd pytrading
This will be the output of the lines we inputted
This is just my preference to be organized and you could have your own preference as well as long as you prioritize organization to your files, switching between files and recognizing which is which would be a no brainer to your future self.
Virtual Environment
We need to install virtual environment ‘virtualenv’ library to our package installer ‘pip’.
pip install virtualenv
Activating a venv
venv for Python3 allows you to manage separate package installation for different projects. From the official Python documentation:
“When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments. It is always recommended to use a virtual environment while developing Python applications.”
To create a venv, make sure you are located in the project’s directory.
py -m venv env
The env
is the directory name and the location to create the virtual environment. This is also the most popular name you can assign as most of seasoned programmers knows.
venv
creates a virtual Python installation inside the env
folder. You can actually open and see the ‘env’ folder in you Explorer.
Activating a virtual environment
You have a virtual environment ready, but not activated. To activate it using PowerShell, type:
.\env\Scripts\activate
In my machine, I am having an error because of some security and unauthorized access error. This is expected as Windows employs restriction and execution policies. To bypass this, type in
Set-ExecutionPolicy Unrestricted -Scope Process
This would allow running virtualenv in the current PowerShell session as noted in this stackoverflow forum.
A green (env)
will show at the beginning of the line terminal to show that the current venv is active.
To deactivate the virtual environment, type in:
deactivate
Installing libraries in your venv
Make sure your venv is activated before you install any libraries using the package manager pip. To install a package, type in
pip list <packagename>
Now, take a look in this screenshot.
We could see that the pip list
command shows scikit-learn
library when venv
is activated and doesn’t show the library when it was deactivated. This is because the scikit-learn
is inside the env
folder we configured earlier. The libraries we see when venv
is deactivated is the libraries that exists in our main python.
That’s it! Thank you for reading this article and hope you understand a little bit more about Python.