Did you know that the primary scripting language used in Kali Linux for pen testing and ethical hacking is Python?
Yeah, that’s right. Python makes Kali Linux one of the best choices for operating systems better suited for programming plus some steroids (pen testing, scripting, ethical hacking, etc).
Can I code Python in Kali Linux?
You can code Python programs in Kali Linux. In fact, Kali Linux is an excellent development environment for Python programming and deployment. Besides that, Python comes fully supported in Kali Linux, with additional pre-installed libraries and tools commonly used when developing Python applications.
When it comes to writing Python code in Kali Linux, the operating system supports popular development and debugging tools such as code editors and IDEs. Some of the IDEs that you can use on Kali Linux include:
- Pycharm, both community and professional editions.
- Pydev on Eclipse
- Jupiter Notebook
- Wing Python IDE
Popular code editors that you can use to write Python scripts in Kali Linux include:
- Visual Studio Code (VS Code)
- Sublime text
As you can see, you have a variety of choices of tools that you can use to write your Python code in Kali Linux.
Besides, what makes Kali Linux a better choice is that all the libraries offered in Python can be used both in the Linux terminal and in these development tools. In the end, it makes the work of the software engineer easier.
In general, writing, testing, and running Python code is easier and better in Kali Linux. You just have to open your favorite IDE or text editor and start writing your scripts.
Let’s see in detail every process involved in writing Python code in Kali Linux. From installing/updating Python binary to installing favorite IDE/code editor, to executing Python scripts, to compiling Python code.
How to write Python script in Kali Linux
The process of writing Python scripts in Kali Linux involves these steps:
Confirm you are using the latest Python 3
If you have Python installed on your system, ensure you are using the latest Python version 3 in your system.
Make sure you are not using Python version 2. For a guide on how to update Python in Kali Linux and ensure you are using Python 3, follow the same link above.
After completing the steps above, confirm that you have the latest Python version installed in your system by running the command:
The python -v Linux command is used to check whether Python is installed and what version is installed. It should display the version number if Python is installed.
Install your favorite IDE or code editor to write Python code
To be able to write Python code within your .py scripts, you should have an IDE, code editor, or a general text editor installed in Kali Linux. The good thing is that Kali Linux supports the majority of these tools.
To install Pycharm IDE, Visual Studio Code (VS Code), or Spyder in Kali Linux, follow this guide: How can beginners learn to code in Linux?
Create a .py script using a code editor or the Linux Terminal
Creating a .py Python script is easy when using Kali Linux. You have a choice of using the GUI window offered by the code editor or IDE or using the Linux Terminal.
To use a GUI window that most of the IDEs and editors will have, you just need to open the editor and on the File Menu or Top Bar, you will find actions for creating a new file.
To create a .py script using the Terminal, you just need to open the Terminal window and type the following:
Then press enter, which should create a new .py script file.
Write your Python code
After creating your .py file, you just need to open it using your favorite IDE or code editor to write its functionality.
In most editors, you will find the File > Open option at the top bar. You can use this to open your file.
To open a .py script file for editing using the Linux Terminal,
if you are using Visual Studio Code
if you are using the default text editor offered by Kali Linux
Execute your .py script using Python
After you are done writing your Python code, executing the script and seeing how it performs is very easy on Kali.
You just need to open a new Terminal window and use the
python3 command to execute the file.
If your file name is: “walk.py”, then you can execute the script by running the following command:
Note: When executing the following command, you must be navigated inside the directory that has the Python file using the cd command.
To learn about basic Python commands used when dealing with Python programming on Linux, here is a nice introduction article on that: How can beginners learn to code on Linux
If you prefer a course, here is a good one for you: The Practical Linux Guide for Beginners
Writing and executing Python code on Kali Linux is as easy as that.
How to compile Python code in a Linux terminal
One of the easiest ways to compile Python code in Kali Linux is to use the Terminal. Besides, it is very fast.
Here are the steps you need to take to compile your Python code in Kali Linux using the Linux Terminal.
Step 1: Open a new Terminal window
To open a new Kali Linux terminal window, you can
- Press the Winkey (the key with the Windows logo) to open the Apps list in Kali Linux. Locate the Terminal app by scrolling or searching it in the search bar. Press on the Terminal icon to open a new window
- Alternatively, you can press CTRL + ALT + T on your keyboard to open a new Terminal window.
Step 2: Navigate into the directory containing the Python script you want to compile
Once you have a new terminal window open, navigate into the directory that you have your Python code located using the
For example, if your Python file is located in the
Desktop folder, type
cd Desktop/ or cd ~/Desktop in the terminal.
Confirm that you are in the right directory by executing the
ls command to list the contents of the directory.
ls command should print out the contents of the directory where one of the files should be the one you wish to compile.
If you wish to become a Linux command line power user, you should definitely check this course
Looking to master the Linux command line interface?
The “Linux Command Line Basics” course is the ultimate solution to all your Linux woes. Designed for beginners, this course covers the essentials of Linux, from understanding the directory structure to managing files and directories and performing user management tasks.
With interactive video tutorials, hands-on exercises, and a supportive community of learners, our experts will guide you through this course.
By the end of it, you’ll be a pro at navigating the Linux command line interface and gain a deeper understanding of this powerful operating system.
Be sure to check it out.
Step 3: Compile your Python code using
python3 -m py_compile command
Once you are in the correct directory, compiling your Python script is very easy: Type the following command to compile your Python file:
python -m py_compile filename.py
As an example, let’s say I have a file inside the Desktop directory called “downloader.py” of which I want to compile, then here are the commands to follow:
cd ~/Desktop python3 -m py_compile downloader.py
Step 4: Locate your compiled code inside the __pycache__ directory
Once you have executed the
py_compile command, you should have your compiled Python program inside the
Navigate into the directory using the cd command:
List the contents of the directory and you should see your compiled Python program there.
Step 5: Confirm your compiled code is working correctly by executing it
Run the compiled file to ensure it is working correctly by running:
filename.pyc with the name of the compiled file that was created in the previous step.
For my example, I should run:
And it should work fine.
That’s it! Your Python code should now be compiled and ready to run in the Linux terminal.
Why do programmers prefer Linux?
Programmers often prefer using Linux operating systems, including Kali Linux, for several reasons that are specific to their needs. Some of these reasons, why programmers may prefer using Kali Linux, include:
- Customizability: One of the main advantages of using Kali Linux is that it is highly customizable. Programmers can tweak and configure the system to meet their specific requirements, and they can choose from a wide range of tools and packages that are available in the Linux ecosystem.
- Command-line interface: Linux operating systems are known for their powerful command-line interface, which allows programmers to execute complex tasks and automate repetitive tasks quickly and easily. This can help increase productivity and streamline the development process.
To learn more about the effectiveness of Kali Linux in software development, read this article: Why do programmers prefer to use Linux?
Do software engineers need to know Linux?
As an experienced web developer, I would say that it is highly beneficial for software engineers to have some knowledge of Linux. Understanding Linux’s basic concepts can help you become a more versatile and valuable software engineer.
Here are a few reasons why learning Linux can be helpful for software engineers:
- Many software tools are built for Linux: A lot of software development tools, like compilers and editors, are designed to work best on Linux. Familiarity with Linux can help you understand how to use these tools more efficiently.
- Linux is widely used in server environments: Linux is often the operating system of choice for servers and cloud computing platforms. Understanding how Linux servers work can be a valuable skill for software engineers who need to deploy their applications to these environments.
- Shell scripting: The command-line interface in Linux, known as the shell, is a powerful tool for automating tasks and scripting repetitive actions. Understanding how to write shell scripts can help software engineers automate testing, deployments, and other tasks.
That’s it for this article and if you wish to learn Python or Kali Linux or both at the same time, here are one of the best courses that I would recommend
To a beginner
Python is a beginner-friendly Python programming language. However, it can take you a very long time to master the language if you do not have the right learning approaches.
Because you are smart- you should always take the shortest route; like I did with learning through the right course.
With this course, I guarantee you will be a master Python programmer who can take any coding/job challenge like me.
The only thing that it takes is to click on the link (Affiliate link) and make the right decision to invest in a top-quality Python course today!
To a more experienced Python programmer
Looking to master the Linux command line interface?
This course is a great opportunity for anyone looking to improve their command line skills in Linux. It covers a vast range of topics, from basic navigation and file manipulation to more advanced topics like file permissions, shell customization, and Bash scripting.
The course is led by an experienced instructor who has taught thousands of students, including me, and has received high ratings and reviews.
The course covers dozens of powerful commands, including find, locate, grep, and more. It teaches you how to string together multiple commands to build complex data pipelines, and how to master command-line navigation and the Linux folder structure.
You’ll also learn how to create, delete, move, copy, and rename files and folders, and how to decipher and manipulate Unix file permissions.
The Linux Command Line Bootcamp is a comprehensive course that will turn you into a command line power user. Whether you’re a Python programmer, web developer, data scientist, machine learning engineer, or just a plain old computer user, the skills you learn in this course will make you feel powerful and give you full control of your machine.
Be sure to check it out.