How to Run Python Programs/Scripts

How to Run Python Programs/Scripts

Introduction

So you have finally creatd the perfect script or just downloaded a really cool program built in Python but aren’t quite sure how to run it? Well in this tutorial we are going to cover how to run Python scripts and programs on various different operating systems! Strap in, this is a fun one and much easier than you think!

Prerequisites

If you haven’t already, you need to make sure that you have installed Python to your system or that Python is already installed be default on your system. If you are unsure of how to do this, please check out one of our guides on how to do so: Install Python on Ubuntu or Install Python on Windows. MacOS and Linux usually already come with Python installed by default, and these articles will let you know how to check to see what version you are running and to make sure your terminal or command prompt has access to the Python command already.

Windows

Command Line

So the first one up today is how to run these scripts from the Windoes command line. This is going to be most similar to other operating systems like Linux and MacOS. To begin first open up a command prompt by searching in the Windows Search Bar, or by pressing Windows Key + R and typing in cmd.exe

Once you have the command prompt open, simply navigate to the folder that contains the script you are looking to run with the cd command.

cd (Change Directory) to your Python Files

cd path/to/your/files

Once you have gotten to the location that houses your files that end in .py, then to run those scripts, all you need to do is simple type out the following.

Run the Python Script/Program

python some_script_name_here.py
python path/to/script/some_script_name_here.py

Now of course the python prefix can vary, if you followed my tutorial on how to Install Python on Windows, then you will know all about the py launcher feature that lets you specify different versions of python easily like so:

How to Use PY Launcher to Run Multiple Versions of Python

py -2 some_script_name_here.py
py -3 some_script_name_here.py
py -3.10 some_script_name_here.py

So that is about it when it comes to running Python scripts in the Windows Command Line. This is basically the same process for Linux and MacOS, though I don’t believe those OS’s have the py luancher feature built in, but we will cover that in those sections.

NOTE: You do NOT have to navigate to the script directly to execute it, you can provide the path to the script from whatever directory you are in without needing to navigate there directly.

Windows GUI

To run Python scripts from the Windoes GUI using your mouse, it is as simple as double clicking on the script or program files that you wish to run. This will then open a popup window asking you how you would like to open this file. Select the Python option and the script or program should start running. You can choose Always or Just Once. If you choose Always, it will always use Python to open your .py files and you will never have to mess around with that again. If you choose Just Once, you will need to select this option each time. This is available on Windows 11. Older versions of Windows have a different way to handle this that I explain later on.

Run Python Script by Double Clicking

Be aware, that they may flash the command prompt briefly and vanish, this is because there is no pause or break before the end of the program execution to keep the window open. If this is a script that you have built yourself you may need to put in a break or pause at the end of the file in order to keep the window open if it is a console based application.

If this is launching a GUI like Tkinter or something else, then the program should just launch like normal Windows applications without issue.

Setting the Default Program for Older Versions of Windows

As I mentioned before, older versions of Windows don’t have the Just Once and Always options. You can also set the default program to handle opening all .py files on the system if you do not want to have to select Python each time. To do this, simply navigate to any Python file in your system:

right-click on that script-> choose open with-> choose default program-> more options-> and select the python.exe option.

Creating a Batch Script

Ok, so you want something a little more robust that can maybe run multiple scripts at once and that you can just double click on and it runs without having to deal with selecting Python each time to run the scipt or program. Well batch scripting is the best way to handle this in Windows and allows you to do some really powerful stuff! We will be creating an entire series on batch scripting in the future, but for now lets look at the basics of setting up a simple script to handle running your python scripts.

Creating a Batch Script to Run Python Files on Windows

@echo off

rem Run first Python script
echo Running first Python script...
python C:\path\to\first_script.py

rem Run second Python script
echo Running second Python script...
python C:\path\to\second_script.py

rem Run third Python script
echo Running third Python script...
python C:\path\to\third_script.py

rem End of script
echo All scripts have been run.
pause # Used to pause the Console/Terminal so you can see the output of the program

In this script, the @echo off command turns off the echoing of commands in the console window. Then, the first Python script is run using the python command and the path to the script file. This is repeated for the second and third scripts. Finally, the script outputs a message that all scripts have been run and pauses to wait for the user to press a key before closing the console window.

As you can see this is rather simple to do and is very powerful allowing you to run multiple Python scripts in a row. Just to clarify, the rem in this script is for remarks and allows you to add comments to the script. If you wish to see the code in the console, such as running a script or program that is a console app, make sure to exclude the @echo off line at the top of the script. Next lets cover how to do this in Linux!

Linux

Terminal

Ok so much of what we covered in the Windows section also applies to Linux and by extension to macOS, with slight differences. In Linux, you can open the python scripts or programs in the terminal app by also typing out

Run Python in the Linux Terminal

python path/to/file/some_script_name.py
python script_name.py

I have provided two different ways, which can also be done in Windows. One lets you set the full path to the file you wish to run from whatever directory you are currently in without having to navigate to it, and the other allows you to run a script in the directory you are currently in. You can do the same on Windows, and just like how I explained in the Windows section, you can cd to the location of your script directly instead.

Linux GUI

To double click and run a Python script or program from the Linux GUI you will need to make the script or program executable. To do this, you will need to go into the Terminal app and run a simple command against the file in question. Once you have done that, you will be able to double click and run the file.

Make Python Script Executable

chmod +x script_name.py

Next, you will want to make sure that the Python script or program that you are wanting to run has this shebang at the top of the file so the system knows what interpreter to run:

Add Shebang/Hashbang to Python Script

#!/usr/bin/env python3

At this point, you should be able to double click on the file in the GUI and have it run or just run it from the terminal app by specifying its name only.

Run Executable Python Script

script_name.py

Another additional step that you can take is to move the script or program file to a directory that is included in your system PATH, so that you can run the script from anywhere on the system without needing to type out the full path to the script or navigate to its directory directly.

Move Script to Directory in System Path

sudo mv script_name.py /usr/local/bin/

Special Note

Remember earlier I mentioned the py launcher that was available on Windows. This is a Windows specific tool to allow switching versions of Python easily. This tool is not available on Linux or MacOS so you will either need to type out the full path to the Python interpreter that you want to use, set up Symlinks, or use something like Python2 or Python3 in order to run the scripts. To setup a Symlink, it is rather straight forward:

How to Symbolic Link Python Versions to Different Commands

sudo ln -s /usr/bin/python3.9 /usr/local/bin/python3.9 # name of the command you will call
sudo ln -s /usr/bin/python2.7 /usr/local/bin/python2.7

# How to call the new symlinks
python3.9 script.py 
python2.7 script.py

So as you can see, the command is rather simple, but allows you to create any name that you wish to use to call various versions of Python in Linux and MacOS. The name you specify at the end of the /usr/local/bin/ is the name of the command that you will use to call that version of Python. Make sure the paths to the versions of Python you are symlinking are correct or you will not be able to run the scripts or programs that you wish.

Linux Shell/Bash Script

So just like on Windows, you can easily create shell/bash scripts to run multiple Python scripts or programs at once. The term Bash or Shell are often used interchangeably in Linux as they refer to the Bash shell. There are other shells available on Linux, but this is the most common one that you will encounter. To create the shell script, you just need to do a few simple steps:

Create a Shell/Bash Script File and Run It

You can use something like Nano, Vim, or Gedit in order to create the shell script file that will house the code to run your Python script or program.

Create and Open a New Shell Script

nano runscript.sh # Creates a new file and opens it in Nano
vim runscript.sh # Creates a new file and opens it in Vim
gedit runscript.sh # Creates a new file and opens it in Gedit

Code to Include in Shell Script

#!/bin/bash
python myscript.py # Only works if the Shell Script and Python Script are in the same directoy
python /path/to/script/myscript.py # Best to put the full path if you want to run the script from anywhere

Make the Shell Script Executable and Run It

chmod +x runscript.sh # Make the script executable
./runscript.sh # Run the script

So as you can see, it is very similar to the Windows batch script with just a couple extra steps in that you need to mark the script as executable and then run it. You should also be able to just double click the script now that it is executable from the GUI and have it run that way as well. Also be aware that this functionality is defined by the file manager that you are running. While most Linux file managers support double clicking and running shell scripts that have already been marked as executable, not all do. If you run into any issues, google your file manager that you are using to see if it supports this feature or not, and how to enable it if it is not enabled by default. I would cover how to do that here, but there are so many different ones, it would be impossible to list them all.

In the next section we will cover MacOS and wrap this tutorial up!

MacOS

Terminal

So very similar to Linux, you are just going to type out:

Running Python Script on MacOS Terminal

python script_name.py
python path/to/script/script_name.py

Nothing needs to change here as both Linux and MacOS are based off of Unix and share many of the same commands and ways to run things. 

MacOS GUI

So unlike Linux there may be some additional steps to allow double clicking and running a Python script on MacOS. 

The general idea is the same, you first need to chmod +x the script and then double click on it. MacOS however sometimes will prevent you from running unsigned programs. Lets recap making the script executable and then I will talk about how to get around gatekeeper on Mac to allow you to run the script or program.

Make Python Script executable on MacOS

chmod +x script_name.py
chmod +x path/to/script/script_name.py

Ok, so that covers marking the script as executable, now you can try double clicking on it and see if it will run. If not, Gatekeeper is preventing unsigned programs from running on your system and you need to bypass it.

WARNING: this is not something to do lightly and only do it on software and scripts that you trust. 

These are the steps you will need to take in order to grant permission for your script or program to run:

  1. Open System Preferences.
  2. Click on Security & Privacy.
  3. Click on the General tab.
  4. Under “Allow apps downloaded from,” select “App Store and identified developers” or “Anywhere” (if it’s available).

Note: “Anywhere” is not available by default on newer versions of macOS, but you can enable it by running the following command in the terminal:

Disable Gatekeeper on MacOS

sudo spctl --master-disable

This will disable Gatekeeper, which enforces code signing and verification of downloaded apps.

WARNING: Disabling Gatekeeper can increase the security risks on your system. Make sure you only run trusted software from trusted sources.

This warning is worth repeating as it can be dangerous if you run scripts or software you did not write yourself and open youself up to malware and other attacks.

MacOS Shell Script

Same as the Linux shell script you can also create these on MacOS to run mutliple Python files at once or just have an easy way to execute a script with some CLI parameters that you may not want to type out everytime. Ill show a quick example below but it is the same process as on Linux. 

Create MacOS Shell Script

vim myscript.sh

Add Code to Shell Script with CLI Parameters - MacOS

#!/bin/bash

python3 /path/to/your/python/script.py "$1" "$2"

Mark Shell Script Executable and Run It

chmod +x myscript.sh
./myscript.sh arg1 arg2

So this script can except some CLI parameters that can then passed into the python script or program each time it is ran. You can also double click the shell script and run it if you have permission or if you have disabled Gatekeeper to run unsigned code. 

That about covers the most popular ways to run python scripts or programs in MacOS, lets wrap this tutorial up below.

Conclusion

So that about wraps up the various ways that you can run Python scripts on various OS’s. As you can see, there are several ways to acheive the same results and whichever you choose is honestly down to personal preference. Windows py launcher is really a handy tool that makes working with Python an enjoyable experience, which was not always the case on Windows in years past. With the power of Symlinks on Linux and MacOS, that is honestly the easiest route to take. Of course you can create shell scripts as well, just like creating a batch script on Windows to handle running more than one Python script or program at a time.

We hope you enjoyed this tutorial and learned something new today. As always, feel free to look around at our ever expanding set of tutorials and leave us a comment and share with your friends if you found this content helpful!

C# Polyglot Notebooks are Here!

C# Polyglot Notebooks are Here!

Polyglot Notebooks now generally available in Visual Studio Code Marketplace

The Polyglot Notebooks extension, which allows developers to mix and match multiple programming languages within the same notebook, is now generally available in the Visual Studio Code Marketplace, according to a recent announcement by Microsoft. This feature enables a more flexible and streamlined way to write and share code across different contexts and purposes, without having to switch between separate tools or languages.

What are Notebooks and why they are useful

If you’re not familiar with notebooks, they are interactive computational files that support mixing executable code, visualizations, equations, and narrative text. Notebooks have code cells that allow code to be run in an incremental and segmented manner, unlike traditional scripts that need to be run in their entirety. Popularized by the open-source Project Jupyter, notebooks have become the de facto tool for data science and teaching, and can be used for a wide range of programming and prototyping tasks.

What are Polyglot Notebooks and why they are different

Polyglot Notebooks take the concept of notebooks to a whole new level. The philosophy behind this feature is that developers should always be able to choose the best language for the task at hand, and not be constrained by the limitations of a single language or tool. With Polyglot Notebooks, you can use multiple languages natively within the same notebook, with full language server support, and share variables between them to maintain a continuous workflow. This means you can connect to a database, run queries in SQL or KQL, create visualizations in JavaScript or HTML, and more, all within the same tool and the same notebook.

How Polyglot Notebooks work and what they support

Polyglot Notebooks in VS Code are powered by .NET Interactive, which is an innovative engine built using .NET technology that can run multiple languages and share variables between them. .NET Interactive can behave as a kernel in the context of notebooks, which means it can execute code in multiple languages, including C#, F#, PowerShell, JavaScript, HTML, Mermaid, SQL, KQL (Kusto Query Language), and more.

Key features of Polyglot Notebooks in VS Code

Some of the key features of Polyglot Notebooks in VS Code include:

  • Connecting to and querying Microsoft SQL Server databases and Kusto Clusters
  • Language server support such as autocompletion, syntax highlighting, and signature help for all languages
  • Variable sharing between different languages in the same notebook, for a continuous workflow
  • Ability to create interactive visualizations and narratives using multiple languages and data sources

How to get started with Polyglot Notebooks in VS Code

To get started with Polyglot Notebooks, you will need to install

Once you have these installed, you can create your first notebook by opening the command palette (Ctrl+Shift+P in Windows, Cmd+Shift+P on iOS) and selecting “Polyglot Notebook: Create new blank notebook”, selecting “.ipynb”, and choosing the language you’d like to start with. To change the language of a cell being used, simply click on the language picker in the bottom right of the cell and choose your desired language.

Once they have all the necessary tools installed, they can create their first notebook by opening the command palette (Ctrl+Shift+P in Windows, Cmd+Shift+P on iOS) and selecting “Polyglot Notebook: Create new blank notebook”, select ‘.ipynb’, and select the language they’d like to start with. They should see “.NET Interactive” in the top right as this indicates the kernel being used. To change the language of a cell being used, simply click on the language picker in the bottom right of the cell and choose your desired language.

Feedback

If developers would like to provide any feedback, file any bugs, or request any features, they can file an issue on the .NET Interactive GitHub repository. To learn more, they can visit the Polyglot Notebook Documentation.

Conclusion

Polyglot Notebooks in VS Code are an excellent tool for developers who want to work with multiple languages natively within the same notebook with full language server support. It allows them to share variables between different languages to maintain a continuous workflow. With its innovative engine built using .NET technology that can run multiple languages and share variables between them, Polyglot Notebooks in VS Code is definitely worth trying out for developers who work with multiple languages.

 

Source: Microsoft Blog

Why C#?

Why C#?

What is C#?

C# is a high-level, object-oriented programming language that was developed by Microsoft. It was first introduced in 2000 as part of the .NET framework and has since become one of the most widely used programming languages in the world.

C# is designed to be a simple, modern, and type-safe language that is easy to learn and use. It’s often used to develop Windows desktop applications, video games, mobile applications, and web applications.

Why is C# a great starting language?

There are many reasons why C# is a great language for beginners. Here are just a few:

Object-oriented programming

C# is an object-oriented programming (OOP) language, which means it’s designed to model real-world objects and concepts. This makes it easier to understand and organize complex programs. OOP also makes it easier to reuse code, as objects can be reused in different parts of a program.

Large community and support

C# has a large and active community of developers who are constantly creating new libraries, tools, and frameworks. This means that if you run into a problem while learning C#, you’re likely to find a solution online. There are also many tutorials, guides, and forums available to help you learn the language.

Versatile

C# is a versatile language that can be used for a wide range of applications. Whether you’re interested in desktop applications, video games, mobile applications, or web applications, C# has a library or framework that can help you get started.

For example, if you’re interested in desktop applications, you can use the Windows Forms or WPF frameworks to build graphical user interfaces (GUIs). If you’re interested in video games, you can use the Unity game engine and the MonoGame library. If you’re interested in web applications, you can use the ASP.NET framework.

Career opportunities

Learning C# can open up many career opportunities, as it’s widely used in many industries. C# is used by many companies, including Microsoft, Amazon, and Intel. It’s also used in a wide range of industries, including finance, healthcare, and education.

According to the TIOBE Index, C# is currently the fifth most popular programming language, after Java, C, Python, and C++. This means that there is a high demand for C# developers.

Getting started with C#

If you’re interested in learning C#, there are many resources available to help you get started. Here are a few steps you can take:

Install Visual Studio

Visual Studio is an integrated development environment (IDE) that is widely used for C# development. You can download Visual Studio from the official website at https://visualstudio.microsoft.com/vs/community/. Once you’ve installed Visual Studio, you can use it to write and run your C# code.

Learn the basics

Once you’ve installed Visual Studio, you can start learning the basics of C#. There are many online tutorials and courses available, such as Microsoft’s C# tutorial, that can help you get started as well as tutorials right here on Learn Code Today!

Practice, practice, practice

The key to learning any programming language is to practice writing code. You can start by writing simple programs, such as a program that prints out the numbers from 1 to 10 or a program that asks the user for their name and greets them.

As you become more comfortable with the language, you can move on to more complex programs, such as video games or web applications. Just remember to never give up, pace yourself, and reward yourself every step of the way.

Join a community

As mentioned earlier, C# has a large and active community of developers. Joining a community can be a great way to get support and learn from others who are also learning the language. You can join online forums, such as Reddit’s r/csharp or Stack Overflow, or attend local C# meetups.

Build projects

One of the best ways to learn C# is to build projects. Projects can help you apply what you’ve learned and give you a sense of accomplishment. You can build projects in a wide range of areas, such as desktop applications, video games, mobile applications, and web applications.

Here are a few project ideas to get you started:

  • Build a Windows desktop application using the Windows Forms or WPF frameworks. A great starter project is a todo app or a small weather application using a free API.
  • Build a video game using the Unity game engine and the MonoGame library. Starting off with a 2d platformer is a great way to have fun and learn the language.
  • Build a mobile application using Xamarin or the Unity game engine.
  • Build a web application using the ASP.NET framework.

Conclusion

C# is a great starting language for beginners who are interested in learning programming. It’s simple, modern, and type-safe, and has a large community of developers. Learning C# can open up many career opportunities, as it’s widely used in many industries.

If you’re interested in learning C#, there are many resources available to help you get started. Install Visual Studio, learn the basics, practice writing code, join a community, and build projects. With time and practice, you can become proficient in C# and use it to build amazing things.

Installing Python on Windows 10/11

Installing Python on Windows 10/11

Introduction

 

Hello and welcome to another tutorial on Learn Code Today. Today we are going to learn how to install Python to your computer. There are several different ways to install Python to our systems, but we are going to look at what I call a “Hard Install”. This means that we will install Python directly to our PC and not to something like Docker which containerizes installs so they don’t conflict with one another on a system. Let’s begin!

Step 1

 

To start, we need to navigate over to the Python website. Head over to https://www.python.org/ and you will see a downloads button right on the front page.

Python.org Downloads

Click on the “Downloads” button on the navigation bar, and it will take you to yet another page that will allow you to choose which version you would like to download. 

Python Download Link

Now quick caveat. This will always download the 32-bit installer. If you are looking for the 64-bit installer, you will need to scroll down the page until you see the spot that says, “Looking for a specific installer”. This is where you will click the latest version and be taken to another page that will have the source files, exe’s, and web installers in both 32-bit and 64-bit. 

Python Specific Installer
Python 64 Bit installer

So you can see in the picture directly above, I have indicated with check marks, which python installer is 64-bit. While 32-bit is often enough for most people, if you have more than 4GB of RAM in your PC, you can benefit from running 64-bit Python on your system. If you have less than 4GB, most likely you are only running a 32-bit version of Windows, and installation will fail when trying to install the 64-bit version.

Ok enough technical speak, lets move onto downloading and installing Python! So once you have clicked the version you would like, a download prompt will appear asking you where you would like to put the installer. You can rename the installer if you like and put it anywhere easy to remember on your system.

Python Installer Save Location

Step 2

 

Ok, once you have the installer saved to your system. Its now time to run it. Quick side note, if you downloaded one of the optional installers as I highlighted above for the 64-bit version, make sure you understand how to run it. If it was the zip file, extract the contents first, and then double click the installer. If it was the EXE, well then all you need to do is double click it just like those that download it directly from the button on the second page without going for the 64-bit version.

Ok, now that you have opened the installer, you will be greeted by a first screen. It is important that you check the box at the bottom that says, “Add Python 3.X to Path” or something along those lines. This will save you major headaches down the line when trying to run python or scripts from the command line. Once you have checked that box, you can proceed with the normal installation by selecting “Install Now”. If you prefer to install Python to a different location, go ahead with the custom install and provide the location and proceed as normal from there.

 

Python Installer Screen 1

Once you begin the actual installation, it should go pretty quick, depending on your internet connection. There will be a UAC popup requesting permission to continue, so make sure to approve that to get the actual installation underway. 

Python Installing

Once the installer has completed, one final screen will appear asking you if you want to disable the path length limit. I would advise selecting yes for this option. Clicking on it will bring about yet another UAC popup. Just confirm and it will take you back to the complete screen with a “close” button in the lower right hand corner. You can now safely close the installer and Python is successfully installed on your system!

Python Installer Success

Step 3

 

Congratulations! You can now start using python on your system. To get started, simply navigate to the windows start menu, and in the recent programs section at the very top, you should see something like this:

Recently Added Programs

Click on the IDLE option and an interactive display will appear allowing you to start coding in real time with Python. You can also just open up the Command Prompt (CMD) and type in Python and hit “Enter” or “Return” and it will launch an interactive version there as well. 

Python IDLE
Python CMD

Conclusion

 

That is it for this tutorial. I hope you found it helpful. If you are on MAC or Linux, the process is fairly straight forward as well. With MAC, you install python just like any other MAC program and there is a section on their website with the download and instructions on how to do so. For Linux, just use your favorite package manager or compile from source and you are good to go! Most Linux distros come with Python pre-installed already as it is, but it is often and older version. I assume also, that if you are on Linux, you are already fairly familiar with installing applications and decent when it comes to being “tech savvy” and are more than capable of figuring it out.

Pin It on Pinterest