This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Set up Python on Windows to not type python in cmd
When I use python on Linux, or even Mac OS from command line, I take advantage of the shebang and run some of my scripts directly, like so: ./myScript.py. I do need to give this script executable permissions, but that is all.
Now, I just installed Python 3.1.2 on Windows 7, and I want to be able to do the same from command line. What additional steps do I need to follow?
This sums it up better than I can say it:
http://docs.python.org/faq/windows.html
More specifically, check out the 2nd section titled "How do I make Python scripts executable?"
On Windows, the standard Python installer already associates the .py extension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as foo.py. If you’d rather be able to execute the script by simple typing foo with no extension you need to add .py to the PATHEXT environment variable.
Related
This question already has answers here:
How can I convert a .py to .exe for Python?
(8 answers)
Closed 1 year ago.
i was making a program for command line and it was very easy to make it a command in other operation systems. but i tried to make another version of that program in windows. i turned the script into a .exe file using pyinstaller and then i searched for making it a command so i can run it from windows command prompt but the only way that i found was to move the script to system32 folder. i have config files and also it doesn't make sense to put a script in system32.
is there any other way to do that?
If I understand correctly, I think that you could create a folder for your custom commands and add it to the PATH environment variable on Windows, that way, you could just open cmd, call the command and it will be recognized and executed.
To run your script from the command line outside of the directory that the script is in, your command line needs to know where your script is.
One of the places the command line will look is in system32, which is why someone told you to put it there.
You can update your environment PATH to point to the location of the python script to do what you want to do.
This question already has answers here:
What do I use on linux to make a python program executable
(8 answers)
Closed 6 years ago.
Suppose I have a hellp.py python file. I found that I only can type command: python hello.py to run my command line, but how can I just type ./hello so that my system can run my hellp.py file?
If you want to run your Python script as an executable, put the right shebang at the top of your .py file. For example, hello.py might be defined as:
#!/usr/bin/env python
print('hello, world!')
Then you'll need to ensure the execute permission is set on the file:
$ chmod u+x hello.py
When you run ./hello.py, the interpreter currently on PATH (including one in a virtualenv if one is activated) will run the Python script.
There are other ways to do this as well with tools like PyInstaller, Py2App, Py2Exe, but I think this is what you're looking for.
EDIT: I noticed that the question specified wanting to just type ./hello at the command line. There are a couple of ways to tackle this. The first would be to simply knock off the file extension and have the Python file simply named hello. The second (and the way I'd recommend) would be to create a link to the file. I'd recommend creating a symlink:
$ ln -s hello.py hello
This way, the code is still obviously a Python file, but you can access it conveniently.
This question already has answers here:
How to run multiple Python versions on Windows
(22 answers)
Closed 6 years ago.
How can I configure windows command dialog to run different python versions in it? For example when I type python2 it runs python 2.7 and when I type python3 it runs python 3.3? I know how to configure environment variables for one version but two? I mean something like Linux terminal.
I also met the case to use both python2 and python3 on my Windows machine. Here's how i resolved it:
download python2x and python3x, installed them.
add C:\Python35;C:\Python35\Scripts;C:\Python27;C:\Python27\Scripts to environment variable PATH.
Go to C:\Python35 to rename python.exe to python3.exe, also to C:\Python27, rename python.exe to python2.exe.
restart your command window.
type python2 scriptname.py, or python3 scriptname.py in command line to switch the version you like.
Python 3.3 introduces Python Launcher for Windows that is installed into c:\Windows\ as py.exe and pyw.exe by the installer. The installer also creates associations with .py and .pyw. Then add #!python3 or #!python2 as the first lline. No need to add anything to the PATH environment variable.
Update: Just install Python 3.3 from the official python.org/download. It will add also the launcher. Then add the first line to your script that has the .py extension. Then you can launch the script by simply typing the scriptname.py on the cmd line, od more explicitly by py scriptname.py, and also by double clicking on the scipt icon.
The py.exe looks for C:\PythonXX\python.exe where XX is related to the installed versions of Python at the computer. Say, you have Python 2.7.6 installed into C:\Python27, and Python 3.3.3 installed into C:\Python33. The first line in the script will be used by the Python launcher to choose one of the installed versions. The default (i.e. without telling the version explicitly) is to use the highest version of Python 2 that is available on the computer.
I would suggest using the Python Launcher for Windows utility that was introduced into Python 3.3. You can manually download and install it directly from the author's website for use with earlier versions of Python 2 and 3.
Regardless of how you obtain it, after installation it will have associated itself with all the standard Python file extensions (i.e. .py, .pyw, .pyc, and .pyo files). You'll not only be able to explicitly control which version is used at the command-prompt, but also on a script-by-script basis by adding Linux/Unix-y shebang #!/usr/bin/env pythonX comments at the beginning of your Python scripts.
This question already has answers here:
Create a single executable from a Python project [closed]
(3 answers)
Closed 8 years ago.
Writing some project on Python via PyCharm.
I want to get an exe file from it.
I've tried to "Save as->XXX.exe" - but ,when i'm trying to execute it there is an error "file is not supported with such kind of OS"
p.s.
i've got win7 x64,it doesn't work on x32 too.
You cannot directly save a Python file as an exe and expect it to work -- the computer cannot automatically understand whatever code you happened to type in a text file. Instead, you need to use another program to transform your Python code into an exe.
I recommend using a program like Pyinstaller. It essentially takes the Python interpreter and bundles it with your script to turn it into a standalone exe that can be run on arbitrary computers that don't have Python installed (typically Windows computers, since Linux tends to come pre-installed with Python).
To install it, you can either download it from the linked website or use the command:
pip install pyinstaller
...from the command line. Then, for the most part, you simply navigate to the folder containing your source code via the command line and run:
pyinstaller myscript.py
You can find more information about how to use Pyinstaller and customize the build process via the documentation.
You don't necessarily have to use Pyinstaller, though. Here's a comparison of different programs that can be used to turn your Python code into an executable.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I run a python program in the Command Prompt in Windows 7?
This is a follow-up to this question: Run a python script in windows.
How would I do the equivalent of
`$ ./checksum.py <folder>
in Windows? Note, the checksum.py file starts thus:
#!/usr/bin/env python
For me, it works just to invoke the name of the script directly, e.g. > myscript.py.
if you have python installed on your system just make sure it's in the global variables.
Then you can type in "python " eg "python myscript.py abcd".
If it's not registered at global level you have to 'cd' (ChangeDir) to the location where python is installed, then run a command "python " eg:
"C:\Programs\Python>python C:\Users\User1\Desktop\MyScript.py abcd" where "C:\Programs\Python" is the current working directory.
If you want to run linux programs and commands on windows you can try MinGW or CygWin.
One potential solution to this problem, while possibly overkill, is to install Cygwin and use its environment to run the script. Of course you can just call the python command from your Windows command line (as long as it's in your PATH, as specified in autoexec.bat) followed by ./checksum.py [folder], but if you're coming from a *nix/OS X environment, you may find Cygwin makes your life simpler. Either way.
Make sure the filename extension .py is associated with the appropriate python.exe. Similarly, .pyw should be associated with pythonw.exe (this is a version of the Python interpreter that doesn't show a terminal window, suitable for use with Python GUI scripts).
The Python for Windows installer does this, so you usually won't have to mess with it unless you have multiple Python installs on your machine. If you do need to change the association, this can be done by right-clicking a .py file, choosing Properties, and clicking the Change button next to "Opens with."
Windows ignores the shebang line, so there is no way (short of Cygwin) to have different scripts use different versions of Python by changing the shebang. You could use a different extension (e.g. .py3 for Python 3 scripts) and associate that with C:\Python31\python.exe -- but that will break the script's ability to be imported as a module (Python expects the .py extension), so use it carefully. Better practice is probably to just specify the desired python.exe directly on the command line.