1

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.

2
  • 3
    Doesn't "python filename.py" work? Commented May 3, 2011 at 21:18
  • 1
    If you want to use the newly installed version, say, 2.7, you probably have the corresponding command python2.7 available. Commented May 3, 2011 at 21:20

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It works just calling python program.py. Should the path still be set
1

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).

Comments

0

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'

Comments

0

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"

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.