I'm starting on a Python 3 project, so I'd like to configure Emacs' run-python command to use the python3 interpreter by default.
I don't want to
change python so that it points to python3 instead of python2.7 (because that might cause various bad things)
pass "python3" as an argument to the run-python command each time I run it (because that sounds annoying)
I've tried setting python-which-shell to "python3" in my .emacs, but this doesn't seem to work.
On Emacs 24.3, add to your .emacs:
(setq python-shell-interpreter "python3")
If this doesn't work on an older Emacs with a different Python mode, try M-x find-function RET run-python RET to inspect the source for an equivalent variable that can be set.
You can use the shebang line to specify which version you want to run:
#!/usr/bin/env python
Will use whichever one the system considers "python"
#!/usr/bin/env python2.6
#!/usr/bin/env python2.7
#!/usr/bin/env python3.2
Will run the specified version if you have more than one installed.
A friend taught me this when our Ops guy set up a VM with 2.6 and 2.7 installed, and I required 2.7, while the CentOS package manager required 2.6. Saved me a lot of having to go back and make things 2.6-compatible.
Related
Just curious, is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux? Is there some kind of way to configure Python so that it runs like this? Thanks.
EDIT: Just to add, the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
To run Python 2 executable:
C:\> py -2
To run Python 3 executable:
C:\> py -3
where py is a Python launcher that is bundled with your Python 3 installation.
py recognizes the shebang (e.g., #!/usr/bin/env python3 causes Python 3 executable to be run), it respects virtualenv (if you run py without specifying the explicit python executable version) i.e., run:
C:\> py your_script.py
and the correct python version is used automatically -- you don't need to specify the Python version on the command-line explicitly.
is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux?
OSX and Linux have python executable installed by default as a rule and it refers to Python 2 version in most cases at the moment that is why you need a separate python3 name there.
There is no Python on Windows by default. And therefore any version that you've installed is just python (I guess). The recommended way to manage multiple python versions is to use the Python launcher.
Is there some kind of way to configure Python so that it runs like this?
If you want to type python3 some_script.py instead of py some_script.py or even just some_script (assuming .py is in %PATHEXT% and Python launcher is configured to run Python scripts (check assoc .py and ftype Python.File) -- the default) then create a bat-file e.g., python3.cmd and put it in %PATH%:
"C:\path to\Python 3.X\python.exe" %*
You likely missed the checkbox at the bottom of the installer.
Full documentation here: https://docs.python.org/3/using/windows.html
Then, I think you just run python, not python3 from the Command Prompt. The reason Unix systems have python3 is because python defaults to Python2.x in many systems.
You have to add the python bin folder to your path. You can do it manually but when you install python i remember you have an option to do that.
I work with multiple Python 2.x and 3.x distros on Windows. Some of them are "portable" - i.e. not recorded in the Windows registry, and therefore not accessible by the version-selector py.exe delivered with Python 3.3+. To save my sanity, I wrote SelectPython.bat which is available on bitbucket. It configures the PYTHONHOME, PYTHONPATH and PATH variables according to the target you give it (a relative or absolute path to the parent directory of python.exe). You can do so in a way that is sticky for the rest of your command-line session:
> SelectPython C:\Path\To\Desired\Version\Of\Python
> python
or transiently, i.e. to call a particular python command without otherwise affecting the environment of the shell you're calling it from:
> SelectPython C:\Path\To\Desired\Version\Of\Python python -c "import sys;print(sys.version)"
You may find it helpful.
I have 2 versions of python installed on my server.
Python 2.6 in /usr/bin/python
Python 2.7.8 in /usr/src
Python 2.7.8 install guide
How do I make python scripts use 2.7.8? instead of 2.6?
how do I set up an alias?
Thanks
Write:
#!<path to your python interpreter>
as the first line of the script your run. Then just execute it.
Other option:
<path to your python interpreter> your_script.py
There are multiple ways of doing this. First, I would suggest you check your $PATH environment variable to see if it holds the path to both of your python versions. If it doesn't, you can either add the path or simply make a symlink to it from /usr/bin/ and call it python2.7, for example.
Then you can use #Pavel's suggestion of specifying the path to the interpreter in the shebang of the file.
If you followed my previous instructions, your shebang should look something similar to the following.
#!/usr/bin/env python2.7
You could also use the second way #Pavel showed you, by simply calling the interpreter first in the terminal and pointing it to the script.
/usr/bin/python2.7 my_script.py
The best option you have, which I recommend, is using virtualenv as suggested before, but only if you want it for specific scripts. If you want it globally, I would suggest you read further.
Finally, if you want all your scripts to run under python 2.7 then I suggest you remove python 2.6 and install the 2.7 version from the package manager, the right way, and save yourself a lot of hassle.
I recently installed epd python distribution in ubuntu. This got installed in the folder /home/jai/Downloads/epd_free-7.3-2-rh5-x86_64
Can you tell me how to make this python as my default python?
I get errors while running a test program (it seems my default python is different and it doesn't have numpy library, other libraries that come along epd python distribution.)
My test program is here: http://www.southampton.ac.uk/~fangohr/computing/download/python/tests/testall.py
The "default" python depends on how you're invoking it.
On Ubuntu, python is normally installed as /usr/bin/python (not /bin/python) -- which may be a symbolic link.
If you invoke the python command, e.g.:
$ python myscript.py
it will use whichever python executable is in a directory that appears first in your $PATH. You can modify your $PATH, either for your current shell:
export PATH="/some/dir:$PATH"
or for all future shells by updating your $HOME/.bashrc, $HOME/.bash_profile, or whatever. /usr/local/bin is one common place to put system-specific executables, or $HOME/bin for user-specific executables.
If you want to execute the script itself, you'll need a shebang as the first line of the script:
$ head -1 myscript.py
#!/usr/bin/python
$ ./myscript.py
...
You can edit the shebang to refer to whatever Python executable you want to use.
You can replace /usr/bin/python with your preferred Python executable, but that might cause unwanted side effects; existing Python scripts that assume /usr/bin/python is the default might break.
Another option is to change the shebang to:
#!/usr/bin/env python
which lets you execute the script directly while still using whichever python is first in your $PATH. This may or may not be a good idea; see my answer to this question for further discussion.
Default python is the one found in /usr/bin directory with the name python. Making a symbolic link of :
ln -s /home/jai/Downloads/epd_free-7.3-2-rh5-x86_64 /usr/bin/python
Assuming that is the name of the python executable, not the installer. After you installed, use the path where you installed it. f.e /home/iai/myNewPythonInstallation
might do the trick.
Most likely default 2.7 python is occupying that name, so you need to remove that, or use another name like epdPython. Then running python scripts would happen with:
epdPython myscript.py
I am using a module that can only be found in python 2.7, so when I run my script I have to specify python2.7 script instead of just script. I know there are bigger issues out there, but still I was wondering -
is it possible, when writing a python script, to set the interpreter to 2.7 by default?
Maybe by setting #! /usr/bin/env python for example?
Most unix environments will have the python2.7 executable, such that you can write:
#!/usr/bin/env python2.7
Obviously this doesn't help much on windows. You can also at least check the python version once you are started, although it won't help you run the later version if it is available:
import sys
print sys.version_info
...really do checks here...
Using the shebang is an appropriate way to specify this, yes. Find out where python2.7 is in your PATH using which python2.7. This is e.g. /usr/bin/python2.7. You then set the shebang:
#!/usr/bin/python2.7
Of course, this only works on those systems where Python 2.7 is available at this location :) This will also work on may systems: /usr/bin/env python2.7
Then make the script executable and fire it off using $ ./wonderful_python_script.py.
If you are using Windows, install Python 3.3, which features a new launcher that provides shebang support under Windows. This will give you a (finally!) sane and cross-platform way of explicitly specifying interpreter versions.
I'm running Ubuntu to compile a set of code which requires python 2.4.
How can I setup a terminal launcher so that when I open that launcher all python related commands will use python 2.4 instead of the python 2.6 that is defaulted in Ubuntu?
Set a bash alias in that shell session: alias python=python2.4 (assuming python2.4 is in your $PATH of course). This way you won't have to remember to explicitly type the 2.4 a zillion times in that terminal -- which is what bash aliases are for!-)
Invoke the interpreter via python2.4 instead of using the default.
For a permenant system wide change put a symbolic link to the version you want in place of /usr/bin/python. ie
rm /usr/bin/python;
ln -s /usr/bin/python2.4 /usr/bin/python
gentoo has a program 'eselect' which is for just this kind of thing (listing versions of programs and setting the default), Ubuntu may have something analogous; you'd have to check their docs.
Watch out for using the alias when wanting to use the python you want. If the python script uses $0 to figure out, how it was called, then uses that answer to execute another python script. The other script will be called with whatever version that matches the link name, not the version the link points to.