Martin McBride, 2021-07-06
Tags python anaconda pycharm
Categories software development

Using Anaconda and Pycharm to choose a Python version

Python is great, made better by the extensive infrastructure provided by open-source modules.

What isn't always so great is installing all the modules you need, and making sure they are all mutually compatible with your version of Python and with each other.

With many modules, you might need a different version depending on the version of Python you are using, and maybe also on your operating system (Windows, Mac, and different flavours of Linux might each require a different version of some modules). This is made worse by the fact that some modules might not be available yet for the latest version of Python.

Added to that, certain modules are difficult to install even when you do have the right versions. Modules that include C++ code need to either be compiled, which can be hit and miss, or you have to find precompiled versions which are often located on obscure websites. I have had particular problems installing NumPy on Windows (I ended up with a wheel file I found on a well known but worryingly obscure personal website), and wxPython seems to be difficult on most systems. Both are fine and useful modules, just a PITA to install.

The solution

The solution is obvious, of course - a package manager. But it is easy to get into the habit of sticking with what works, for as long as possible. In my case, I have stuck with Python 3.6 for quite a while. I have all the modules I need, but some of them involved a lot of messing around to get working. All working up till now...

Currently, I am working on a GUI project using wxPython. It is a graphical front end for CSound, but partly just an excuse to try developing a GUI app in Python. All was going great using my trusty Python 3.6 until one particular call gave an assertion error from the guts of the wxwidget's native code. A bit of googling indicated that Python 3.6 wasn't entirely compatible with the current wxPython, and downgrading to an earlier wxPython seemed like a retrograde step. I tried it anyway, like you do, but my heart wasn't in it so I soon gave up when it didn't seem to be working.

Time for a proper fix. I've previously dabbled with Anaconda, a package manager that has an emphasis on data science but is generally pretty good all round. Finally, it was time to get serious about it.

Installing Anaconda

I was developing on my Linux machine, which I tend to prefer for Python and Git, so the first job was to download the relevant installer from anaconda.com. For Linux, this takes the form of a bash script.

Having downloaded the script, installation was just a matter of running the script from the command line, in my home folder:

bash Downloads/Anaconda3-2021.05-Linux-x86_64.sh

Your file name may well be different, of course.

Installing wxPython

Installing wxPython with conda is a breeze:

conda install -c anaconda wxpython

At least, it would have been, but unfortunately Anaconda had installed Python 3.9 and it appears that wxPython hasn't been ported yet (or at least, it hasn't made it to Anaconda). No matter, the latest supported version, Python 3.7, will do fine. You can set up to use Python 3.7 like this:

conda create --name py37 python=3.7

This creates a virtual environment called py37 (you can call it anything you like), using Python 3.7.

You can switch to this environment using the following command:

conda activate py37

At this point, the default python interpreter is 3.7, and anything you install will go to that environment. I repeated the previous command to install wxpython, and it worked fine.

If you did ever need to use the 3.9 environment, it is still there, called base, and you can switch to it using:

conda deactivate

Making PyCharm use the new environment

With py37 (or whichever Python environment you want) active, open/create your Pycharm Python project.

  • In the PyCharm settings, go to the Project > Python interpreter page.
  • In the dropdown menu, select py37 in the Python interpreter dropdown, if it is present.
  • If the option isn't present (which it probably won't be the first time you use it):
    • Click the cog icon next to the Python interpreter dropdown.
    • Select Add.. in the menu that appears. The Add Python interpreter dialog will appear.
    • Select Conda environment and tick the Existing environment radio button.
    • Select py37 in the Interpreter dropdown, then OK to close the Add Python interpreter dialog.
  • Whichever method you used, the Project > Python interpreter page should show the correct interpreter and will list all the installed packages.

All works well, the obscure bug is fixed, and I never need to worry about upgrading Python again!


Join me on substack to keep up to date with this blog