Common issues when learning Python

Learning something new is fun.. but let’s face it, there are times when it can be challenging and the easiest thing is to give up. I’ve been teaching Python for a couple of years, and it’s given me an opportunity to see what challenges folks have when learning Python.

 

My focus has been teaching for us for use in financial markets and also more recently also teaching a combined Python and alternative data course, partially based on The Book of Alternative Data, which Alexander Denev and I wrote. I’ve taught a wide range of folks, ranging from students, at Queen Mary University of London to practitioners in funds and banks (and if you’d like me to come and teach one of my workshops at your organisation let me know too!)

 

Of course learning to code in Python, or indeed any other language, is a challenge particularly if you’ve never coded before. It’s going to take a few months to get up to speed for a total beginner. If you’ve coded before, it’s somewhat easier and quicker, but still there might be specific things about Python which seem a bit different (like tabs instead of curly brackets to denote blocks of code). I’ve tried to list a few of the different problems or questions which have cropped up when I’ve taught folks Python.

 

I can’t install Python, and I get error messages saying “No module Pandas installed”
Perhaps unsurprisingly, the first barrier which people face is trying to install Python. It can be confusing given there are several Python distributions out there. In general, I tend to recommend that folks install the Anaconda distribution of Python. Yes, it is a big download, and it can be a bit heavy, but I’ve found it the best distribution to use given my focus is on data science problems. I’ve also put together a py37class conda environment which can be installed quickly, which has:

 

  • standard data science libraries, like Pandas, NumPy etc.
  • machine learning libraries and stuff for NLP/computer vision etc.
  • Cuemacro’s financial libraries (chartpy, findatapy & finmarkpy)

 

I’ve put together full instructions on my GitHub teaching page, for installing Anaconda, my conda py37class environment (YML file) plus a number of other useful applications like Git (for version control) and PyCharm.

 

Sometimes, people have issues with firewalls, particularly in a corporate environment, which can block the conda from installing libraries, making it challenging to use my py37class environment. In that case, one option is to install libraries as needed, using pip, if you get error messages like “No module Pandas installed”. Indeed, it’s worth noting, that we can use pip (or indeed any command line), in a Jupyter notebook, by typing ! before the command, eg. to install Pandas from a Jupyter notebook.

 

In particular if you have no permissions to install Python locally and you can’t get your IT team to install it, and you want a way of playing around with Python, I usually suggest trying to write and run Python in Google Colab, which gives you an online Jupyter notebook and you can use pip to install additional libraries (when the Jupyter server is idle, it will close, which might result in you having to redo all your pip installs). With a Google Account you can save all your Jupyter notebooks online too. Another alternative is Repl.it, which has an online coding environment for Python and many other languages too, given that sometimes Google Account logins might be blocked from corporate machines.

 

Where can I write Python code?
There are many ways to write and run Python code. First and foremost, you can type it directly into the interpreter. This is probably the easiest option! However, I’d say in general for learning, it’s better to use a Jupyter notebook or indeed if you’re doing research work. It allows you to intermingle explanations with your code. With Jupyter notebooks which when you reopen them, you lose anything in memory (including any library imports), and I often get questions asking me about this point.

 

It’s also worth trying to using PyCharm as an IDE. Using an IDE can be beneficial, when you’re trying to write reusable scripts and in particular, when putting together coding libraries. You also get lots of other nice features, like integration into GitHub, the code profiler, autocomplete etc. You can run any Python script using the command line too.

 

However, whilst I tend to use PyCharm, it’s not the only IDE, and everyone has their own favourite IDE (or to simply using vim)! Other ones worth looking at including VSCode and PyDev (particularly if you have a Java background and use Eclipse).

 

How can folks who don’t know Python run my scripts?
If you’re developing a code library, your client are other developers. They don’t necessarily need to understand all the code you’ve written but you need to write documentation, so they can call the code. In many other cases, your end user might not be a developer. You don’t really want to force them to use a Jupyter notebook to run your code, or do stuff in the command line, us PyCharm etc.

 

There are several ways to expose your Python code. One is as a website, for example using Plotly’s Dash library to create dashboards. The advantage of a web based/intranet solution, is you don’t need to deploy Python (or a particular conda environment) to your users’ machines. Another way is using xlwings so Excel becomes a Python front end. If the code doesn’t need to be interactive (eg. for the creation of a daily report which is created every day for uses), you can run a Python script as cron job on Linux, or using Task Scheduler on Windows.

 

If you want to distribute your Python code for folks to run it, Docker can often be a good solution, if it has lots of dependencies, like databases, web servers etc. so each of these can run in their own sandbox.

 

Why do we need to bother learning about classes in Python?
Python is simpler to start than other coding languages. Strictly speaking you don’t need to use the object oriented features of Python at all when you code. However, it is worth understanding object oriented programming more broadly, because obvious it’s a feature of many languages, not just Python. I know not everyone is a fan, but it does tend to make it easier to make your code more reusable and you can also use things like design patterns too.

 

How long does it take to learn Python?
This will vary and depends on your coding experience. If you’re a complete beginner I would say maybe a few months to get up to speed and needs you to spend a lot of time coding yourself. If you’ve already coded, a lot of the concepts will be simpler, although the syntax will be slightly different. It will take longer to get to know many of the libraries in detail (and there are always new libraries to learn too). I’ve been coding in Python for a few years, and still I learn new stuff all the time about new libraries, functionality etc. as I do more client projects and explore new topics. 

 

Conclusion
Python is easier than many other programming languages, but it still takes a bit of time to get up to speed in it if you’re a beginner. Whilst it is easier, it is still a challenge to write good maintainable Python code (as opposed to something that just works). The above issues and question are of course not comprehensive, hopefully, knowing these will answers some queries you might have when learning Python to start.