20+ Must-Know Python Commands You Should Know

Aug 4, 2022
Python Commands

A 2021 Stack Overflow study shows Python as the preferred programming language that most designers want to create using the most.

Python is the most popular language with 19.04%, Typescript follows with 15.29%, and finishing the top 3, JS with 14.59%.
Survey of Stack Overflow.

Through the use of programs such as Jupyter Notebook, Google Colaboratory and online compilers, you'll be able to start using this programming language, without having to be concerned about installing any software.

In this tutorial will teach you the most essential tools that are useful for Python development.

What is the Command-Line Interface?

Command-line interface, often abbreviated CLI -- is a type of application that allows you to execute software and execute tasks associated with operating system (OS) OS (OS) including creating and managing documents.

A CLI can execute different shells. Below is a screenshot of two CLIs using the Python shell, the other uses Bash:

Python and Bash shells opened in two different CLIs showing off the output of the print and neofetch commands.
Python and Bash shells.

Both concepts can be misunderstood, and this is the way to break them down:

  • CLI is a text-based interface which lets you write commands and then receive the output of those commands. It can execute different commands.
  • A shell is an interpreter for commands that is equipped to communicate with the operating system.

Believe it or not, the application you're running is run through commands. Your window ( GUI) that you use daily are comprised of commands which are activated through bindings, which enable users to communicate with their operating system.

Do you need the CLI to Python Development?

Actually, Python has loads of libraries for building CLI apps like Typer, Argsparse, and Click. You have the option of becoming just a CLI user to becoming an author of your own CLI applications! This can be a great illustration of the strong link between CLI as well as Python.

The CLI The CLI: Introduction

Based on the OS that you're running There are differences depending on the operating system you're using. The way you interact with the command line. Each OS has an individual method of opening and working with the CLI because of the different files organization and commands.

Let's take a look at three the operating systems that are the most frequently used by programmer: Windows, Mac, and Linux.

Windows

Keep in mind that Windows uses the letters for paths rather than /. Be aware of this as you navigate through directories.

Also on Windows you have the option to install Git Bash, an command line which emulates how the Bash shell on Windows. This makes the vast majority of Unix commands that are listed below compatible with your system.

Mac

In the case of Mac the commands are accessible via an integrated application known in the form of "Terminal". The application can be searched using Launchpad and can be found within the "Utilities" folder, which is located under "applications".

Linux

In Linux There are plenty of options depending on the operating system you're using. But, the procedure "Ctrl + Alt + T" typically is used to activate the default terminal for the system.

After that, you'll be able to be able to see a window similar to that below:

 Manjaro Linux CLI showing system status.
CLI.

20+ of the most Used CLI Commands to Python Development

After you've successfully got your CLI up and running, you're now ready to investigate the most well-known options for command line that can help you become a Python developer significantly simpler.

Installation Commands

It's likely that you've come across a variety of methods for installing Python. But sometimes, it's much simple to install it with just a command.

These are some of the options that will help you install Python across various operating systems.

1. Chocolatey

If you're running Windows, you have no standard package manager. A solution to this problem is Chocolatey which provides users with the ability to install programs right from the command line, obviously this includes Python.

It is important to install Chocolatey before performing the following commands:

choco install python --pre 

2. Pyenv and Homebrew

macOS includes Python 2.7 as a default. However, Python 2.7 has been removed from the Mac OS. The whole community has shifted towards Python 3. To manage your Python versions in a way which is effective, it's possible to use an application like Python.

Start a command-line and download the most up-to-date version of Homebrew (a package manager that is similar to Chocolatey) with the following steps:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After that, you are able to set up pyenv using this instruction:

brew install Python

It is also possible to install a particular Python version and then set it as the default Python executable in place in place of Python 2.

Python installation 3.10 The version you're looking to install. Python globally 3.10.4 # Sets the version as default

If you are running Python, it will be the Python version that you have configured using pyenv.

Python# Python 3.10.4 .... # >>

3. 3. and dnf

In the Debian-based distributions (Ubuntu, Linux Mint, Kali Linux) You'll utilize apt. It's a short form for "advanced tools to develop packages":

sudo apt update sudo apt install python3

If you would like to make Python 3 as your preferred Python interpreter, use the following command

sudo apt install python-is-python3

For distros based on Arch You can make use of the official package manager, pacman:

sudo pacman -S python

For Fedora along with RPM-based Linux distributions (Red Hat, CentOS), you employ the dnf

sudo dnf install python3

Interpreter Commands

Review the key flags -- command line options -for the Python command, and also its packaging manager the pip.

4. Python

The Python command is accompanied by a variety of flags. They are choices that change the manner in which you execute code.

To run the Python file, just invoke the interpreter, and then add to the address of the file, including it's .py extension:

python helloworld.py

If you're struggling to remember the significance of flags then use the assistance flag to any of these 3 presentations:

Python - -? Python --H Python

To print (see) which version of Python you're running You can do this by using the following:

Python --V Python version

If you'd like to execute Python code, without opening or editing the .py file, you can run it directly through your terminal by using the command flag

# Hello, World! Python # Hello, World! Python "print('Hello, World! ')"

The flag the m flag runs in a Python module that uses script. This is extremely useful when you wish to make an virtual space using the built-in the venv module.

Python with m.Venv .venv

5. pip

The pip command searches for the packages in the Python Package Index (PyPI) Additionally, it resolves dependencies and installs the program you've picked.

In order to install a Python package, just type pip along with your name. Then, enter the package you'd want to install.

This option installs the most recent version of the software:

pip install django

If you're looking for a specific version, run this command:

# pip install package==version pip install django==4.0.4

In collaboration projects, it is essential to keep track of dependencies using your requirements files. By using the flag the r flag the user can access and install programs in a text-based file

pip install with -r requirements.txt

A different feature that is commonly utilized is the "freeze" flag. This flag will generate an inventory of all the packages versions that you've put on your PC. Additionally, it can record your dependencies into an requirements file

pip freeze >> requirements.txt

Permission Commands

Python is an excellent program for writing scripts as well as managing files. For these functions, you need to have an understanding of how permissions are set up within your OS.

6. sudo, runas

For Unix-based systems (macOS, Linux, BSD) It is necessary to be granted superuser privileges to perform particular tasks such as installing software similar to what you have done in the earlier example.

The sudo command lets users to temporarily gain administrator rights to execute any of these commands.

Here is an example of installing Ueberzug (an images review Python program) all over the world:

sudo pip install ueberzug

The Windows version of Runas is Runas, which performs scripts under another user, or administrator

runas /noprofile /user:Administrator cmd

There are also other projects such as Gsudo Gsudo, that make the procedure of granting permissions significantly simpler than using other integrated Windows commands.

Installation of gsudo choco install gsudo : The program reads text files such as MySecretFile.txt Gsudo input MySecretFile.txt

7. Chmod

chmod can be used to modify the permissions of files and directories in Unix.

A common usage is making an Python script executable.

# Creates mypythonscript.py executablechmod +x mypythonscript.py

After you've made an executable script , you'll be able to run it directly by using the .Note Note: Please Note:

# Executes the script./mypythonscript.py

Utilizing the file system through commands-line-interface is an everyday task for Python programmers. The following are the main actions you can use for controlling your system when developing with Python.

8. Ls Dir

To list the contents of an entire directory (folder) to list the contents of a directory it is necessary to execute the ls (Unix) or dir (Windows) command. This was likely the very first command you were taught the first time you encountered the CLI.

Here's the syntax used:

Ls # Shows the directory's contents that is used for the operation of mydirectory by using ls.

This is an illustration of the contents of the contents of a local file system:

Ls test_python# classes_error.py radius.py test-Django

This command is an amazing instrument with a wide range of choices. It can be used with the command ls -al to display hidden files (those that have an asterisk at the beginning) and also the dimensions of the document, their mode and date of each file:

alias ls="ls -al" # Results total 20 drwx------ 3 daniel daniel 4096 ene 16 19:13 . drwxr-xr-x 36 daniel daniel 4096 may 17 22:18 .. -rw------- 1 daniel daniel 32 nov 17 2020 classes_error.py -rw------- 1 daniel daniel 327 nov 10 2020 radius.py drwx------ 4 daniel daniel 4096 ene 16 01:07 test-Django

For Windows it is possible to utilize ls using Git Bash, or utilize the integrated director commands:

dir

9. Pwd

PWD is a reference for "print working directory" and does precisely it: provides you with all information about the directory you're in:

pwd # /home/daniel/github/HTML-site/images

If you've ever confused on the PC, this feature is invaluable.

You can achieve the same output in Windows by using the command the cd command without parameter (note this command used for Unix will take you to the Home directory):

# Only available on Windows Cd # D:FoldersubFolder

10. Cp

Transferring files using a visual file manager is intuitive however, it is not efficient. With this method, you are able to copy every data file on your system.

cp old_file.txt copy_old_file.txt

To copy all the contents of the directory, utilize Cp .:

cp -r originaldirectory/ newdir

The equivalent word for Cp within Windows is the following copy:

Copies old_file.txt copy_old_file.txt and a

11. Cat Type

Print the contents of text files in the terminal, with no opening the file within an editor, you can use the keyboard to write cats, more than or lesser or less Unix or typing to type on Windows:

cat old_file.txt # Unix type old_file.txt # Windows # Content Hey there! I hope you're enjoying my artwork ... I hope you enjoy itas in the same way as I have enjoyed making it! The end of the example.

12. Mv, move

The command mv. This command is employed to transfer directories and files from one place to the next (basically it's a copy-and-paste. Also, it changes the name of the file in case it's not located in the directory:

# Rename files mv source_file.txt renamed_file.txt
# Copy file to another directorymove renamed_file.txt thenewdir/

Additionally, you can use pattern matching to facilitate file transfer. In this case you could shift the entire .py files to another directory:

M *.py mypythondir/

Another similar command available for Windows could be to move it. It has the exact same functionality as the above:

# Windows move source_file.txt renamed_file.txt

13. rm, del

It's possible to utilize the Rm command to delete directories and files.

To delete a file that doesn't not an entire folder you'd need to use:

Rm file_to_remove.txt

If you'd like to erase any empty directories then you should use the"recursive" ( -r) flag:

rm -r dir_to_remove/

To remove a directory with contents inside, employ the force ( -f) and the recursive flags

Rm with contentThen /

As with the preceding it is possible to find the command del within Windows. Take extra care since the command does not come with the flags to prevent it from happening.

del \mywindowsdir

14. 14.

After you've completed the Python program, you'll be able to quit the shell session. In most cases you'll also close down the program that you're currently running within:

exit

This command is available equally on Windows and Unix.

Command-Line Editors

Once you've mastered the command line, it will be slower to change windows or even use your mouse to edit text.

Being able to type code even while on the command line , is not only a great option to cut down on time, it also helps you appear as a king among your colleagues!

These are the most affluent editors for command-line use.

15. Vim/Neovim

Vim and its variant, Neovim, are keyboard-based text editors which are mainly used within the Command Line. Based on a 2021 Survey conducted by the Stack Overflow which ranks them 4th in the list of the top users of editors, according to the developers.

Survey results show Neovim as the most loved editor and Vim in the 4th position.
Favorite code editors.

You can now enjoy the capabilities of Vim only by typing the name of Vim in your command line

vim

The interface will be which is text-based and has different keyboards for each step you'll need to perform when writing code using Python.

Vim is a program with a long learning curve. However, once you've learned the software that you will never go back to a different program.

Vi IMproved.

16. Nano

Nano is a command-line text editor, which is typically used for editing quick changes.

If you've made an error in your syntax code, however you're not able to launch your editor in order to fix the problem. Nano aids you in fixing the issue directly on your command line:

nano

17. Emacs

Emacs is one of the most flexible and custom text editors that you will discover. There's an entire section dedicated to Python programming that includes a plethora of extras to enhance your development experience.

Emacs is available in almost every operating system. if you're still having trouble installing, you can follow the instructions for downloading.

To launch Emacs using the command line option, make use of the no-window system flag ( -nw):

Emacs --nw

Development Tools

Python development involves not just programming, but also the use of other tools, including virtual environments versions control systems and deployment tools.

18. virtualenv/venv

Virtual environments are a vital tool used in Python development. They permit you to organize applications that use the same software within different projects into a lightweight folder, most often referred to as .venv.

If you're using Python 3.3 or higher, you're able to use the built-in venv module, which can create an environment virtual:

# .venv is the name given to the Python virtual environment -m"venv .venv

virtualenv is an external project that's faster and more extensible as compared to the built-in feature. In order to create a virtual world, you need to first install the software virtualenv:

# Installs the virtualenv pip --user virtualenv # Creates the .venv virtual environmentvirtualenv .venv

Next, you'll need to activate the virtual environment. If you're using Windows, run one of these command depending upon whether or not you're using cmd or PowerShell (recommended):

:: PowerShell .venv\Scripts\Activate.ps1
:: Cmd .venv\Scripts\activate.bat

In Linux as well as macOS:

the source .venv/bin/activate

19. Git

Once you've set it up, open a terminal and have a first glimpse of the options available with this command:

Help git

To make a repository make use of git init and then type in the name of the repository:

git init name_repository Initialized empty Git repository in /home/daniel/name_repository/.git/

git clone [email protected]:DaniDiazTech/HTML-site.git ... Cloning into 'HTML-site'... remote: Enumerating objects: 24, done. remote counting objects 100% (24/24) completed. remote: Compressing objects: 100% (18/18) accomplished. remote Total (delta 6) Reused 21 (delta 4). (delta 6.) Reused 21 (delta 4) Pack-reused zeroReceiving object 100 percent (24/24), 4.98 MiB | 438.00 KiB/s, done. Resolving deltas: 100% (6/6), done.

20. Docker

Docker allows you to package and deliver your Python applications in light and portable containers that are self-sufficient. It assists in both development and deployment, making it possible for each collaborator to utilize the same settings.

To use Docker You must be sure to follow the steps as outlined for your particular operating system from the Start Docker page.

To display Docker commands available to you, follow these steps:

docker help 

It's difficult to describe the steps to use Docker compose in this narrow section. Make sure you read the official instructions.

21. Grep

Grep is an indispensable command-line application that is used for pattern matching within plain text documents.

It is commonly used to find the frequency of repetitions in a file:

grp Python pythondocument.txt 2.

In the previous example it is possible to see the total number of instances Python (case insensitive) is available in the pythondocument.txt file.

The Windows equivalent of grep can be described as findstr. But it's not exactly the identical to grep. There's a Git bash that runs the command grep in Windows:

isr/i /C Python pythondocument.txt 2

22. HTTPie

HTTPie is a command-line HTTP client, which allows users to communicate easily via web-based applications. It can be used for instance, to check your Python APIs or interact with websites of third parties.

This CLI tool is accessible within almost every package manager according to the official documentation of HTTPie. However, it is also available as an Python package. You could install it using pip.

pip installation httpie

This is how to query an API from outside - In this instance, GitHub API:

http GET https://api.github.com/users/danidiaztech HTTP/1.1 200 OK Accept-Ranges: bytes Access-Control-Allow-Origin: * ...

23. ping

This command is typically used to verify that two computers are connected such as your own machine, as well as the Python application that is running on a Web server:

ping .com PING .com(2606:4700:4400::ac40:917d (2606:4700:4400::ac40:917d)) 56 data bytes 64 bytes from 2606:4700:4400::ac40:917d (2606:4700:4400::ac40:917d): icmp_seq=1 ttl=50 time=686 ms

Command Reference Table

Below, we'll give you a quick guide to the commands we've discussed:

Command Use
choco Installs package on Windows
HTML1 brew macOS package manager
APT, pacman of dnf Package manager on different Linux distros
python Runs Python interpreter
pip Python package manager
sudo, runas Unix and Windows applications used to extend permissions
CHMO Changes file permissions
Ls The directory's contents are listed. Directory
password Prints the directories of operation
cp Directories, files and copies
cat cat Contents of the print file
Mv Move Modifications (renames) files and directories
rm, del Removing directories and files
leave ends the current shell session
vim, neovim Text editing that is efficient
nano Text editor for quick edits
Emacs The editor with the most flexibility
virtualenv, venv Virtual environment generators
Git System for controlling version
docker Containerize apps
HTML1 Grep Application for pattern matching
http Web Service Testing Utility
Ping Checks for connectivity to the network
kill Terminates programs

Summary

What other command are you using for Python development? Comment them below!

Reduce time, money and improve site performance by:

  • Help is available immediately from WordPress experts on hosting 24/7.
  • Cloudflare Enterprise integration.
  • The global reach of the audience is enhanced by 34 data centers all over the world.
  • Optimization using the built-in Application Performance Monitor.

This post was posted on here