MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python Converting py to exe

Steps to Create an Executable from Python Script using Pyinstaller


Step 1: Install the Pyinstaller Package


The first step is to install the module pyinstaller. For this, we will create or destinate a folder and open our power shell window there using shift + mouse right-click.

Now we will run the following command for our module installation.

pip install pyinstaller

Step 2: Save your Python Script


Right-click on the screen and then new and text document to create a .txt file and write the python script. We can save our python script in a text file, too. For the conversion, it does not necessarily have to be a .py file, but the code should be Python. Now open the power shell window and write the following commands.

python .\main.txt

The code written in the main.txt file will execute.

Step 3: Create the Executable using Pyinstaller


As we want a .exe file. So we are going to run the following command.

pyinstaller main.txt

It will show us a few warnings, as shown in the image below. Along with that, it could be a little time-consuming in the case of bigger programs. We should not avoid the warnings as they can be a security threat later on. You can search for the meaning of the given warning by searching it on the internet. Sometimes software causes problems while installing the pyinstaller module. The reason for this might be the incomplete installation of pip.

Step 4: Run the Executable


After running the pyinstaller main.txt command, it will create some folders. Click on the dist folder and then click on the main.

In that folder, we can find our .exe file. We can open it through the PowerShell window by running the command.

.\main.exe

Now, as you saw, by converting the file to .exe, we got several files in the folder, but we can also run a command that will provide us with only one file as a resultant. It will take more time in creation, and later on, it will extract too, but for compatibility, we can use the following command:

pyinstaller --onefile file.py

Here the file extension depends on your file that whether you created it using .py or .txt.

Once we click on the file, we are ready to launch our program. So, that’s how you can easily convert the .py file to the .exe file in Python.

Conclusion

In this page (written and validated by ) you learned about Python Converting .py to .exe . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Command Line Utility.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.