{"id":57750,"date":"2023-12-29T08:04:40","date_gmt":"2023-12-29T08:04:40","guid":{"rendered":"https:\/\/www.askpython.com\/?p=57750"},"modified":"2025-04-10T20:48:53","modified_gmt":"2025-04-10T20:48:53","slug":"python3-alias-as-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/python3-alias-as-python","title":{"rendered":"How to Create Python3 Aliases in Windows and Ubuntu\/Linux"},"content":{"rendered":"\n<p>Alias is a custom shorthand or nickname assigned to a longer command or sequence of commands. This allows users to create shortcuts for commonly used commands. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Creating a Python3 alias involves simple steps in both Windows and Ubuntu\/Linux. In Windows, it requires a batch file named &#8216;python.bat&#8217; with specific commands, whereas in Ubuntu\/Linux, you add &#8216;alias python=python3&#8217; to your shell configuration file, like ~\/.bashrc, and reload it.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Up Python3 Aliases on Windows and Ubuntu\/Linux<\/h2>\n\n\n\n<p>So now we are creating an alias (nickname) &#8216;Python&#8217; for &#8216;Python3&#8217;<\/p>\n\n\n\n<p><em>Also read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/clear-pycharm-run-window\" data-type=\"post\" data-id=\"57317\">How to Clear the PyCharm Run Window in Python?<\/a><\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Python3 Alias in Windows<\/h3>\n\n\n\n<p>On Windows, you can achieve an alias by creating a batch file that acts as an alias. Here are the steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open Notepad in your system. Press &#8216;Win + R&#8217; to open the run dialog. Then type Notepad and press Enter. Now Notepad will open on your screen.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In Notepad you have to write the following batch commands:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@echo off\npython %*\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"368\" height=\"215\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/BatchFile.png\" alt=\"BatchFile\" class=\"wp-image-57830\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/BatchFile.png 368w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/BatchFile-300x175.png 300w\" sizes=\"auto, (max-width: 368px) 100vw, 368px\" \/><\/figure>\n\n\n\n<p><strong>&#8216;@echo off<\/strong>&#8216; this line turns off the echoing of each command to the console, making the script cleaner and <strong>&#8216;python %*&#8217; <\/strong>calls the python3 executable with any additional arguments passed to the batch file. The<strong> &#8216;%*<\/strong>&#8216; in the batch file passes any arguments provided to <strong>python.bat<\/strong> to the python command.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Save your batch file with the .bat extension. For example, <strong>python.bat.<\/strong> Click on File in Notepad select Save As and save it with the .bat extension.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now test the alias. Open the command prompt and navigate to a directory containing Python script or use the cd command to change directory where the your batch file (<strong>&#8216;python.bat<\/strong>&#8216;) is located.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now you can run Python using the alias. Now you can use the python command as an alias for python3. For example: <\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npython hello.py\n<\/pre><\/div>\n\n\n<p>Here you can replace hello.py with the actual python file you want to run.<\/p>\n\n\n\n<p><em>Also read: <a href=\"https:\/\/www.askpython.com\/resources\/installing-latest-version-python-on-ubuntu-vps\" data-type=\"post\" data-id=\"45413\">Installing the Latest Version of Python on Ubuntu VPS<\/a><\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Python3 Alias in Ubuntu\/Linux<\/h3>\n\n\n\n<p>Let&#8217;s now edit the bashrc to add an alias for python3 letting us call it using python.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Simple Alias Creation in Ubuntu\/Linux Using Nano<\/h4>\n\n\n\n<p>To create an alias for Python3 we have to follow some steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open a shell configuration file eg. &#8216;~\/.bashrc&#8217; for a Bash or &#8216;~\/.zshrc&#8217; for Zsh in a text editor.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nnano ~\/.bashrc\n<\/pre><\/div>\n\n\n<p><strong>&#8216;nano&#8217;<\/strong> is a command-line text editor in Unix-like operating systems. This user-friendly text editor provides basic text editing capabilities through a command-line interface. &#8216;~\/.bashrc&#8217; is the file being opened for editing. &#8216;~&#8217; is a shorthand representation of the user&#8217;s home directory. So &#8216;~\/.bashrc&#8217; refers to a file located in the user&#8217;s home directory.  <\/p>\n\n\n\n<p>Then press enter and it will open a text editor.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"345\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/alias_for_python3_nano.png\" alt=\"Alias For Python3 Nano\" class=\"wp-image-57825\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/alias_for_python3_nano.png 840w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/alias_for_python3_nano-300x123.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/alias_for_python3_nano-768x315.png 768w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p>In the nano text editor add the &#8216;alias python=python3&#8217; at the end of the text as shown in the above figure. Then save (ctrl+o and enter) the file and exit (ctrl+x) the text editor. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now reload the shell configuration to apply changes by using the following command:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsource ~\/.bashrc\n<\/pre><\/div>\n\n\n<p><strong>&#8216;source&#8217;<\/strong> is used to execute the commands in the specified file. <strong>&#8216;source ~\/.bashrc&#8217; <\/strong>reads and executes the commands in the <strong>.bashrc<\/strong> file. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now when you type Python it will refer to Python3. You can check by running the following command:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npython --version\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"486\" height=\"53\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Alias_output.png\" alt=\"Alias Output\" class=\"wp-image-57826\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Alias_output.png 486w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Alias_output-300x33.png 300w\" sizes=\"auto, (max-width: 486px) 100vw, 486px\" \/><\/figure>\n\n\n\n<p>Here you can see Python refers to Python3.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Using Vi Editor for Python3 Alias in Ubuntu\/Linux<\/h4>\n\n\n\n<p>This second method is similar to method 1, in this method, we are using a<strong> &#8216;vi&#8217;<\/strong> text editor. Let&#8217;s see how we are creating an alias using a <strong>vi<\/strong> text editor.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open text editor. To open the text editor use the following command:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nvi ~\/.bashrc\n<\/pre><\/div>\n\n\n<p>This command opens the vi editor with a .bashrc file. You can now navigate to the end of a file by using arrow keys.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now press i to enter insert mode. Then add the following line to create an alias in the .bashrc file <\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nalias python=python3\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"362\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/alias_for_python3.png\" alt=\"Alias For Python3\" class=\"wp-image-57832\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/alias_for_python3.png 740w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/alias_for_python3-300x147.png 300w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\" \/><\/figure>\n\n\n\n<p>Then press <strong>&#8216;Esc<\/strong>&#8216; to exit insert mode. To save and exit type <strong>&#8216;:wq<\/strong>&#8216; and press enter. This writes changes and quits the vi editor.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now reload the shell configuration to apply changes by using the following command:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsource ~\/.bashrc\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Now you can use Python for Python3. You can check by using the following command:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npython\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"781\" height=\"50\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Vi_output.png\" alt=\"Vi Output\" class=\"wp-image-57833\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Vi_output.png 781w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Vi_output-300x19.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/Vi_output-768x49.png 768w\" sizes=\"auto, (max-width: 781px) 100vw, 781px\" \/><\/figure>\n\n\n\n<p>Here by running a Python command. It displays the version of Python3.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Alias allows users to create shorter and more memorable names for frequently used commands. It also makes it more efficient for users. Here we create an alias for python3 as python but you can use another shorter name. For example, py3. We create an alias on Ubuntu\/Linux and windows with different methods. Creating an alias on Ubuntu\/Linux is more straightforward than Windows.<\/p>\n\n\n\n<p><em>Now that you&#8217;ve mastered creating aliases in both Windows and Ubuntu\/Linux, think about other commands you frequently use. How can creating aliases streamline your workflow? What other shortcuts could you implement?<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>References:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/superuser.com\/questions\/1576758\/how-do-i-alias-python3-on-windows\" target=\"_blank\" rel=\"noopener\">https:\/\/superuser.com\/questions\/1576758\/how-do-i-alias-python3-on-windows<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/askubuntu.com\/questions\/320996\/how-to-make-python-program-command-execute-python-3\" target=\"_blank\" rel=\"noopener\">https:\/\/askubuntu.com\/questions\/320996\/how-to-make-python-program-command-execute-python-3<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Alias is a custom shorthand or nickname assigned to a longer command or sequence of commands. This allows users to create shortcuts for commonly used commands. Creating a Python3 alias involves simple steps in both Windows and Ubuntu\/Linux. In Windows, it requires a batch file named &#8216;python.bat&#8217; with specific commands, whereas in Ubuntu\/Linux, you add [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":64112,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-57750","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=57750"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57750\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/64112"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=57750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=57750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=57750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}