The provided content outlines methods for running Python scripts without a console in Windows, including using Bash and VBS scripting to execute the scripts with a single click and even automate tasks without user interaction.
Abstract
The article discusses the utility of running Python scripts without the need for a console window, which is particularly useful for GUI applications, automation, and making scripts more accessible to non-technical users. It provides a step-by-step guide on creating a Bash script to run a Python script within a Conda environment or a standard Python environment, and a VBS script to execute the Bash script without displaying the console window. The author also includes a bonus tip on using the pynput library to create a keylogger that can trigger scripts or applications based on specific keypress patterns. The article concludes with a call to action, inviting readers to follow the author for more content and to consider trying out an AI service recommended by the author.
Opinions
The author believes that running Python scripts without a console makes GUI applications more user-friendly and automation tasks less intrusive.
It is implied that using a combination of Bash and VBS scripts to run Python code is a simple and effective solution.
The author suggests that the described methods can enhance the user experience by allowing scripts to be executed with a single click or automatically.
There is an opinion that integrating a keylogger using pynput can further streamline task automation by responding to custom keypress sequences.
The author endorses an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), indicating a preference or positive evaluation of this service.
Run Your Python Code Without Console
Simple Trick To Run All Your Python Scripts Without a Console In Windows, With One Bonus Tip At The End
Image By Author
In the world of programming, if you want to choose a language that can do anything for you also at the same time it has to be simple then Python will be your first choice. it is a vast language with the extraordinary support of various packages and libraries. Like other programming languages python also needs a console to run the code but there is a way using which we can easily make our python script run without a console in windows. But first, we need a python script.
How Useful is to Run Python Without Console
▶ Makes Your Python GUI’s to Run On a Single Mouse Click.
▶ Helps in automating tasks.
▶ Looks Impressive even for a non-tech guy.
▶ Makes the script easy to handle and use for anyone.
▶ Can help to connect multiple scripts at once.
Now That We Know It is very useful to have this tool in our toolkit let’s write the python script.
We are going to write a script that can be used to listen to the trending news of your country with a single click from the desktop. We will be using the NewsAPI for retrieving the trending news.
Just Visit the NewsAPI and get your API key. Now You Need to choose your country and accordingly you will get a URL. also, we are planning to make our script speak news so we will require pyttsx3 a python package to speak the news for us.
Now Comes The Best Part How To Run This Code Without Console. It is very simple we just need two more files to add to our main .py file.
1. Bash Scripting File
It is a file that will help you to run the python code using the console without any external help from the user. It requires just one click to run the code. We can customize the script to run a python file even inside a conda environment or a normal python environment. it is saved with the extension .bat
Line 1: @echorepresents the path or steps taken to run the steps. @echo off will basically hide all the steps and just show the output.
Line 2: call is used for calling the conda command prompt and then running it through the activate.bat file.
Line 3: call is used for activating the conda environment scraperLine 4: The Python file news.pyis being run at this moment in the command prompt and an end loop is started until the output comes.
Line 5: Exits the loops and closes the command prompt.
Rather than a conda environment, you have a normal python -menvironment then you just need to remove line 2 and in line 3 you can pass the path to the env call C:\Users\abhay\Desktop\Automation\Scripts\activate rest will be the same.
2. VBS Script File
VBScript is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. It allows Microsoft Windows system administrators to generate powerful tools for managing computers with error handling, subroutines, and other advanced programming constructs, as per wikipedia.
We can use this script to run our bash script without a console.
The process will be simple our VBS Script will run the .bat file without a console which will then automatically run the python code.
Simply open a notepad file and save it as .vbs after pasting the below code. Also, Paste all three files in the same directory.
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs ="cmd /c news.bat"
oShell.Run strArgs, 0, false
oshell will provide the script command Line Privileges. strArgs contains the complete path to your bat file. if your bat file is at a different location then use the complete path.
strArgs="cmd /c C:\Users\abhay\Desktop\news.bat"
Now That You have everything set up Just create a shortcut of the VBS script and paste it on the desktop. Now Follow Below Steps.
Right Click on the shortcut and then click on properties.
Click on change icon and select an icon that suits your script best.
Apply and press ok.
You can access your script by just double pressing the icon on the desktop you have just created.
What Else Can I Do
You can use the same process to run a software .exe file from the desktop. when we download crack software from the internet then sometimes the software only runs from the folder it is installed. even after creating a shortcut of the .exe file it doesn’t work. In these types of situations, we can use these two scripts to run the software directly from the desktop. The VBS script will be the same but the .bat script has some changes.
@echo off
START F:\softwares\photoshop\PhotoshopPortable.exe
exit
Line 2: File path to the .exe file.
Bonus Tip
There are many libraries in python that help you to track the record of your key presses. One of the libraries that I remember is pynput that helps you do the same. We can use this library to build a keylogger for ourselves that tracks the records of the keypresses and on a certain pattern of keypresses, it will do some tasks like opening an application or running a python script. An example of this can be running the trending news script on [0–0]a set of keys. Below is a code sample you get you started with the unended journey of automation.