Hello gyus i have started python and i want to know how can i excecute python file without using terminal.Just like the most games using (exe) file extension but for py files.I have tried py2exe but it doesn't show anything on the screen. I tried to make excecutable the py file with no luck. Please tell me how to excecute the and if there is an option whithout using a specific program for that.
My system is : Windows 7 / Ubuntu 12.04
Here, have a look in my blog, It explains how ca you do it in Ubuntu.
http://insidepython.wordpress.com/2012/08/04/hello-world-or-how-or-say-ni/
but basically:
Add this line to the beginning of your script
#!<location of your python interpreter>
To find out where your python interpreter is installed:
$ sudo find / -name "python"
After executing the previous, you should get the location of your python interpreter, then you need to set the environment variable, in my case python executable is located in /usr/bin/python
$ export PATH="$PATH:/usr/bin/python"
Then you need to set the file attributes to executable, you can look more into file attributes in Unix/Linux here http://en.wikipedia.org/wiki/Chmod
$ chmod +x shrubbery.py
And finally to execute your application
$ ./shrubbery.py
For Windows, there's also a python script called GUI2Exe that I have used in the past to create distributable versions of python scripts as apps. It's available at
http://code.google.com/p/gui2exe/ and is, in my opinion, very simple to use. Tutorials and whatnot are easy to find on Google.
It uses py2exe (or any other Python compiler library) to put together the script, but it doesn't require any syntactically annoying setup.py file.
Related
I am trying to create a portable venv on my stick to copy paste it to someone, so he doens't need to install python on the pc to run my python script.
I tried searching here for a solution, but maybe because I am new to python and environments I can't find the right answer.
I installed Python version 3.9.1 from the website in my folder (Python) and created an environment with it:
Python\pyver\py391\python -m venv pyenv
running where in venv shows:
(pyenv) D:\CD_Inhalt\UpperLimb>where python
D:\CD_Inhalt\UpperLimb\Python\pyenv\Scripts\python.exe
running my script show an error:
(pyenv) D:\CD_Inhalt\UpperLimb>UpperLimb.py
Traceback (most recent call last):
File "D:\CD_Inhalt\UpperLimb\UpperLimb.py", line 11, in <module>
import c3d
ModuleNotFoundError: No module named 'c3d'
But I know that I installed c3d:
(pyenv) D:\CD_Inhalt\UpperLimb>pip list
Package Version
--------------- ---------
appdirs 1.4.4
c3d 0.3.0
certifi 2020.12.5
chardet 4.0.0
...
This Computer doens't have Python installed. Running print(sys.path) shows following:
(pyenv) D:\CD_Inhalt\UpperLimb>python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', 'D:\\CD_Inhalt\\UpperLimb\\Python\\pyver\\py391\\python39.zip', 'D:\\CD_Inhalt\\UpperLimb
\\Python\\pyver\\py391\\DLLs', 'D:\\CD_Inhalt\\UpperLimb\\Python\\pyver\\py391\\lib',
'D:\\CD_Inhalt\\UpperLimb\\Python\\pyver\\py391', 'D:\\CD_Inhalt\\UpperLimb\\Python\\pyenv',
'D:\\CD_Inhalt\\UpperLimb\\Python\\pyenv\\lib\\site-packages']
So where excatly did I made an error? I am confused. Appreciate any help :)
Greetings
Edit 1: like hoefling mentioned it. If I write python UpperLimb.py it works. How can I run it directily as an executable? Without python before?
This is a XY-problem. Let me first tell how you can solve your problem, and then explain in the Appendix what was the reason for the problems you were facing.
1) Solving the problem: Creating portable application
Your original problem is in the first paragraph: You want to share your python script/app with someone, and not to force the user to install python. Virtual environments are not solution to your problem. Although, they are part of the solution since any application development should be done inside a virtual environment to make your projects isolated. Virtual environments are not portable; they are dependent on the python installation on the machine they were created on.
If you want to share your code, you have three different options
(1) Creating and executable
Many people like to use tools like cx_Freeze, pyinstaller or Nuitka to create an executable from the python code. There are gotchas, though. These tools work quite well with simple apps, but can be a pain with larger and more complex applications that use lot of static files, extension modules (.dlls), or depend on the __file__ attribute of the module to access a file on filesystem. Just do a simple search on SO with the toolname to see some example cases.
(2) Sharing code with portable python
There are portable python versions. For example WinPython, or the Embeddable Python from Python.org. You could pack them with your application code and simple .bat file that runs the python.exe myscript (using relative paths). If you have external dependencies (some packages), you could include their .whl files (download from PyPI), and make your .bat install them, too. I have done this successfully previously with very complex python programs, and handled my programs to end-users that have no clue about python. This takes a bit of effort and manual work, though.
(3) Using pynsist
An alternative approach would be to use pynsist for the job. It is used for distributing python apps, but it does not make a single "myapp.exe". What it does is basically automates the "Sharing code with portable python". It also creates an installer that you can just handle to your friend(s) to use, and the installed program will be visible in Windows Start Menu and Installed Apps. It even creates a shortcut, and your users will never have to install python themselves, or even know that your app is created with Python.
Appendix
Some explanations on your the problems you were facing.
A1) About launching a python script
When you launch a python script, you typically type python myscript.py. What happens is that your OS (Windows) will search through a list of folders for python.exe and when it finds it, it uses that python.exe to run your script. This list of folders is called the PATH environmental variable.
What the activate script (Activate.ps1 for powershell and activate.bat for cmd) does is it adds the folder of the virtual environment that contains the python.exe to the top of the path, so when you run
python myscript.py
The python.exe in your virtual environment (here pyenv/Scripts/python.exe) will be used, which guarantees also that the packages installed in your virtual environment are found.
A2) Running just myscript.py
On Windows, the standard Python Installer associates .py files with Python executable. I tested it and it seems to be the special Python Launcher for Windows (py.exe) that is used. So, when you just type
myscript.py
on command line, it will be run as <full-path-to-py>/py.exe myscript.py. Exactly the same thing happens if you just double-click the myscript.py in File Explorer. You can change the default program by right clicking a .py-file -> "Open With" -> "Choose application" -> Check "Always use this application", if you wish, but this does not help you with your problem.
If you want to run the script directly without using python before, then you have to tell the code that which program it should use to run this code.
Let's say if you have python installed at /usr/bin/python then you have to write in the python file which program to use. In this case, it is python.
So, the first line in the UpperLimb.py file will be #!/usr/bin/python. This line will tell the program to use the python program at /usr/bin/python.
After this, you need to make this script executable. You can use the following command to make this file executable.
$ chmod +x UpperLimb.py
Now, you can run this file as follows using the command ./UpperLimb.py
I'm trying to run a Python script from the command line as a command on Windows -- so no usage of "Python" or ".py". If my script is named "testing.py", I am attempting to make this name into a command and call "testing" from the command line.
Going through the docs it seems I need to use this shebang #!/usr/bin/env python as long as I have Python in my PATH.
https://docs.python.org/3/using/windows.html#shebang-lines
I also have the script folder in my PATH, so something like
"testing.py" is currently working from the command line.
According to the docs and this tutorial,
https://dbader.org/blog/how-to-make-command-line-commands-with-python
I should be able to evoke my Python script just by "testing" if I have the proper paths within PATH and the above shebang. However, I can't seem to get the script running withouth adding ".py".
The accepted answer by #AKX is incorrect for Windows 10 standard Python 3, certainly in the latest Windows 10 (1903) if not earlier.
(Note: I cannot speak to how this may or may not work under WSL.)
I have several versions of Python installed (2.7, 3.6, 3.7, and most recently Python 3.8b1). I've been using the #!/usr/bin/env shebang for years in my scripts for cross-platform compatibility (usually to distinguish Py2 vs Py3 scripts).
I've created a little script in a folder (C:\so_test\awtest.py):
#!/usr/bin/env python3.6
import sys
print(sys.version)
If I run this with awtest.py or just awtest I get 3.6.x reported (showing it's running with Python 3.6). If I change the shebang to refer to 3.7, I get 3.7.x reported. If I change the shebang to just #!/usr/bin/env python3 I get the latest version of Python installed (3.8).
Now, if I add that folder to my path (path=%PATH%;C:\so_test in the command window you're testing in, or in the main env vars (you will need to restart the command window if you do the latter though)), I can change to a different directory and run awtest or awtest.py and they still work and refer to the folder in the path. If I remove the script folder from the path these files are no longer found.
While I wouldn't necessarily expect this to work on Windows prior to 10 or Python 2.7, this functionality appears to be the way of things going forward.
No, Windows does not support shebang lines.
The documentation you've linked relates to the py launcher installed by Python, which can interpret various shebang lines to choose a Python version to run a script with.
setuptools is able to generate wrapper .exes for your Python scripts, but it gets a little involved and already assumes you have a package with a setup.py and so on.
Locally, if you really, really need this, you probably could add .py to the PATHEXT environment variable, so the Windows command line looks up .pys like it looks up .exes (and various others; the current modern default is .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC). However, this will naturally not scale for distributing apps, as all of your users would need to set that too.
My recommendation is to stick with just that boring old python testing.py, really.
You can use shebang in windows by setting the path of your interpreter as the first line in the file(you will see a marker on VSCode that says 'set as interpreter ' on that line).
Using windows 10,Python version 3.9 see example:
#!C:/Users/waithira/AppData/Local/Programs/Python/Python39/python.exe
print('hello world')
if you're not going to do this often. You can still add a batch file to your path testing.bat containing the command that you need to execute your code.
#echo off
python testing.py
It's a borring workaround but it works without needing to mention the extention since windows interpret batch files the same way it interpret executables.
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.
I'm trying to use Pypy to make my code run faster, but I don't know what to do with the zip file I downloaded from the site (I tried to read the directions but it moves too fast and I don't know what's going on). I was wondering if anyone had simple step by step instructions on how to install and use Pypy. Also, Im using Wing on Windows
Unzip the zip file to a suitable location, e.g. C:\pypy then run pypy.exe from the folder to which you unzipped. There is no installation for pypy. Similarly to uninstall just delete the folder.
Running C:\pypy\pypy.exe gives you an interactive prompt, just like the one you get for running any other version of Python except it uses four chevrons >>>>.
To run a script with pypy you can explicitly name the interpreter:
C:\pypy\pypy.exe script.py
Or, if you have Python 3.3 installed on the same system, you can use the Windows Python launcher. Edit the ini file (%USERPROFILE%\AppData\Local\py.ini) with %USERPROFILE% replaced by your home folder name to contain:
[commands]
pypy=c:\pypy\pypy.exe
Then you can simply put a hashbang line at the start of any script and it will automatically run pypy when you run the script from a command line e.g. script.py, or when you click on its icon:
#!pypy
import sys
print(sys.version)
or use #!python27 to make a script run with Python 2.7, or #!python33 to make it run with Python 3.3.
I have installed the new python release and would like to run .py files from the terminal.
How is this done from the terminal? I dont want to include the path in each command to run a .py file.
If you want to override the python command, you can set your PATH variable correctly, e.g. in your ~/.bash_profile:
export PATH=/path/to/python/:$PATH
That said, for managing different versions of components that are also provided by Mac OS X, I suggest to use a package manager such as Homebrew.
if you add a shebang at the start of the python file then you can run a python file by just its name from terminal
add #!/usr/bin/python
for mac(others add your respective path for python)
at the top of your python program and from your terminal you can run it just by filename(if it has executable permissions).
Have a look at the Python package under Applications. There is a shell script there called Update Shell Profile.command
Run this and it should set your path up properly.
Unless you mark you script as executable with chmod +x, you'll need to run python over it first. e.g. `python myscript.py'
I installed all of my python through macports, which has pros and cons. One of the benefits is that you don't have to worry about stuff like this, it just works. You can install python 2.6 and python 2.7 (and others), and then use the python_select utility to set up which python is run when you call "python blah.py"
Since you have installed a working python, the easiest way to run python files from the terminal is to cd your terminal to the directory where the file is located and then just type python my_code.py in the terminal.