How do I run a .py script directly from cmd? - python

Suppose I have a script called 'run.py', how can I do this?
C:\Users\Administrator>run
And this script will be executed. Please notice that I don't want a '.py' shown after 'run'.
I am using python 3.3.5, and I tried putting 'C:/python33' into environment variable path. But it didn't work.
It seems like it only works when it's a '.exe' file.
Thanks in advance.

You will need to add .py to your PATHEXT environment variable.
From the Python on Windows FAQ, 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.
The exact method for setting environment variables varies with different versions of Windows, but this link can probably help you out.

Related

Can a Script be an Environment Variable

I have a Python script that I would like to be able to execute from no matter where. Either in Linux or in Windows, but in this case preferably in Windows. Putting the path to the script into PATH under Windows did not work, so from some directory calling python my_script.py results in the message that there is no such file in this directory. So, is this somehow possible?
You can try creating an alias as such:
Linux
Create a .bash_aliases file on your home folder
Add an alias such as alias pscript='python /home/pythonscript.py'
Log out and back in or do a source .bash_aliases
Windows
Run doskey pscript=python C:\script.py. Read more here
The PATH is used to search for executables, and this doesn't include in windows script files.
A workaround is to convert the script to a batch file, see here
how to simply have the script act also as a batch file
For the operating system to run your script, it needs to find it (the PATH variable), recognize that it is executable and know which program should execute it. You seem to have the PATH part handled, so now for the other two.
On unixy systems you need to make the script executable. To set the user-executable bit, do chmod u+x myscript.py. Then you need to tell the system which program should run it. Typically you use the "shebang" as the very first line in the file:
#!/usr/bin/env python3
The system will search the path for a program called "python3" (use "python" for python 2 scripts) and use that executable to run the script.
On Windows, you need to associate the file extension (.py) with the python exeuctable. That's usually done for you when python is installed. If not, you can dig into ftype, assoc and pathext here.
Windows doesn't care about the shebang (unless you are running cygwin, then see unixy systems above) so the same script can live in both worlds.
Once the script is executable, you call it directly instead of executing python and giving the file as the script name. Its just
myscript.py

How does python know to run from the command line?

Hi I noticed that whenever from the command line (using windows 8.1) I type
python file.py
It automatically knows that I meant to write python.exe file.py
How does it do this?
I installed Anaconda, and I understood I have an environment variable pointing to python.exe. But that doesn't explain why I need not type python.exe everytime.
This is not a python feature. The behavior to call executables without file extension is defined by the operation system and the PATH variable. Wikipedia has a good answer to your question
PATH (variable)
...
When a command is entered in a command shell or a system call is made by a program to execute a program, the system first searches the current working directory and then searches the path, examining each directory from left to right, looking for an executable filename that matches the command name given.
...
The file name after the executable could be everything. So if you want you can call python demo.txt. If the file content is readable for python it would also be executed.
Python is searched for using the path. Find the PATH in the list of environment variables.
If you do not write python and simply double-clicks the file, the registry is searched.
You can see what and associate files with program using ftype and assoc from the command line. See e.g. http://www.fileformat.info/tip/microsoft/assocftype.htm
If you omit the extension the registry is also searched. This cmd shell searches the environment variable PATHEXT and the registry for finding python.exe. After this, it is using the registry to find the location.
When you register your python distribution from within Spyder, these changes are made.
You can reveal this information by using ftype and assoc like I wrote in the beginning.

Getting custom files to launch like .exe in Windows

How does Windows know that an .exe application is an .exe application. Also, how would you tell windows to send this type of application to this program, that you have created. Like how Python programs are .py and are text files, but when you click on it, it acts like an executable. Or is it the other way around? I don't know. Any type of help would be useful, thanks.
In Windows this is different.
There is a variable called PATHEXT in the environment, like this
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY
Notice that I've added the .py extension to mark python scripts to be executable. That is the first step.
Then, you need to tell Windows which program will run this script. For that, you have the assoc and ftype commands.
When you type assoc you get the list of known file extensions. Here, I get .py=Python.File as output. Now you have to connect this extension to a program, ie. the python executable python.exe. Do this with ftype:
D:\\>ftype Python.File
Python.File="C:\Program Files (x86)\python27\python.exe" "%1" %*
I think that the correct calls to assoc and ftype are done when installing python. Then you only have to append the extension to PATHEXT.
A .py file doesn't execute when you write $ ./file.py unless you add #!/usr/bin/env python or C:\\Python27\python.exe something like that at the very beginning of it. The OS tries to execute the following: the_thing_you_wrote_after_# file.py If you change the extension to .lold it will work too.
If you try to do ./file.py without adding the line I was talking about earlier, the current shell'll try to parse the text and will definitely give errors.
I'm not quite sure, but Windows checks if it's possible to run a file by checking its extension first. It also searches for the entry point. If you rename your test.exe to test.test it won't work.
You can't launch a .py file like an app unless you use special tools like py2exe and cxfreeze.

How to import modules from alternate locations when using Python IDLE?

I've been trying to figure this out for more than 2 days, screening the internet and the tutorial, but yet I don't have solved my problem. I'm a real newb and don't yet really know what I'm doing..
Software I use:
Mac OS X 10.6
Python v3.2.2
Interactive interpreter (IDLE)
Problem:
IDLE's default directory is /Users/ME/Documents/. Files with the extention .py can only be opened when located in this directory. However, I made a folder where I would like to save all the .py files etc that have to do with this software. Currently, IDLE cannot load .py files from the chosen directory by me.
What I did first was I added to IDLE:
import sys.
sys.path.append('Users/Mydir/')
sys.path
However, in an already existing thread from 2010 I read sys.path is for the Interpreter ONLY, and that if I am to change this I need to modify the PYTHONPATH environment variable:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
However, I'm confused how to use this and cannot find answers to my following questions:
1) PYTHONPATH (.py?) is already existing on my computer when I installed the program?
If YES, where is it? I cannot find it anywhere.
If NO, I need to create one. But where and what should be the content so that IDLE can load files from a non-default directory? Should it contain only the words in bold?
I hope I made my problem clear.
Cheers
It's not totally clear to me what you mean by load. That could mean Open and Close files in the IDLE editor. Or it could mean being able to use the Python import statement to load existing Python modules from other files. I'll assume the latter, that by load you mean import.
There are two general ways to launch IDLE on Mac OS X. One is from the command line of a terminal session; if you installed Python 3.2 using the python.org installers, by default typing /usr/local/bin/idle3.2 will work. The other way is by launching IDLE.app from /Applications/Python 3.2, i.e. by double-clicking its icon. Because you say the default directory for files is your Documents folder, I'm assuming you are using the second method because IDLE.app sets Documents as its current working directory, which becomes the default directory for *Open*s and *Save*s and is automatically added as the first directory on Python's sys.path, the list of directories that Python uses to search for modules when importing.
If you want to add other directories to sys.path, as you've noted you can use the PYTHONPATH environment variable to do so. The standard way to do this is to add an export PYTHONPATH=... definition to a shell startup script, like .bash_profile. However, if you use IDLE.app, no shell is involved so commands in .bash_profile have no effect.
While there are ways to modify the environment variables for OS X GUI apps, in this case, a simpler solution is to use the other method to invoke IDLE, from the command line of a shell session, using either /usr/local/bin/idle3.2 or, if you've run the Update Shell Profile command in the /Applications/Python 3.2 folder (and opened a new terminal session), just idle3. Then, a PYTHONPATH environment variable you set up will be inherited by that IDLE.
BTW, there is no direct way to modify the initial current working directory of IDLE.app from Documents other than modifying the code in IDLE. If you start IDLE from a command
line, it inherits the current working directory of the shell.
[UPDATE] But rather than fooling around with defining PYTHONPATH, here is another even simpler, and probably better, approach that should work with either IDLE.app or the command line idle. It takes advantage of Python path configuration (.pth) files and user site-package directories. Assuming you are using a standard Python framework build of 3.2 (like from a python.org installer) on Mac OS X, create a path file for the directory you want to permanently add to sys.path. In a terminal session:
mkdir -p ~/Library/Python/3.2/lib/python/site-packages
cd ~/Library/Python/3.2/lib/python/site-packages
cat >my_paths.pth <<EOF
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_1
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_2
EOF
Now, whenever you run that Python 3.2 or IDLE under your user name, the directories you have added to the .pth file will automatically be added to sys.path.
BTW, the exact path location of the user site-packages directory for versions of Python earlier than 3.2 or 2.7 may be slightly different. Also, on other Unix-y systems, the default location for the user site-package directory is ~/.local/lib/python3.2/site-packages.
PYTHONPATH is an environment variable (see here and here). I don't have a Mac, but from the threads I have linked to you would type something like
launchctl setenv PYTHONPATH=/Me/Documents/mydir:$PYTHONPATH
on the command line to allow you to run Python scripts from /Me/Documents/mydir. Alternatively, put this line in a file called .bashrc in your home directory (~) and this path will be set each time each time you open a terminal. See here for a short introduction to .bashrc and other .bash* files. Hope that helps.
EDIT See also this question.

How to set python enviroment variable on windows

I've downloaded python installer from http://www.python.org/ftp/python/3.1.2/ , this python-3.1.2.msi file, I need to execute some python files? How do I do that? For example in php I'd do php filename.php from console, I do however have python command line but I don't know how to execute those files.
So if I could set ENV variable to directly execute my file(s) if that is possible that would be great.
There is an option in the installer called "Register Extensions" to associate Python files with the interpreter, so double-clicking them or entering filename.py in the console should work.
Apart from that you might want to add C:\Python31 to your PATH variable (right-click on My Computer, choose Settings, choose the Advanced Tab - there you can access the system variables. Better do this as an admin.
If you type python in the Windows command line, what happens? Is the Python interpreter in your PATH yet?
If not, add the Python installation directory there (here's a good guide). Then just do python script.py just like with PHP.
you can just execute
python yourfile.py
Or if the python command don't work you have to give the absolute path to you python installation or add it to windows path

Categories

Resources