Run a .py file without Python installed using .bat - python

I want to create a .bat file to run my .py file on Windows without Python installed, mainly so I can send my programs to friends. So in my .bat file I would like to:
Change to the current working directory. (So it runs wherever the file is)
Change directory to a folder inside that directory.
Run a python file without my friends having to install python themselves.

Run a .py file without Python installed using .bat
You can't run a Python script using a batch command without Python being installed. You can compile an executable with Py2exe (which bundles an entire Python interpreter with your script), or convert a subset of Python to C++ with shedskin, which can then be compiled to an executable. You could also issue a shell command to install Python if it's not already installed and the user has internet.
But doing exactly what you asked is impossible.

Take a look at http://www.py2exe.org/ which will convert you python code to a executable windows program that you can send to your friends.

Related

Running .py file from cmd opens the python modify setup

I have a python file located at myFile.py that works perfectly fine when I run it from visual studio code or any python file launcher.
The problem is that whenever I run it using the python myFile.py command in the windows command prompt it just opens the Python Modify Setup (image below) instead of running the file.
Repairing or modifying python isn't the solution.
Here's a video showing the problem.
This problem is due to the python installation file python.exe being in the same directory as myFile.exe. It should be removed from that working directory.

Main Python File Using Another Python File (I Want To Convert To .exe)

I Created a Program That Has 2 .py files.
I Want To Make The Program a .exe file, I do it using cx_Freeze.
My Problem Is That I Convert The main.py To .exe but The Second Python File Is Still a .py File.
I Don't Want It Be a .py Because If It Is The User Can See The Code.
If I Also Convert The Second Python File The Program Doesn't Work Because I import The Python File In The Main File.
Any Suggestions?
(I Don't Want To Copy The Second Python File To The Main Python File)
You are chasing the wrong rabbit here. The various tools that generate executable files from Python code are not compilers. They are just tools that embed a Python interpretor with py (or pyc) files to allow users to use the program without a prior Python installation.
Said differently you should not use them to hide your code (except from people not knowing a lot of Python): a pyc does not contain text code but according to the answers to Is it possible to decompile a compiled .pyc file into a .py file? , tools exists that convert back a pyc file into a py file (of course except the comments).
IMHO, you should better google for python obfuscate to find tools dedicated to obfuscation, what neither cx-freeze nor pyinstaller are.
BTW while there are appropriate use cases for obfuscation you should be aware that a determinate attacker can always circumvent it. The reason why the real protection for intellectual property is law and not technics...
I'm not sure how two or more .py files can be converted to .exe.
But in python the easiest way to convert to .exe is a module named pyinstaller .
You can install it using command pip install pyinstaller can get it . After just go to the directory where your project files are and open command prompt in that directory and execute pyinstaller file_name

How to run pyinstaller from python script already converted to exe?

I'm trying to run pyinstaller in python exe file in order to someone without python can use pyinstaller but no idea how to do it.
I tried import PyInstaller with other needed modules and convert this script to exe but I got error "The 'PyInstaller' distribution was not found and is required by the application". I also tried to pack PyInstaller exe file but didn't worked too. Python 3.6.5
Any ideas how to do it?
Unfortunately, what you're describing is not possible with PyInstaller. I submitted an issue on GitHub, and this is what one of the developers said:
Nope, this won't work.
PyInstaller internally uses a lot of sub-processes where it is assumed that sys.executable points to a python interpreter (e.g., here); this is not true in a frozen application, where it points to the frozen executable, which ends up in effectively endless recursion.
Even if this was not a problem, the frozen application includes only a subset of python environment, so it cannot be used to freeze an arbitrary script (not to mention the issue of 3rd party packages).
So whatever use case you have for this, it cannot be supported by PyInstaller.
check the requirements for the Pyinstaller from this link initially
https://pythonhosted.org/PyInstaller/requirements.html
Then install Pyinstaller by,
pip install pyinstaller
To create .exe file use this command for basically,
pyinstaller your_script.py
To run the created .exe
METHOD 1
Do double click the .exe file in your directory.
METHOD 2
In your cmd prompt load in to your current directory or project directory then do
: .\dist\your_script.exe
because the create .exe files are saved in dist folder inside to the project folder with the name of your script file names only.

How to download python with python?

So basically what I'm trying to do is to download python with a python file. It needs to be so that user does not have to interact with the python installer at all. They just click the file and python is installed on the computer. I don't need pip I just need the computer to be able to read .py files. I seem to only be able to download the installer which the user then must open.
What would the solution to this be?
I'm using python 3.6. Also, the file must be able to be compiled to .exe so something built into python and not a module from the internet would be best (I sometimes have trouble compiling files with modules from the internet with pyinstaller) but I'm open to anything.
This is my first question so I might be doing something wrong...
You will want to start by developing a python script that does any function you want it to do. In this case, it looks like you want it to download python and install it without user interaction and you want to be able to run this as an executable on windows. So your process would be as follows.
your_script_name.py
This contains all the code to download and install python non-interactive.
Then to convert this into windows.exe install pyinstaller then run the following.
pyinstaller --onefile <your_script_name>.py
This will result in your_script_name.exe
Note: You need to run pyinstaller on a windows system for it to produce an .exe

Python 3.6.0 can't be installed on Ubuntu 16.04?

I wrote a script on my Windows machine using python 3.6.0. I wanted to run it on Linux machines as well so I transferred the .py script onto my Ubuntu VM. The problem is, I tried to install python 3.6.0 in every way possible but it doesn't really work. Python3 works when I want to execute a .py file, but since I am trying to use pyisntaller to convert it into an executable for linux, I need the default version as 3.6.0. python --version gives me Python 2.7.2. How can I fix this so when I do python --version it shows 3.6.0?
There is no point in changing your default python version, Ubuntu relies heavily on older versions of python. And what do you mean by convert it into an executable for Linux? Almost every file on your file system is an executable. If you want to make your program run as a command in the terminal you can do this though:
text_editor_of_your_choice .bashrc
This should open the hidden .bashrc file, and scroll to the bottom of it. Below is what the file looks like...
.bashrc
After that you can create a function for the python command you want to run.
eg. function ghst {python3.6 /home/user/example_script/example.py $#}
Start a terminal session and then try running it. Now that's if you want to run it inside the terminal. If it you want to create this "executable file" you speak of you can create a simple shell script.
First go in the terminal and run touch file_name_of_your_choice.sh
This will create a blank file named file_name_of_your_choice with the file extension "sh". After that open the text file in whatever directory you created it in, if you are unsure do pwd.
In your first line make sure that the user is inside the directory of the .py file by doing
cd /home/user/folder/ On the next line you should then execute the .py file. You can do this by typing this: python3 file.py. This will execute the program inside a terminal window. After you have done all of this make sure that the python file and the script file are executable. Do this by doing chmod +x file.py and chmod file.sh.
This is what the end result looks like
example script
You mentioned inside your question that you want your program to be able to run on Linux machines as well. Whether this is for personal or a public project of some sort it comes in handy by packaging all the required files in a .zip format this makes it easy to unpack everything when moving to separate machines. If there are any problems let me know.

Categories

Resources