Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I set the default python for running py files without "python" keyword i.e $ django-admin.py manage.py startproject" in bash shell?

share|improve this question

1 Answer 1

That depends on the first line in the .py file (the "shebang line"). Commonly you'll see something like this:

#!/usr/bin/python2.6

In which case, the script when run as a standalone executable will run using the version of Python at /usr/bin/python2.6. Why this version of python? It might have been hard-coded into the script, but more likely it was set automatically when you installed the script. E.g. when you installed Django you either:

  1. Downloaded the source and ran python setup.py - in which case the script gets installed to run with the version of python you ran setup.py using
  2. Ran easy_install django - in which case the script gets installed to run with the same version of python that easy_install (also a Python script) uses. (This is why you'll often see multiple versions of easy_install listed, e.g. I have easy_install-2.5, easy_install-2.6 and easy-install-2.7 available. When I just type easy_install it uses easy_install-2.6.)
  3. Ran pip install django - same deal as with easy_install, pretty much.

To run the script with another version of Python you can change the shebang line (e.g. to read #!/usr/bin/python2.7) or specify your python on the command line (e.g. /path/to/python2.7 /path/to/django-admin.py args). Note however that scripts such as django-admin.py are just a front-end to a larger set of libraries (in this case, Django), and those libraries will have been installed by default in the library location for a specific version of Python. For example, if the first line of django-admin.py indicates that it's running under Python 2.6, the Django libraries will have been installed in a Python 2.6-specific library directory, and they won't be available by default to e.g. Python 2.7.

In other cases you'll see a shebang line like this:

#!/usr/bin/env python

In which case, the script will run with whichever version of Python you get when you just type python at your command line. You will be able to change the version of python that scripts with this kind of shebang line have by, for example, altering your PATH environment variable so that your shell finds python in a different directory.

In either case, you should consider checking out virtualenvwrapper, an extension to virtualenv. It allows you to quickly and easily set up multiple virtual environments for Python projects, each with its own default Python version and libraries.

Update: since your question mentions OS X it's also worth mentioning that in OS X, /usr/bin/python runs a user-configurable version of Python. See the OS X python manpage for details.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.