Trouble setting Python version in Sublime Text2 - python

I'm having trouble setting the build environment in Sublime Text2.
I'm using Macports for Python and package installation.
My python.sublime-build file looks like this:
{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
I think (from searching) that I need to modify the "cmd" line to point to the Macports version. Anyone done this successfully?
From Terminal, everything builds/runs fine, it's only Sublime Text2's build that's grabbing the system version still.
Additional info:
which python
/opt/local/bin/python
Thanks for any help.

Your Sublime Text 2 environment is different from your shell environment; the $PATH variable is probably not pointing to the same directories and the wrong executable is selected.
You have several options to work around that:
Set a "path" option that includes /opt/local/bin need to use an absolute path for your python executable, as Sublime Text 2 doesn't share the same environment PATH variable your shell uses. Inspect echo $PATH and use that as a template:
{
"cmd": ["python", "-u", "$file"],
"path": "/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/Current/bin:/Users/yourloginname/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
That'll use /bin/bash to run the command, which I think would have the same $PATH setting as your terminal.
Run the command through the shell instead by setting the "shell" option:
{
"cmd": ["python", "-u", "$file"],
"shell": true,
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Use the full path for the python command:
{
"cmd": ["/opt/local/bin/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

Related

Sublime Text Python3.6 cmd+shift+b not working

I've set up a new build system for Sublime Text (Python 3.6).
The problem is that the build system does not show up results.
Python 2.7 works properly.
The cmd+Shift+B command is not working either (Python 3.6).
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "unicode",
"path": "/Library/Frameworks/Python.framework/Versions/3.6/bin/"
}
The way the new build system is set-up.
Try this, it worked for me:
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
Or if you don't have python3 there, just use whatever you get from typing this in command line:
$ which python3
Note that when I did this, it broke my Python build (2.7) so I had to change cmd to reflect the full path to the python binary.

Possible to switch between python 2 and 3 in Sublime Text 3 build systems? (Windows)

All my current coding has been in python 3, which I've installed via the Anaconda package. However I need to work on some code simultaneously in python 2. Is there a way I can add a build system in Sublime so I can switch between the two fluidly? I have both python 2 and 3 installed, however can't see a way of simply editing the build system slightly to switch between the two languages. The build system I'm using for python 3 is:
{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
Thanks very much!
Specify the absolute path to the version of Python. For example, if Python version 3 is located at /usr/bin/python3 and Python version 2 is located at /usr/bin/python2:
Python 3 Build Configuration:
{
"cmd": ["/usr/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
Python 2 Build Configuration:
{
"cmd": ["/usr/bin/python2", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
Find the location of Python via which:
$ which python
/usr/bin/python
$ which python3
/usr/bin/python3
$ which python2
/usr/bin/python2
See Build system of Sublime Text 3 executes a different version of PHP, it is the same principal.
Old question, but...
On Windows, you dont need create a new build system or specify the absolute path. Edit already existing python build system.
In SublimeText\Data\Packages\ create folder Python -> like this SublimeText\Data\Packages\Python\
In this folder, create file Python.sublime-build , it will overwrite the existing python build system.
In this file write (python launcher documentation):
{
"shell_cmd": "py -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
}
For choose version of python interpretator, in the first line of your scripts write (shebang lines)
#! python or
#! python3
1, intall pyhton3:
https://www.python.org/downloads/
2, find the file "python.exe" and rename to "python3.exe"
3, in Windows command line:
where python3.exe
Save this ubication wherever you want.
4, In sublime goto -> Tools -> Build system -> New build system (this open a file)
5, finally the ubication file of number 3, only change the ubication:
{
/*replace this*/"cmd": ["C:/Users/youruser/AppData/Local/Programs/Python/Python37-32/python3.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
Save the file like "python3".
6, In sublime go to Tools -> Build system -> Select python3 and you can run this code in python 3.
7, Show your python version:
import sys
print(sys.version)
If you want to run in python 2, then go to Tools -> Build system -> Select "Python"

How to make Sublime Text read PYTHONPATH environment variable on Windows

There are several online resources describing how to make SublimeText read PYTHONPATH variable specified in Windows Start menu > Control Panel > System > Advanced Settings > Environment Variables.
Yet, none of them clearly describe how to customize Sublime in Windows. How do we do it?
If using python 3.x you need to edit the Python3.sublime-build (Preferences > Browse packages > Python 3)
to look like this:
{
"path": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"env":{"PYTHONPATH":"/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages"},
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
env needs to be a JSON object, or dictionary if you will, like this:
"env":
{"PYTHONPATH":"/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages"},

Change Python Version in SublimeText2

I'm almost going crazy trying to figure this out, but I know I'm close.
I want to change the Python used by SublimeText2
I'm running Python in ST2, but it is not the version I want.
import sys
print sys.executable
>>>> /usr/bin/python
The version I want is in the terminal.
me_user$ which python
>>>> //anaconda/bin/python
I want to use the Anaconda python in ST2, not the /usr/bin/python.
But I cannot figure out how to change the path in ST2 to read the Anaconda version of python.
Any help?
..... UPDATE .....
I followed the instructions by Jerome but now I get an error.
Here is my build path:
{
"path": "/anaconda/bin/python",
"cmd": ["python2.7", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
And here is the error:
import sys
print(sys.version)
[Errno 20] Not a directory
[cmd: [u'python2.7', u'/Users/robertdefilippi/Desktop/test2.py']]
[dir: /Users/robertdefilippi/Desktop]
[path: /anaconda/bin/python]
[Finished]
What have I done wrong now?
Ah figured it out.
I used:
echo $PATH
In the terminal to get the full range of paths.
I then copied the paths into
{
"path": "PATH",
"cmd": ["python2.7", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python" }
And it works :)

Problems with running Python 32 in Sublime Text 2

I know that there are lots of questions about running Python in Sublime Text 2, but i have a problem.
I've changed "Python.sublime-build" file in AppData package on this one
{
"cmd": ["C:\\Program Files\\Python32\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Where "C:\Program Files\Python32\python.exe" is my own path. When I press Ctrl + B. I see "building..." and than the inscription disapeers and nothing happens.
What to do. help me please!
You shouldn't be modifying any files in AppData/Roaming/Sublime Text 2/Packages/ unless they're in the User/ directory. Any changes will be overwritten upon upgrade, and if you break something (unless you've made backups) you might not be able to fix it without reinstalling.
So, change Packages/Python/Python.sublime-build back to the following:
{
"cmd": ["python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Next, create a new file Packages/User/Python3.sublime-build with the following contents:
{
"cmd": ["c:/Program Files/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Save the file, then go to the Tools -> Build System menu and select Python3. You should now be able to run a Python 3 file by hitting CtrlB. I'd suggest running the one below, as it will show if your Python installation is working properly:
import sys
print(sys.version)

Categories

Resources