Friday, November 22, 2019

[Python] Install matplotlib and basemap in Python3.7

I need to install a new matplotlib and basemap for my new anaconda python 3.7. I still remember the pain many years ago when I first installed these two guys using source...check my old blog!

One thing requires attention is that basemap is not working with the newest version proj4.6.X. You will get errors like (this thread):
epsgf = open(os.path.join(pyproj_datadir,'epsg'))
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/miniconda3/envs/test/share/proj/epsg'
So need to specify the proj4 version!

$ conda create -n test python=3.7 pandas pillow numpy
$ conda activate test
$ conda install -c conda-forge matplotlib basemap proj4=5.0
$ python -c "from mpl_toolkits.basemap import Basemap"

After this, I have an issue about the QT backend support between matplotlib and anaconda, such as "Could not open XDG", "Could not connect to display" (see this post). So I add the following in ~/.bashrc:

export QT_QPA_PLATFORM='offscreen'

And it worked for me!

Friday, November 15, 2019

[Python] Removing hard-coded number with argparse

Assume you have a function as below in a python script calculate_sum.py,

def summing(a, b):
    return a+b
summing(2,3)

Now if you want to change 2 and 3 to other numbers, you can use a powerful tool argparse instead of modifying these hard-coded numbers in the script, which can be complicated in a set of complicated scripts. 

import argparse

parser = argparse.ArgumentParser(description='calculate the sum of two numbers')

# Either use positional arguments. The position matters, so a and b can not reverse in order.
parser.add_argument('a', type=int)
parser.add_argument('b', type=int)

# Or use optional arguments. The position of a and b does not matter.
parser.add_argument('--a', type=int)
parser.add_argument('--b', type=int)

# Pass the arguments to the parser python module
args = parser.parse_args()

def summing(a, b):
    return a+b
summing(args.a,args.b)

If you use positional arguments, your command should be:
$python calculate_sum.py 4 3
7

Otherwise, your command should be:
$python calculate_sum.py --a 4 --b 3
7


Monday, November 4, 2019

[Python] make python script executable

Once you wrote a python script run.py, you can run it in the terminal as below:

$ python run.py

However, you can add #!/usr/bin/env python at the top of your run.py, so that the run.py becomes executable, and can be called without the preceding language "python".

$./run.py

Please also see why using #!/usr/bin/env python but not #!/usr/bin/python at this post.

[WSL] Set up Visual Studio Code and Pycharm editors in Windows Subsystem for Linux

Last week, I've walked through the initialization of Linux subsystem in Windows (read my blog here). This week, I can't wait to share my experience about the code editors on Windows 10.

Here is some background. I mainly work with Linux and use Python and Javascript. The first question I have is whether I need to install two Python distributions for Windows and WSL. I felt that it may trigger a problem because (1) the compiling of Python in different operating systems must be different, and (2) the slashes in the two file systems are opposite. I did a quick test by launching the anaconda python.exe from the WSL linux terminal (in my case, Ubuntu). Surprisingly, I am able to open Windows Python.exe in WSL! However, it is not much meaningful because in WSL I am not able to invoke the virtual environment and all the packages installed in Windows. So the short answer to this question is that you should install separate Python distribution in Windows and WSL, in order to manage your packages and avoid conflicts. Ideally, one should use virtual environment for each Python project, no matter in Windows or in WSL.

I use two major editors, Visual Studio Code and Pycharm. I use VSC for most languages, such as Python, Javascript, Markdown, Dockerfile, R, etc. I use Pycharm when I focus myself on one single Python project. These two editors are fantastic from their interface designs to the elaborative documentation and online support.


Surprisingly, both editors support dual-system launching! By that I mean, after you install the software in Windows, you not only can open it from Windows (e.g., start menu), but also are able to launch it from the WSL linux terminal (in my case, Ubuntu). I've never seen this dual-system functionality before in Linux nor OSX, which is unbelievably amazing and smart! This feature is really important because the running/debugging environment is entirely different in Windows and WSL. With that, you can now run Python from VSC internally (with your WSL bashrc file launched).

To install and launch Pycharm in both Windows and WSL systems, please follow this guide: python-development-on-the-windows-subsystem-for-linux-wsl.

To install and launch Visual Studio Code in Windows, please follow this official guide: windows. To further launch VSC in WSL, please follow this official guide: run-in-wsl. At the bottom left of the VSC window, there is a green panel displaying which system you are now invoking VSC.

Friday, November 1, 2019

[Windows] How to set up conda path in PowerShell

In windows, I installed anaconda package from the official website, and you can open python or other conda softwares (jupyter-notebook for example) using the Anaconda Prompt.

But what if you already use PowerShell and don't want to open an extra Anaconda Prompt window? Or what if you are using visual studio to edit python scripts and run python within it?

The first time you type in "conda" in PowerShell, you may see the following error:

conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is     
correct and try again.

The solution is to go to the anaconda directory (either PowerShell, command line, or WSL, it doesn't matter), and simply type "./conda.exe init".