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.
Related
I have some small utility scripts written in Python that I want to be usable on both Windows and Linux. I want to avoid having to explicitly invoke the Python interpreter. Is there an easy way to point shebang notation to the correct locations on both Windows and Linux? If not, is there another way to allow implicit invocation of the Python interpreter on both Windows and Linux without having to modify the script when transferring between operating systems?
Edit: The shebang support on Windows is provided Cygwin, but I want to use the native Windows Python interpreter on Windows, not the Cygwin one.
Edit # 2: It appears that shebang notation overrides file associations in Cygwin terminals. I guess I could just uninstall Cygwin Python and symlink /usr/bin/python to Windows-native Python.
Read up on the Python Launcher for Windows in the docs, which was initially described in PEP 397. It lets
you define custom shebang configurations in "py.ini" (e.g. to use pypy),
and out of the box you can use virtual shebangs such as #!/usr/bin/env python3, or shebangs with real paths such as #!"C:\Python33\python.exe". (Quoting is required for paths containing spaces.) You can also add command-line options to a shebang. For example, the following shebang adds the option to enter interactive mode after the script terminates: #!/usr/bin/python3 -i.
The installer associates .py (console) and .pyw (GUI) script file types with the respectively named launchers, py.exe and pyw.exe, in order to enable shebang support for scripts in Windows. For an all-users installation, the launchers are installed to the Windows folder (i.e. %SystemRoot%). For a per-user installation, you may need to manually add the installation directory to PATH in order to use py.exe in the shell (*). Then from the command line you can run Python via py -2, py -3, py -2.6, py -3.3-32 (32-bit), and so on. The launcher is handy when combined with -m to run a module as a script using a particular version of the interpreter, e.g. py -3 -m pip install.
(*) The new installer in 3.5+ defaults to "%LocalAppData%\Programs\Python\Launcher" for a per-user installation of the launcher, instead of installing it beside "python.exe", and it automatically adds this directory to PATH.
Unless you are using cygwin, windows has no shebang support. However, when you install python, it add as file association for .py files. If you put just the name of your script on the command line, or double click it in windows explorer, then it will run through python.
What I do is include a #!/usr/bin/env python shebang in my scripts. This allows for shebang support on linux. If you run it on a windows machine with python installed, then the file association should be there, and it will run as well.
Short answer:
The easiest way is to install git for windows wich comes with GitBash. Then add shebang lines in your scripts to indicate they should be run with python when executed.
#!/usr/bin/env python
More info:
Unlike Cygwin, git bash uses your native windows applications and lets you use bash scripts without any configuration.
It will automatically treat any file with a shebang line as executable the same way Linux shells do.
eg: #!/usr/bin/env php or #!/usr/bin/env node or any other application you want will work as long as you add the paths to your windows ENV path.
You can edit env vars in windows by hitting start and typing env should be the first option.
Git bash also installs git and hooks it up with a credentials manager for you and makes it super easy to sign into 2fa-enabled svn services and a ton of other handy developer features.
Git bash is IMO a must on every developer's machine.
Another option:
Install WSL (Windows Subsystem for Linux) which will work the same.
WSL also lets you install native Linux versions of all your command line applications if you prefer.
Linux binaries will take precedence over windows ones if installed but you can still choose to run the windows version of commands any time you want by specifically adding .exe on the end.
Install pywin32. One of the nice thing is it setups the file association of *.py to the python interpreter.
sorry for open old topic.
I create one file py.cmd and place it in the C:\Windows\System32 folder
py.bat:
#(
#set /p shebang=
)<%1
#set shebang=%shebang:#! =%
#%shebang% %1 %2 %3 %4 %5 %6 %7 %8 %9
py.bat file explain:
Get the first line from *.py file
Remove shebang characters "#! "
Run python file using shebang python path
All windows python script must start with shebang line as the first line in the code:
#! c:\Python27\python.exe
or
#! c:\Python37\python.exe
Then run it:
cmd> py SomePyFile.py param1 param1 paramX
Not with shebang ... but you might be able to set up a file association, see this SO question which deals with Perl and the associated answers which will also be pertinent as there's known problems with Windows and stdin/out redirection...
I want to be able to run a python script at the command line using Enthought Canopy, but I don't want to specify the full path to the script.
As I see it, there are two options.
Option 1: Make the python script an executable, add #!/usr/bin/env python to the top of the script, and put the directory containing the script on my $PATH. Now I can execute the script like this:
$ run.py
Option 2: As suggested by Andrew Clark in another SO post, just put the directory containing the script on my $PYTHONPATH. Then I can execute the script like this:
$ python -m run.py
The -m causes python to search the $PYTHONPATH.
I prefer Option 2, and it works fine with the system python on my mac (v2.7.2), but I cannot get it to work with Enthought Canopy. I can load Canopy python and import modules in the same directory as run.py, so I know that I have the path correct. I just cannot execute the script from the command line. Is this a bug or am I doing something wrong?
BTW, it's probably a typo, but just to make sure you should be using the module name, not the file name, with the -m option. For example, python -m run
If that is not the problem then make sure that the python that is used in your option 2 is the python located in your Canopy User virtual environment. You can use the which command to verify that. For example:
$ which python
/Users/YourUserId/Library/Enthought/Canopy_64bit/User/bin/python
If that is not what you get then you can either add that bin folder to the beginning of your PATH environment variable, or you can activate that virtual environment like this:
source /Users/YourUserId/Library/Enthought/Canopy_64bit/User/bin/activate
I have 2 versions of python installed on windows, 2.7.3 and 3.3. Some of my scripts are 2.x and some 3.x. Is there an easy way when executing these scripts from a command line to direct them to the appropriate interpreter?
Note: For Windows use the new Windows Python launcher (available with Python 3.3 and downloadable here for earlier releases) which recognizes Unix shell shebangs. You can read about it here.
Most Linux distributions will create python2 and python3 aliases for the installed Python 2.x and Python 3.x interpreter (if not you can just create the symbolic links yourself anywhere on your $PATH, the env command will take care of finding them), so you should just need to set the appropriate interpreter as the first line of your script:
#!/usr/bin/env python2
or
#!/usr/bin/env python3
This will direct the shell to use the appropriate interpreter, if you set the script files to be executable and just invoke them directly on the shell. E.g.:
$ chmod +x script.py
$ ./script.py
Try this first: I am on OS X, but when I want to use Python 2.6 instead of Python 2.7 (its a numpy/scipy thing), I simply run python2.6 whatever.py to run whatever.py in Python 2.6. Try that first.
If that doesn't work, then you can use virtualenv - the virtual environment builder for Python.
http://pypi.python.org/pypi/virtualenv
I am sure there are similar alternatives, too.
Pedro Romano's answer is the most elegant way of doing this.
But if you don't want to download and install the Python launcher, create a batch file as described below. You can also create a shortcut, copy C:\Python27\python.exe to C:\Python27\python27.exe, etc.
I'm guessing C:\Python27 and C:\Python33 are already on your system path. If so, you can create a batch file called python2.7.bat in C:\Python27\ which contains:
C:\Python27\python.exe %1
and a similar file (e.g. python3.3.bat) in C:\Python33\
Now, you can run python2.7 script.py from anywhere in command prompt and it should work :)
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.
I have virtualenv installed on windows.
In cmd, i run python and look at sys.path and see the virtualenv path included.
but when i run manage.py (for django), I don't see the virtualenv path,
so virtualenv is not working with django server.
Why?
the solution is to explicitly invoke python by using: python file.py
as described in http://www.velocityreviews.com/forums/t727997-problems-running-virtualenv-under-windows.html
for some reason, the python registered with .py in windows does not invoke virtualenv.
Virtualenv modifies the PATH to include a Python with the correct setup. It's a completely separate program from the system Python.
The PATH is used to look up programs by name: the first program of a given name that's in the PATH gets executed.
When you “run a file”, Windows uses the extension of the file to look up a program to run. It doesn't look it up by the name of the program, and so doesn't check the PATH.
The solution is to explicitly invoke Python from the command line (python manage.py) while a virtualenv is active. This way, Windows will search PATH for what you meant by “python”, and find the correct one.
have you done:
> source ../path-to/bin/activate
?