Furthermore, having libraries like NumPy, OpenCV, scikit-learn makes it perfect to be used for projects of various Computer Science fields like machine learning and data science. For a language that keeps on rapidly growing, it is thus imperative for developers to use editors that are fully able to grasp the intricacies behind it.
One such text editor is Emacs, which, being open-source and cross-platform along with a highly customizable and user-friendly interface, offers some scintillating features to its users such as multiple editing modes, text manipulation tools and integration with external tools. Since it is extremely customizable, it can easily be configured to be used as a Python IDE. Hence today, we will be looking at how one can configure Emacs for Python Development and turn it into a Python IDE.
Before moving onto configuring Emacs, it is first important to check whether your system meets the requirements for the upcoming process. First of all, you need to make sure if pip is installed on your system. To do this, enter the following command into the terminal:
For systems not having pip installed can do so by using the following command:
Note: the above commands are for Python 3 only.
Next, we have to install some dependencies that help in configuring Emacs and provide functionalities such as auto-completion and syntax checking. It can be done by the following command:
We are done with the prerequisites part. Now we can move onto turning Emacs into a Python IDE.
Configuring the Initialization File (Recommended)
When Emacs starts, the first file that is processed is the initialization file (init file), which contains commands written in Lisp that allow users to configure Emacs. So, we need to add some commands to this file for Emacs to be used for Python Development. Firstly, open Emacs and hit Ctrl + X, followed by Ctrl + F and then enter ~/.emacs to open the init file.
Next, add the following lines of code to your init file:
'("melpa" . "http://melpa.org/packages/") t)
What this code does is that it adds the Melpa archive to the list of package repositories and then gives permission to Emacs to use these packages.
Configuring the Initialization File (Optional)
The next few lines of code are optional for the user to add to the init file as they are mainly used to install some packages that are not necessary for Python Development. These packages make Emacs more user-friendly and give it a darker vibrant theme.
(mapc #'(lambda (package)
(unless (package-installed-p package)
(setq inhibit-startup-message t)
(load-theme 'material t)
The Basic Customization part in the above code disables the initial Emacs startup message, loads the darker theme we installed above, and adds line numbers to Emacs. After adding all the lines of code, simply hit Ctrl + X followed by Ctrl + S to save your file. After closing and restarting Emacs, the changes will start to appear. See Image below:
Although Emacs has a built-in Python mode, it is minimal and does not provide features like auto-completion, syntax checking, automatic indentation, etc. Hence, we will be using the elpy package, or Emacs Lisp Python Environment, which provides a variety of efficient Python IDE features to users. For installation of elpy, in the above line of code where we installed the better defaults and material theme packages, replace that section with the following lines:
Note the above is for when you add the optional content to your init file. In case you are not using the optional content, then simply the following lines of code will be added below the recommended section:
Next, in both scenarios, add the following lines to the end of your initialization file to enable the elpy package as well as the ipython shell:
(setq python-shell-interpreter "ipython"
python-shell-interpreter-args "-i --simple-prompt")
Now users will be able to create and edit Python files in Emacs using the features present in a Python IDE.
For users wanting to test and run their programs, open your python file and then click on Ctrl + C followed by Ctrl + C. For example, in the image below, we are running a simple calculator that asks the user what option he or she wants to choose from the given choices. Then the two numbers are inputted, and the results are shown.
In addition to elpy, you can add multiple other packages to Emacs, providing it with even more features. A particular example would be integration with the Jupyter Notebook.
Why use Emacs for Python?
Emacs is one of the most powerful text editors out there, being highly customizable and flexible in nature. This allows users to set up the editor according to their needs and preferences and thus enabling them to work more efficiently. With Python being used almost everywhere today, it is therefore imperative to have an editor that can offer multiple tools and features that bring out the best of the language. Emacs is undoubtedly a must editor to have for Python Development.