Want to Learn Python? — What Nobody Tells You Until It Is Too Late!
Set up your Python environment NOW….even before you type “Hello World” for the first time!
Are you about to embark on your first explorations into the world of Python programming? Are you totally new to coding and software development? Are you about to jump right in and start following your first Python tutorial? If so, then I encourage you to read on and learn from my mistakes.
Three months ago, I would have answered yes to all three questions. I was full of excitement and eagerness to open my newly installed editor and write some code. Having purchased a new 2019 MacBook Pro as my weapon of choice, selected Python as the best language to learn as a beginner, formulated a list of courses and online tutorials to work through, and with a lifetime of pent-up interest in wanting to explore the world of computer programming, I was raring to go.
So why am I writing this article? What have I learned in just three months as a newbie in the programming world that would compel me to write my first ever article on a topic that is still so new to me?
I have learned what no course or tutorial has even tried to teach me. In fact, following courses, tutorials, blogs, and community posts have actually created the problem that I have had to learn the hard way to fix — the importance of the Python development environment.
Hopefully, my story will stop you from making the same mistakes as me!
What Did I Do Wrong?
“I simply followed instructions without enough knowledge or understanding of the consequences.”
Almost without exception, within the first couple of chapters of most courses and tutorials, instructions are given as to how to install Python to your computer. However, these installation instructions often differ between each course and tutorial, each suggesting to install Python from a specific source or in a way that matches the personal preference or experience of the teacher.
The focus is always on getting set up quickly so that you can get to the exciting stuff…writing code, because that’s what you are there to do, right?
What I have experienced in most cases, is that no context is given to the pros or cons of installing Python from the stated sources. No explanation of where Python will be installed on your machine. No commentary as to what directories may be added to your “PATH” variable or why (assuming you even know what the “PATH” variable is in Chapter 2 of your first online Python course. I certainly didn’t!).
Maybe, more importantly, there are no warnings about what complications you may face in the future should you end up with multiple versions of Python installed from all these different sources.
And then there is PIP! Most courses and tutorials actually do mention PIP and give an overview of what you will need to use it for as you progress through the course. What they ALL fail to mention at this early stage is where the packages you install with PIP will be stored and the future implications of simply installing every package globally on your computer.
Considering that all these critical environment topics are not even discussed at this stage of courses and tutorials, it will come as no surprise to you that there is, therefore, no explanation of any tools or best practices that can be used to help manage this complexity and actually help you to create a suitable and sustainable development environment.
So in my ignorance, I continued through many courses and tutorials whilst branching off occasionally to look more closely at specific topics that interested me. The time happily installing multiple versions of Python from different sources, installing packages and modules at will to support my learning, getting increasingly pleased with the progress I was making, and totally unaware of the problem I was creating for myself in the future.
What Did This Mean for Me?
“After three months, I was beginning to get to grips with the basics of the Python language and could solve some basic programming challenges, BUT my Python environment was now unworkable…”
Having developed a good understanding of the syntax, fundamental principles, and capabilities of the Python language and having started to self-explore some specific topics like web scraping and data processing/plotting, I was now ready to embark on some small projects to develop my skills further.
I created a new projects folder and set it to work setting up the project structure. The project only needed two specific external packages (NumPy and Panda’s plus the associated dependencies). However, these had already been installed during one of the training courses and were now mixed up in a globally installed library with over 100 other Python packages and dependencies. Which dependencies related to NumPy and Panda’s specifically, I had no clue. How could I isolate these packages and dependencies to create a ‘Requirements” file to be included in the project folder? Again, no clue. This was the first time that I realized I had a problem!
And it wasn’t long before I’d run into my second problem. When running Python from the command line I had no idea how to control which version of Python would actually run. By this time, I had the MAC System Python 2.7, multiple Python 3 versions installed via Homebrew, an official Python.org download of Python 3, and an installation through Anaconda. Furthermore, my “PATH” variable was a mess. Each install of Python had added folders to either the beginning or the end of the “PATH” variable and I had no idea how to fix it!
I felt totally deflated, whilst my actual Python programming skills had developed well, I realized that I had no idea how to manage my Python development environment. How could I build and deploy useful programs if I couldn’t manage and organize them in a controlled and structured way? Why had none of the courses warned about these issues and explained how to manage the environment…I actually felt quite angry that I had ended up in this position!
How Did I Fix It?
“Very painfully, I rewound everything right back to day one, in fact, before day one”
I had no choice. I had to clean my environment. Using various Python community forums and after asking lots of questions, one by one, I worked through each version of Python I had installed, identifying where all the associated folders, files, libraries, packages etc were stored, and then performed the cleanest and safest way to uninstall and delete each one.
As I removed each version it was essential not to touch anything related to the system install of Python 2.7. The last thing I wanted to do was to break the MAC operating system as well!
Whilst this was a very slow and painful exercise it certainly helped me learn how to navigate around the MAC’s file structure and to understand where certain types of files are stored.
Finally, my environment was restored and I was ready to start all over again. This time, before focusing on developing my coding skills or even creating my Python environment, I set about understanding how my computer actually works. I spent time getting used to the command-line interface (CLI) and the Terminal application. I learned about the “PATH” variable, what it is, what it does, and how to edit it myself, as well as learning about shell configuration files and how to amend them.
Only then did I research Python-specific environment topics, looking at options for managing and switching multiple Python versions, what virtual environments are and how to use them, how PIP works and what is best practice for managing Python packages and dependencies.
After what seemed like weeks, I felt like I had enough knowledge to build a Python environment that would work for me, creating an infrastructure that I understood, that is straightforward to operate within, and most importantly would allow me to focus on what I originally wanted to learn….Python Programming!
My New Python Environment
“Now it was time to create my chosen environment …below is a summary of my new Python environment setup”
This article is not intended as a tutorial on how to set this environment up, and I fully appreciate that others will have different and preferred environments tailored to suit their specific needs. I simply hope that by sharing my setup this may provide a starting point for someone for whom this topic is brand new….
- Computer : 2019 MacBook Pro 16"
- OS : MacOS Big Sur
- Terminal : iTerm2 (installed via Homebrew)
- Shell : zsh (with Oh-My-Zsh plug in)
- Python 2 Version : 2.7 System (retained and protected)
- Python 3 Versions : Multiple but all installed and managed via Pyenv which was installed via Homebrew (Global currently set to Python 3.9.1, allows for Global, Local and Shell specific version control)
- Python Virtual Environments : Managed via Pyenv-virtualenv installed via Homebrew (general virtual env created for training purposes into which any packages can get installed, specific individual project virtual envs created for accurate control over specific Python version, package and dependencies needed for each project).
- Python Package Manager : pipX installed via Homebrew, used to install global CLI packages/tools into a specific virtual environment that used globally (used for Autopep8, Black, Pylint, iPython)
- ZSH Shell Config file updated to enable auto activation and de-activation of virtual environments when moving in and out of project folders
- Editor/IDE : VS Code (installed from website) also configured to select correct Python Interpreter version when moving in and out of project folders and virtual environments
- Source Control : Git installed via Homebrew
The Take-Away
“When setting out to learn Python, be patient, get the environment right first, before you start coding”
If only I knew three months ago, what I know now! When you start out on your Python journey, you don’t know what you don’t know. It is a fair expectation that beginner courses and tutorials will help you create a future-proofed Python environment setup, but from my own personal experience, it appears that they don’t! Whether this is because the creators think it is too complicated a topic for a beginner to understand or whether this is because they want to retain your interest by getting you coding as quickly as possible, I don’t know. Either way, I believe this identifies a big gap in the Python learning experience that needs to be filled.
I really hope this article will prevent many newcomers to the world of Python from going through the same painful learning experience that I have endured.
I encourage you to invest a little time into your own research on the topics and tools I have mentioned throughout this article. Taking the opportunity to set up your environment correctly before you start on your Python journey will ensure you can remain fully focused on what you wanted to be doing all along…learning to code in Python!
I look forward to receiving all your comments and feedback on this article.
Over the coming weeks, as my experience and knowledge grows, I hope to publish some specific guides on how to install and configure the tools I have referenced in this article…please watch this space!