TextMate seems to use the built-in Python version I assume (sys.path doesn't work). How do you configure it to use 3.1 instead? I've already installed the 3.1 package and I can use IDLE for interactive sessions, but I need to use TextMate now.
Thanks
TextMate uses the value of the TM_PYTHON variable to find the path to the Python interpreter. A good solution is to take advantage of TextMate's ability to define variables like TM_PYTHON on a per-project basis:
Open a new or existing TextMate Project (File -> New Project or File -> Open)
De-select any file in the project list sidebar.
Click on the Get Info (i) icon in the sidebar. A Project Information pane appears.
Click + to add a new variable.
Enter TM_PYTHON in the Variable field and the full path to the desired python in the Value field (for example, /usr/local/bin/python3.1).
Close the Information window and save the Project (File -> Save Project As).
Then you can add files as needed to the project and they will be run under the chosen python with TextMate Python bundle's Run Script command. You might want to save a Python 3 project, say, for running ad-hoc scripts under Python 3. For bigger projects, you'll want to create a separate TextMate project for it anyway.
To change the Python version used globally within TextMate:
From the TextMate menu bar, open TextMate -> Preferences
click on the Advanced pane
click on the Shell Variable tab
click the + to add a new variable
enter TM_PYTHON in the Variable field and the full path to the python in the Value field (perhaps /usr/local/bin/python3.1)
As Alex points out, you may break other TextMate functionality by changing the Python version globally so the per-project change is probably a better solution.
UPDATE (2010-10-31):
There is another approach that may be easier to use for some projects. The Run command in TextMate's Python bundle appears to respect a shebang line in the file being run. So, instead of modifying TM_PYTHON, you can specify the path to the interpreter to be used by including a first line like this:
#!/usr/local/bin/python3.1
# sample code to show version
import sys
print(sys.version_info)
In many case you would prefer not to hardwire the absolute path but manage use through the normal shell PATH environment variable. Traditionally /usr/bin/env is used for that purpose. However when running under TextMate, your shell profile files are not normally used so any changes to PATH do not show up there including possibly /usr/local/bin or /opt/local/bin or wherever your desired python3 command is located. To get around that, you can add or modify a global PATH shell variable to TextMate -> Preferences (see above) with a value of, say, /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin. Then you can use a more general shebang line like this:
#!/usr/bin/env python3
(This all seems to work with the most recent vanilla TextMate and its Python bundle: no guarantees about earlier versions or with other Python bundles.)
Late to the party, sorry! I take it you want to run the script using TextMate's 'built-in' interpreter? I've found the simplest solution is to add a shebang, which works extremely well;
#!/usr/bin/env python3
for python 3.1 or;
#!/usr/bin/env python
for default system python, although the latter is superfluous for the exercise.
According to this long thread (which was about Python 3.0, and the TextMate version existing back last spring, but I believe is still valid for Python 3.1 and today's TextMate), you can get it done (e.g. via #Ned's answer), but once you do many TextMate commands may well break (because they're written to use Python 2, and Python 3 is not backwards compatible with Python 2 -- for example, the use of reload, which disappeared in Python 3, is repeatedly mentioned in the thread). Still, it might work if you do not use or need much of TextMate's functionality (LaTeX typesetting for example was mentioned as something that totally breaks once you make TextMate use Python 3 instead of Python 2).
the shebang is the best solution, to see where python 3 is installed type in terminal:
which python3
you will get something like this:
/usr/local/bin/python3
if nothing shows up first install python3
and at the top of your script insert:
#!/usr/local/bin/python3
I wanted to achieve this in TextMate version 2.0.23 with the aim to use python3 as the default.
So this is how I could set the TM_PYTHON variable (based on #Ned Deily's answer above):
Open Textmate
Hit CMD + ; to open the settings
Now Add a new Variable with the + sign
4. In the Terminal.app I typed which python3 which gave me /usr/local/bin/python3.
Now whenever I open a python script and hit CMD + R it will execute as a pyhton3 script by default while "honouring" the installed PIP packages.
Related
On Windows 10:
Hi, I am having an issue with understanding how my computer works with python. When I run a python file in my IDE, Atom, it uses the python from PATH. When I run python in .cmd, it uses the python from PATH. When I execute the same python file from .cmd it uses a python that is not in PATH. Why is it doing this and how can I get it to use the same python? I want everything to use the anaconda python.
I don't understand where the \AppData\ python is coming from?
![
]4
What you see in Control Panel is just a template (if you will). Every process gets those settings when it starts, but it can modify them afterwards. Take the following example:
Start cmd.exe:
1.1. echo %PATH% - will output the inherited value
1.2. set PATH=%PATH%;some_other_dir - will change the inherited value in the current (cmd.exe) process
1.3. Repeat #1.1 - will output the updated value
1.4. Open the Control Panel settings again. You won't see some_other_dir there
To check the current Python process environment (inherited from the parent process), use os.environ. Add in your script:
import os
print(os.environ["PATH"])
So, the non Anaconda Python might actually be in %PATH%.
But the behavior could also be (and I'm leaning towards this one) due to [Wikipedia]: File association. For more details, check [SO]: How do I set “default App” for a file extension to an “.exe” on Windows 10 after April 2018 update (#CristiFati's answer).
I've just started with Python, I've installed Python and it's extension from Microsoft, and when I run Python file in terminal I get the results, but the ugly path is covering plenty of space in terminal (like on the photo below). I've found video how to remove that from debbuging console, but couldn't find anything related to normal terminal. Is there any solution for that?
I am using Windows 10 and newest version of VS Code.
https://i.stack.imgur.com/vjKOm.png
You need to do 3 things.
1.
You need to add Python to your PATH. Call this python2 if you want to use python 3 as well for other project.
(However if you are starting, considering using python 3 from the beginning)
2.
Just use the file name if you are already in the folder
Your command is reduced to this then:
python2 strings.py
3.
Use prompt to change the line before your command. See doc for more information: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/prompt
I can change the order to python2,3 folders in the system PATH variable. But what are other ways to do this?
There should be more elegant way to change versions of python i want to run.
e.g. in console:
python file.py #will run python2
and after i change python command to use python3, it should be the same:
python file.py #will use python3
I suppose you are trying to run your script with the correct interpreter depending on which python version was used. On Unix/Linux this is done with a so called “shebang” which is written in the very first line of the file. E.g.:
#!/usr/bin/env python3.6
If your installation of Python3 is newer than Python 3.3 you can use the python launcher for windows, which should be able to launch the correct version of Python depending on the shebang, even on window.
Also see here for more informations on shebangs.
If your concern is what Python version is executed when calling python in a console, then an alias or a stub script are the two ways to go.
This post will explain you how you can do this on Windows.
The alias way, just like it would be on a Unix system, is to create an alias, either temporary to the session or permanent, so that python now means C:\Python27\python, or whatever version you want.
The script approach consists in putting a script named python in a directory referred to in your PATH, and have that script run the right version of Python.
I highly doubt that this will affect all the batch scripts that call python, but it will definitely fire the right Python when you'll type python in a console.
Now, if you're concerned about what version a script is executed with, you can specify an explicit version with a shebang line, or manually select it by right-clicking the .py file and clicking open with.
How do I, in the main.py module (presumably), tell Python which interpreter to use? What I mean is: if I want a particular script to use version 3 of Python to interpret the entire program, how do I do that?
Bonus: How would this affect a virtualenv? Am I right in thinking that if I create a virtualenv for my program and then tell it to use a different version of Python, then I may encounter some conflicts?
You can add a shebang line the to the top of the script:
#!/usr/bin/env python2.7
But that will only work when executing as ./my_program.py.
If you execute as python my_program.py, then the whatever Python version that which python returns will be used.
In re: to virtualenv use: virtualenv -p /usr/bin/python3.2 or whatever to set it up to use that Python executable.
Perhaps not exactly what you asked, but I find this to be useful to put at the start of my programs:
import sys
if sys.version_info[0] < 3:
raise Exception("Python 3 or a more recent version is required.")
I would use the shebang #!/usr/bin/python (first line of code) with the serial number of Python at the end
Then run the Python file as a script, e.g., ./main.py from the command line, rather than python main.py.
It is the same when you want to run Python from a Linux command line.
While the OP may be working on a nix platform this answer could help non-nix platforms. I have not experienced the shebang approach work in Microsoft Windows.
Rephrased: The shebang line answers your question of "within my script" but I believe only for Unix-like platforms. Even though it is the Unix shell, outside the script, that actually interprets the shebang line to determine which version of Python interpreter to call. I am not sure, but I believe that solution does not solve the problem for Microsoft Windows platform users.
In the Microsoft Windows world, the simplify the way to run a specific Python version, without environment variables setup specifically for each specific version of Python installed, is just by prefixing the python.exe with the path you want to run it from, such as C:\Python25\python.exe mymodule.py or D:\Python27\python.exe mymodule.py
However you would need to consider the PYTHONPATH and other PYTHON... environment variables that would point to the wrong version of Python libraries.
For example, you might run:
C:\Python2.5.2\python.exe mymodule
Yet, the environment variables may point to the wrong version as such:
PYTHONPATH = D:\Python27
PYTHONLIB = D:\Python27\lib
Loads of horrible fun!
So a non-virtualenv way, in Windows, would be to use a batch file that sets up the environment and calls a specific Python executable via prefixing the python.exe with the path it resides in. This way has additional details you'll have to manage though; such as using command line arguments for either of the "start" or "cmd.exe" command to "save and replace the "console" environment" if you want the console to stick around after the application exits.
Your question leads me to believe you have several Python modules, each expecting a certain version of Python. This might be solvable "within" the script by having a launching module which uses the subprocess module. Instead of calling mymodule.py you would call a module that calls your module; perhaps launch_mymodule.py
launch_mymodule.py
import sys
import subprocess
if sys.argv[2] == '272':
env272 = {
'PYTHONPATH': 'blabla',
'PYTHONLIB': 'blabla', }
launch272 = subprocess.Popen('D:\\Python272\\python.exe mymodule.py', env=env272)
if sys.argv[1] == '252'
env252 = {
'PYTHONPATH': 'blabla',
'PYTHONLIB': 'blabla', }
launch252 = subprocess.Popen('C:\\Python252\\python.exe mymodule.py', env=env252)
I have not tested this.
You can't do this within the Python program, because the shell decides which version to use if you a shebang line.
If you aren't using a shell with a shebang line and just type python myprogram.py it uses the default version unless you decide specifically which Python version when you type pythonXXX myprogram.py which version to use.
Once your Python program is running you have already decided which Python executable to use to get the program running.
virtualenv is for segregating python versions and environments, it specifically exists to eliminate conflicts.
For those using pyenv to control their virtual environments, I have found this to work in a script:
#!/home/<user>/.pyenv/versions/<virt_name>/bin/python
DO_STUFF
I had this problem and just decided to rename one of the programs from python.exe to python2.7.exe. Now I can specify on command prompt which program to run easily without introducing any scripts or changing environmental paths.
So i have two programs: python2.7 and python (the latter which is v.3.8 aka default).
While working with different versions of Python on Windows,
I am using this method to switch between versions.
I think it is better than messing with shebangs and virtualenvs
install python versions you desire
go to Environment Variables > PATH
(I assume that paths of python versions are already added to Env.Vars.>PATH)
suppress the paths of all python versions you dont want to use
(don't delete the paths, just add a suffix like "_sup")
call python from terminal
(so Windows will skip the wrong paths you changed,
and will find the python.exe at the path you did not suppressed,
and will use this version after on)
switch between versions by playing with suffixes
The version of Linux I am working on has python 2.6 by default, and we installed 2.7 on it in a separate folder.
If I want to run a .py script, how do I tell it to use 2.7 instead of the default?
Use update-alternatives --config python and shoose python2.7 from choices.
If you need to remove it use update-alternatives --remove python /usr/bin/python2.7.
Sorry for "stealing" answers, but I feel that there is a little bit of chaos here.
There are different ways to achieve that, it depends on at what level the decision is taken.
System Level
[Credit to #WoLy]
Use the update-alternatives feature of the system. Specifically, use
$ update-alternatives --config python
and you can choose the specific version.
Result: Once you do this, everything that uses "python" will use the python2.7 binary. This will happen in the whole system, for all users.
User Level
This is a little bit trickier. Credit to #TheFlyingProgrammer
The basic approach would be to change the .bashrc file in order to change the path and/or add an alias. Problem is if you are relying on the "shebang" of the file:
#!/usr/bin/python
<code python here>
This kind of file will be unaffected of your changes. However:
#!/usr/bin/env python
<code python here>
or executions like
$ python name_of_script.py
will use the interpreter of choice (the one forced at the .bashrc file).
Result: The owner of the modified .bashrc file will use, by default, the interpreter of choice. But some sheband will behave differently. So it is a bit trickier. So, be cautious.
Script Level
[Credit to #Anony-Mousse]
The "shebang" approach, modifying the first line. The idea is using the full path in the first line of the python source file:
#!/usr/bin/python2.7
<code python here>
You can use python, python2 or python2.7 and you will be more or less specific in the version. The problem would be if you want it to be portable. A similar approach would be to use full specification of version but without path:
#!/usr/bin/env python2.7
<code python here>
Note that if the PATH is not correctly set, this won't work. This gives some power to the user (when setting the PATH). You can, for instance, choose #!/usr/bin/env python2 to force some Python 2.x flavour, but maybe the specific binary will change from user to user.
In addition to that, keep in mind that if you plan to use virtual environments, using /usr/bin/env python is advisable (if I'm not mistaken).
Result: Well, it depends whether you use the env binary or not. But, in any case, you are putting the semantic in the file, which makes sense in many cases (e.g. if there is an incompatibility, it is at the script level).
Execution Level
[Credit to #Prune]
This is the simplest approach:
$ /path/to/your/python/bin/python2.7 my_script.py
You change, for this specific execution, which binary will be using (the Python interpreter ignores the shebang, because it is a comment).
Result: You override all other choices by cherry-picking your Python binary. Very good approach to test behaviour, but not very maintainable or shareable.
If you require a particular version, use the full path.
If you have e.g. python2.7 and python3.4 installed (and this is very common, as they are not fully compatible):
A script with
#!/usr/bin/python
will usually be running the latest version of python2 because of comparibility reasons. **You should avoid overriding what /usr/bin/python points to, to not break your system. Some apps will require this to point to a compatible version.
Instead, use
#!/usr/bin/python3
to use the latest python 3
#!/usr/bin/python2.7
to require python2.7
If you manually installed python (why? use the packages, that is much smarter because of automatic upgrades) then use the full path!
~/my-python/bin/python myscript.py
or if you start your script with the shebang:
#!/home/whatever/my-python/bin/python
so you can +x your script and do simply
./myscript.py
or make yourself an alias such as py if you are lazy to type.
open ~/.bashrc with a text editor, and add alias python=/usr/local/bin/python2.7
In case you're using Cent OS as your linux distribution, you will notice that
sudo python <file.py> doesn't work. That's because sudo doesn't have /usr/local/bin as a secure path. Open /etc/sudoers with a text editor, and you should see
#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults env_keep += "HOME"
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Add :/usr/local/bin to that secure_path and save.