My current setting is Tools > Build System > Automatic.
I use two build systems: Python.sublime-build and Python64.sublime-build.
How to make that Sublime uses the latter if and only if the .py file begins with #python64?
(so that I don't have to manually switch between Sublime's Tools > Build System > Python 64 and Sublime's Tools > Build System > Python).
or, alternatively, how to make that:
CTRL+B uses Python (32 bit)
CTRL+SHIFT+B uses Python (64 bit)
(both of them should display the output in Sublime's bottom build output panel).
# Python.sublime-build
{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
# Python64.sublime-build
{
"cmd": ["c:\\python27-64\\python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)"
}
PS: I use Windows, and I need both versions of Python (32 and 64), for reasons that would be out of topic here.
In general, Sublime automatically selects the appropriate build system based on the type of the file that you are editing (e.g. a python source file). In Sublime Text 3, it is also possible to make a build system activate based on the presence of a certain file (e.g. a file called Makefile).
Neither of these is a viable solution to the use case presented here, and the second is only available in Sublime Text 3 and not Sublime Text 2.
There are a couple of ways to accomplish this. I'm providing two sets of instructions here, one for Sublime Text 2 and one for Sublime Text 3, so that this is a more broadly useful answer.
Option #1 - Custom Build Target
A build system can have an optional argument named target which specifies the command that sublime should execute in order to perform the build. When this is not specified, the default is the exec command. Most of the contents of the build file are actually just arguments that are directly passed to the exec command itself.
By specifying a custom target you can add extra logic to the build command so that has the power to analyze the current file and act appropriately.
The first part of this is to provide the custom command which will be used to perform the build, which can be done with some simple plugin code. This should be saved in your User package as a python file (e.g. Packages\User\python_build.py).
The second part is to modify the build system that you're using in order to take advantage of the new command to do what we want it to do. The single build file will be used both ways. You would name this Python.sublime-build and enable it either as an override to the existing build in Packages\Python\Python.sublime-build or in your User package as Packages\User\Python.sublime-build.
Sublime Text 2 Plugin:
import sublime, sublime_plugin
class PythonBuildCommand(sublime_plugin.WindowCommand):
def detect_version(self, filename, python32, python64):
with open(filename, 'r') as handle:
line = handle.readline ()
return python64 if (line.startswith ("#") and "64" in line) else python32
def execArgs(self, sourceArgs):
current_file = self.window.active_view ().file_name ()
args = dict (sourceArgs)
python32 = args.pop ("python32", "python")
python64 = args.pop ("python64", "python")
selected = self.detect_version (current_file, python32, python64)
if "cmd" in args:
args["cmd"][0] = selected
return args
def run(self, **kwargs):
self.window.run_command ("exec", self.execArgs (kwargs))
Sublime Text 2 Build File:
{
"target": "python_build",
"python32": "python",
"python64": "c:/python27-64/python",
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Sublime Text 3 Plugin:
import sublime, sublime_plugin
class PythonBuildCommand(sublime_plugin.WindowCommand):
def detect_version(self, filename, python32, python64):
with open(filename, 'r') as handle:
line = handle.readline ()
return python64 if (line.startswith ("#") and "64" in line) else python32
def execArgs(self, sourceArgs):
current_file = self.window.active_view ().file_name ()
args = dict (sourceArgs)
python32 = args.pop ("python32", "python")
python64 = args.pop ("python64", "python")
selected = self.detect_version (current_file, python32, python64)
if "shell_cmd" in args:
args["shell_cmd"] = args["shell_cmd"].replace ("python", selected)
return args
def run(self, **kwargs):
self.window.run_command ("exec", self.execArgs (kwargs))
Sublime Text 3 Build File:
{
"target": "python_build",
"shell_cmd": "python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"python32": "python",
"python64": "c:/python27-64/python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "python -m py_compile \"${file}\"",
}
]
}
Notice that the plugin code is mostly the same in both versions of the code. Sublime Text 3 supports shell_cmd as well as cmd for specifying the executable, and the default build systems for Python in each version reflect that. If desired, the Sublime Text 2 version should also work in Sublime Text 3, as long as you use the appropriate build file as well.
In either case, the custom command will check the first line of the file to see which of the two versions of python that it should execute, modify the command in the build system as appropriate, and then invoke the exec command to perform the build.
The build file itself needs to specify which version of the python interpreter to use in either case, with the fallback (as determined by the code in the plugin) being python for both if it is not specified.
If you are using Sublime Text 3 and place the build file in your User package, your build menu will contain the Python option twice; once for the built in version and once for your own. In this case you may need to ensure that the proper one is selected.
Option #2 - Using a Key Binding
There is no command in either version of Sublime that can run a build and also specify the build system to use (at least not a documented one that I can find). This is still possible with a key binding in both versions, although in the case of Sublime Text 3 is is a little easier.
Sublime Text 2 Key Bindings:
For Sublime Text 2, the command build will perform a build using the currently selected build system and set_build_system can be used to swap the build system around.
In order to do this with a single key press, you need to install the ChainOfCommand plugin, which allows you to chain multiple commands together. With that package installed, you can set up the following key bindings:
{
"keys": ["ctrl+b"],
"command": "chain", "args": {"commands": [
["set_build_system", {"file": "Packages/Python/Python.sublime-build"}],
["build"]
]}
},
{
"keys": ["ctrl+shift+b"],
"command": "chain", "args": {"commands": [
["set_build_system", {"file": "Packages/Python/Python64.sublime-build"}],
["build"]
]}
}
The first of these changes the build system to be python and then runs the build, while the second one changes it to be Python64. Modify the paths to the build files as appropriate (e.g. if you stored one or both in your User package instead).
This is a bit sub-optimal because it makes the Ctrl+B key always try to build python even if that is not appropriate.
I don't actively use Sublime Text 2 so I'm unsure of how you would go about making these bindings specific only to a python file. The few things I tried that would work in Sublime Text 2 did not work here.
Sublime Text 3 Key Bindings:
For Sublime Text 3, this is a little easier. This version supports variants in a build system, and the build command can be told to execute a variant of the currently selected build.
To get this working, you need a single build system that looks something like the following version. This is a modified version of the standard Sublime Text 3 python build file, which removes the Syntax Check variant in favor of a Python64 version. This could be modified as desired.
{
"shell_cmd": "python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Python64",
"shell_cmd": "c:/python27-64/python -u \"$file\"",
}
]
}
With this in place, add the following key binding:
{
"keys": ["ctrl+shift+b"],
"command": "build", "args": {"variant": "Python64"},
"context": [
{ "key": "selector", "operator": "equal", "operand": "source.python"},
]
},
Now the build system has a variant, so that the regular Python build will use the 32 bit version and Python - Python64 will use the 64 bit version instead.
The key binding here is set to operate only in a python file, since in Sublime Text 3 this key sequence is used to prompt you for the variant of the current build to use.
For some initial setup, once you enable this key binding you should open up a python file and select Tools > Build > Build With... from the menu, then select Python in order to tell Sublime that you want to use the Python build.
From this point forward, while you're editing a python file, Ctrl+B will execute the main build, which is the 32-bit python, and Ctrl+Shift+B will execute the variant that uses the 64-bit version.
In addition to OdatNurd's excellent answer, here is what I used, using keymaps and variants.
CTRL+B : Python 32 (output log inside Sublime)
CTRL+SHIFT+B : Python 64 (output log inside Sublime)
ALT+SHIFT+B : Python 32 (in a new terminal window)
CTRL+SHIFT+ALT+B : Python 64 (in a new terminal window)
CTRL+ALT+B : kill the current Python script
Default (Windows).sublime-keymap
[
{ "keys": ["ctrl+alt+b"], "command": "exec", "args": {"kill": true} },
{ "keys": ["alt+shift+b"], "command": "python_run" },
{ "keys": ["ctrl+shift+alt+b"], "command": "python64_run" }
]
Note: there's nothing to specify here about CTRL+B (default build) and about CTRL+SHIFT+B (default variant build).
python_run.py
import sublime
import sublime_plugin
import subprocess
class PythonRunCommand(sublime_plugin.WindowCommand):
def run(self):
command = 'cmd /k "C:\Python27\python.exe" %s' % sublime.active_window().active_view().file_name()
subprocess.Popen(command)
python64_run.py
Idem with C:\Python27-64\python.exe
Python.sublime-build
{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"variants": [ {
"name": "Run",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"cmd": ["C:\\Python27-64\\python.exe", "-u", "$file"]
} ]
}
Related
I want to configure Sublime Text 3 to build Python 3, but I don't seem to understand how the builds work. Many tutorials have told me to make a build file containing code such as:
{
'cmd': ['/usr/bin/python3', '-u', '$file'],
'file_regex': '^[ ]*File "(…*?)", line ([0-9]*)',
'selector': 'source.python'
}
and save it as a file called Python.sublime-build or python3.sublime-build (much of the information I found was conflicting). One tutorial suggested creating a new folder in the ST3 Packages folder called Python and add the build file in there, whilst other tutorials suggested leaving it in the folder called User.
One tutorial explained how I had to change the Environment Variable path on my operating system to get it to work. That didn't seem to help either.
I added a folder Python to Packages (since it wasn't there already) and added in a build file with the name Python.sublime_build which featured only the code I posted above in it. Now when I attempt to run Sublime Text it gives me this error:
Error trying to parse build system:
Expected value in Packages\Python\Python.sublime-build:2:5
The reason you're getting the error is that you have a Unix-style path to the python executable, when you're running Windows. Change /usr/bin/python3 to C:/Python32/python.exe (make sure you use the forward slashes / and not Windows-style back slashes \). Once you make this change, you should be all set.
Also, you need to change the single quotes ' to double quotes " like so:
{
"cmd": ["c:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
The .sublime-build file needs to be valid JSON, which requires strings be wrapped in double quotes, not single.
Steps to Make Sublime Text a Python IDE (Windows)
Tested successfully on Sublime Text 3. Assuming Sublime Text and package control are already installed . . .
Install Python (python.org) and pay attention to where it is installed or choose a simple location like the C drive, agreeing to remove character limit at the end of the installation.
Install package SublimeREPL (Cntrl + Shift + P, Package Control - Install Package, SublimeREPL, Enter).
Go to Preferences, Package Settings, SublimeREPL, Settings - User.
Paste in the following, updating the file path to your python installation folder, as needed. You may customize these and choose whatever syntax you like (last line) but I prefer my output in plain text.
{
"default_extend_env": {"PATH":"C:\\Program Files\\Python36\\"},
"repl_view_settings": {
"translate_tabs_to_spaces": false,
"auto_indent": false,
"smart_indent": false,
"spell_check": false,
"indent_subsequent_lines": false,
"detect_indentation": false,
"auto_complete": true,
"line_numbers": false,
"gutter": false,
"syntax": "Packages/Text/Plain text.tmLanguage"
}
}
Save and close the file (SublimeREPL.sublime-settings).
Go to Tools, Build System, New Build System.
Replace all existing text with the following:
{
"target": "run_existing_window_command",
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
Cntrl + S or save as "C:\Users[username]\AppData\Roaming\Sublime Text 3\Packages\User\SublimeREPL-python.sublime-build" updating username or path as needed. This should be wherever your settings and builds are stored by Sublime Text.
Go to Tools, Build System, select SublimeREPL-python.
All done--now to test. Open or create a simple python file, having a *.py extension and save it wherever desired.
Make sure the file is open and selected in Sublime Text. Now, when you press Cntrl + B to build and run it, it will open another tab, titled "REPL [python]", executing and displaying the results of your python code.
If you would like to go a step further, I highly recommend making the follow changes, to allow Sublime to reload your executed python in the same window, when you press Cntrl+B (Build), instead of it opening a new tab each time:
Add the following line in the "repl_python_run" command in (Preferences, Browse Packages) SublimeREPL\config\Python\Main.sublime-menu, right before the "external_id": "python" argument:
"view_id": "*REPL* [python]",
and then to change the line:
if view.id() == view_id
into:
if view.name() == view_id
in SublimeREPL\sublimerepl.py.
If you are using PyQt, then for normal work, you should add "shell":"true" value, this looks like:
{
"cmd": ["c:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell":"true"
}
Run Python Files in Sublime Text3
For Sublime Text 3,
First Install Package Control:
Press Ctrl + Shift + P, a search bar will open
Type Install package and then press enter
Click here to see Install Package Search Pic
After the package got installed. It may prompt to restart SublimeText
After completing the above step
Just again repeat the 1st and 2nd step, it will open the repositories this time
Search for Python 3 and Hit enter.
There you go.
Just press Ctrl + B in your python file and you'll get the output.
Click here to see Python 3 repo pic
It perfectly worked for me. Hopefully, it helped you too.
For any left requirements, visit https://packagecontrol.io/installation#st3 here.
Steps for configuring Sublime Text Editor3 for Python3 :-
Go to preferences in the toolbar.
Select Package Control.
A pop up will open.
Type/Select Package Control:Install Package.
Wait for a minute till repositories are loading.
Another Pop up will open.
Search for Python 3.
Now sublime text is set for Python3.
Now go to Tools-> Build System.
Select Python3.
Enjoy Coding.
Version for Linux. Create a file ~/.config/sublime-text-3/Packages/User/Python3.sublime-build with the following.
{
"cmd": ["/usr/bin/python3", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"
}
And to add on to the already solved problem, I had installed Portable Scientific Python on my flash drive E: which on another computer changed to D:, I would get the error "The system cannot find the file specified". So I used parent directory to define the path, like this:
From this:
{
"cmd": ["E:/WPy64-3720/python-3.7.2.amd64/python.exe", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
To this:
{
"cmd": ["../../../../WPy64-3720/python-3.7.2.amd64/python.exe","$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
You can modify depending on where your python is installed your python.
first you need to find your python.exe location, to find location run this python script:
import sys
print(sys.executable)
Then you can create your custom python build:
{
"cmd": ["C:\\Users\\Sashi\\AppData\\Local\\Programs\\Python\\Python39\\python.exe", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"}
You can change the location, In my case it is C:\Users\Sashi\AppData\Local\Programs\Python\Python39\python.exe
Then save your new build. Don't change the file extension while saving.
I'd like to add just one point to the accepted answer:
when you edit the cmd portion of the snippet below, make sure to add the file address (with forward slash) where python is kept on your computer.
{
"cmd": ["c:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
How to know where python is installed on your system? Two ways for windows users:
Open Command prompt and do the following query: where python
If it shows the path to python.exe, copy and paste it with /. If it shows some error, then follow process below:
Go to start -> Search for python shortcut (not IDLE) -> Right Click -> More -> Open file location -> Right click the shortcut -> Properties -> Target -> Path is now visible -> Copy and paste it in the field above.
Here is a very simple Python Sublime Text build system that works when python scripts are invoked with py file_name.py.
Just create py.sublime-build by Tools > Build System > New Build System and add the contents below:
{
"cmd": ["python3", "$file"]
}
You can select it in Sublime Text editor by going to Tools > Build System > py and building with Ctrl + b.
Note: If your filesystem doesn't python3 than you need to provide path/to/python3 and it should work.
I have just started reading the 2nd edition of Python Crash Course, and just in case someone has the book, I am on the subsection "Using Variables in Strings" in chapter 2.
The problem is, when I enter
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)
on Sublime Text, it is supposed to output
ada lovelace
but instead, I receive an error reading
File "/Users/davidjiang/Code/Python/python_work/full_name.py", line 3
full_name = f"{first_name} {last_name}"
^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/davidjiang/Code/Python/python_work/full_name.py"]
[dir: /Users/davidjiang/Code/Python/python_work]
[path: /Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
I have already checked that Sublime Text is running in python3.7 by inserting
{
"cmd": ["python3", "-u", "$file"],
}
into the build system Python3.sublime-build. My terminal also runs in python3.
I also made sure that my code didn't have any errors by finding and using this link explaining how to insert values into strings.
Additionally, the book I am reading comes with a collection of online resources complementing the course, including the code written at the beginning. When I put it through python IDLE, the code worked out fine.
Can someone please explain why the code doesn't work on Sublime Text? Thanks in advance!
Edit: Apparently, my Sublime Text is running python2 instead of python3. As it seems that inputting the above code won't work, does anyone have any advice on how to change Sublime to python3?
Using python Repl should work as this can run F strings and inputs. Make sure you are using this as an add on otherwise this will not work.
Using this should help-
https://packagecontrol.io/packages/SublimeREPL
In you error output, it's clearly written
[shell_cmd: python -u "/Users/davidjiang/Code/Python/python_work/full_name.py"]
The key thing here is that the shell command is python not python3. So you aren't running the proper build system.
{
"shell_cmd": "python3 -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "python3 -m py_compile \"${file}\"",
}
]
}
Save this as python3.sublime-build (in the Packages\User directory)
Use ctrl+shift+b and select python3.
Next time ctrl+b will automatically use python3
There's a way to download python 3 to sublime text. First you want to go to the build system option then scroll all the way down to "New Build System". From there it will open up a new tab and will display this: { "shell_cmd": "make" }. Delete that and replace it with:
{
"cmd": ["python3", "-i", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python" }
then save, naming it whatever you'd like (make sure it saves as a .sublime-build file). Now you should have it in your build systems. If it is not there after you save you may have to restart the program.
(I understand variations of this question have been asked before, but not specifically with Python 3+ and Sublime Text 3, and I tried the solutions but none of them worked.)
Anyhow: I'm using Python 3.5. I'm trying to use a 3rd party module - Scrapy - in Sublime Text. While the module works in IDLE, it does not work in Sublime.
I tried redirecting my PYTHONPATH but that still didn't work. My syntax-specific settings for Python are:
{
"path": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"word_wrap": true,
"env": {
"PYTHONPATH": "/Library/Frameworks/Python.framework/Versions/3.5/bin"
}
I've run into this.
In your case Sublime Text 3.x needs a new build system that needs the "path" and the "cmd" defined in the new build system to correctly recognize both the location of the modules and what python command string you want to build with.
For Python 3.7 on OSX 10.14.2 I used the build file below. I assume the same will work with 3.5 or any 3.x version if modified by replacing the 3.7's with 3.5's. If you install a new version of python you will need a new build system in sublime as well.
Under tools>build system>new build system add a file containing the settings below and save it.
{
"path": "/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Switch to it using tools > build with.
What you need is a build system, not syntax-specific settings. Select Tools -> Build System -> New Build System... and change the contents to the following:
{
"path": "/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.5/bin/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Save the file as Packages/User/Python3.5.sublime-build where Packages is the folder opened by selecting Preferences -> Browse Packages... (when you hit Save after editing the new build system, it should automatically point you to Packages/User).
To use the build system, select Tools -> Build System -> Python3.5, then hit CtrlB (Windows, Linux) or ⌘B (OS X) when your .py file is focused.
Does anyone have a step by step process for a beginner to get the latest version of Python (3.4) to work on Sublime Text 3? I tried adding Python 3 as another build but I don't think i added it correctly (the instructions were for how to add it for Sublime Text 2) because I cannot build my function when I set it to Python 3 on Sublime Text 3. Only the basic Python build version is working. How can I get Python 3.4 to work? Thanks really need help immediately
Goto Tools > Build > New Build System and enter the following.
{
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "utf8",
"path": "/Library/Frameworks/Python.framework/Versions/3.4/bin/" }
Save the file and restart Sublime Text. If it still doesn't work, check if the 'path' is correct in your case.
Tools > Build System > New Build System..
Replace this code
{
"shell_cmd": "make"
}
with this
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
}
/usr/local/bin/python3 ==> python.exe path in your system
then save with a name.sublime-build. It will appear in build system. select the build system and run the python code by pressing Ctrl + B
Select the menu Tools > Build > New Build System and enter the following:
{
"cmd": ["python3", "$file"]
, "selector": "source.python"
, "file_regex": "file \"(...*?)\", line ([0-9]+)"
}
After that, save it to the following (Mac-specific) directory: ~/Library/Application Support/Sublime Text 3/Packages/User
That's all there to it.
I am using sublime text 2 for python development along with virtualenv!
The standard sublime text 2 build system uses the standard python install rather than my virtualenv where my packages are installed.
How can I get sublime text 2 to build using my virtualenv?
I currently use the terminal to activate my environment and run my scripts.
UPDATE: Never got it working, but seeing as i am using flask and it builds when you make a change, it's not a big issue
You can also set the path for the build system to the bin directory of your virtualenv, like so:
"build_systems":
[
{
"selector": "source.python",
"env": {"PYTHONPATH":"/Users/user/project"},
"path":"/Users/user/work/myvirtualenv/bin:$PATH",
"name": "Run virtualenv python",
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"variants": [
{ "name": "Run all Tests",
"working_dir": "/Users/user/project",
"cmd": ["nosetests"]
}
]
}
]
This also allows other tools, like nose in the example, to find the correct python binary from the virtualenv.
In windows this works for me:
"build_systems":
[
{
"name": "Run Tests",
"working_dir": "/path/to/to/your/django_project",
"cmd": ["/path/to/your/virtualenv/bin/python.exe", "manage.py", "test"]
}
]
Sublime's Build System supports variables which can be used with Sublime project files to make this a bit more portable across projects.
If your virtual environments are in a standard spot, create a new project file (Project -> Save Project As) into the root directory of your project just above your virtual environment directory. Then create a new build file with something like this:
{
"cmd": ["$project_path/venv/bin/python", "-u", "$file"]
}
It seems to then pick up the rest automatically - the same as if you typed ./venv/bin/python from the command line - no need to mess with paths, environment variables, etc.
I'm using Flask, but I think it's apply to nearly every case.
My actual build is like this, where "benicio" is the directory of my project:
{
"cmd": ["source ~/projs/benicio/venv/bin/activate && python ~/projs/benicio/benicio_tests.py"],
"shell": true
}
Sorry to add yet another answer to this - but this caused me a large amount of grief figuring this out.
Not only do you need to make a build system like:
"build_systems":
[
{
"name": "Maths",
"env": {"PYTHONPATH":"/home/nebffa/Desktop"},
"path":"$project_path/bin",
"cmd": ["$project_path/bin/python3.3", "-u", "$file"]
}
]
but you HAVE to change a setting in Sublime Text - go to Tools --> Build System --> "Maths". In my case I need to choose "Maths" because that's what I named my build system. If you don't do this - Sublime Text does not use your build system!!
I have just got sublime text 3 to working in a virtualenv. Although the OP specified ST2, there all likely more like myself who are using ST3. Thanks to user1248490 and Russell Beattie I arrived at the following:
{
"shell_cmd": "$project_path/vi_sys_pkgs/bin/python3 -u \"$file\"",
"path": "$project_path/vi_sys_pkgs/bin",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Note that "cmd" is now "shell_cmd" in ST3. See ST3 blog
Under MAC OSX, this works for me
{
"cmd": ["/your/virtualenv/bin/python", "-u", "$file"]
}
What i did was keep it simple:
Went to root drive and created python folder:
sudo mkdir python
then went in there and created the virtualenv
virtualenv --no-site-packages virtualenvname
then created a newbuild in ST2 with the above command and it works
This is what I have as a build system (assuming my virtualenv is created as a folder called 'env' in my current project directory). This at least means I don't need to constantly change the build system between projects:
{
"cmd": ["env/bin/python", "-u", "$file"]
}
I saved this as a New Build System (Tools -> Build System -> New Build System).
I use this to build my Flask project. I have added the following code to my Project Settings: Project -> Edit Project
{
"folders":
[
{
"path": "/C/MyDev/chilinzb/"
}
],
"build_systems":
[
{
"name": "Flask",
// activate the specific virtualenv for the project
"cmd": ["C:/MyDev/virtualenvs/env_chilinzb/Scripts/python", "$file"]
}
]
}
and then I just switch to my run.py file and hit Ctrl+B
this combination worked great:2 steps
1) add the 2 appropriate keys to the 'env' key.
A) DJANGO_SETTINGS_MODULE
B) PYTHONPATH
2) update cmd to reflect the version of python you want to use.
{
"env":{"DJANGO_SETTINGS_MODULE":"my_project.settings",
"PYTHONPATH":"d:\\Projects\\virts\\my_project\\Scripts;d:\\Projects\\virts\\my_project\\Lib;d:\\Projects\\virts\\my_project\\Lib\\site-packages;D:\\Projects\\my_project"
},
"cmd": ["c:/Python27/python.exe","$file"]
}
I have an answer for anyone who uses Heroku and uses their foreman tool, and it works great. Simply create a new build system like so:
{
"cmd": ["foreman", "run", "python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
This pulls in all of the environment variables available to Foreman, including your virtualenv's $PATH variable, which adds the virtualenv Python to your python path.
source did not work for me inside the build on lubuntu.
use '.' or dot instead of 'source'.
this did work:
{
"env": {
"DJANGO_SETTINGS_MODULE":"django_project_name.settings",
"PYTHONPATH":"/home/my_username/current/django_project_name:/home/my_username/.virtualenvs/django_project_name/lib/python2.7:/home/my_username/.virtualenvs/django_project_name/lib/python2.7/site-packages/"
},
"working_dir":"$project_path",
"cmd":[". /home/my_username/.virtualenvs/django_project_name/bin/activate && python $file"],
"shell":true
}
this worked for me:
{
"cmd": ["C:/Users/user/virtualenvs/env1/Scripts/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
saved build in:
"C:\Users\user\AppData\Roaming\Sublime Text 2\Packages\User\"
as
"Python_env1.sublime-build"
Select
Tools> Build System> Python_env1
done!
using windows 8.1, st2
Assuming you keep your project-specific virtualenv in an .env-folder on the top level of your project.
Sublime > Project > Save project as... (if you haven't already. This will allow you to set custom build options for the project
Project > Edit Project:
{
"folders":[
{
"path": ".",
"folder_exclude_patterns": [".env"],
}
],
"build_systems":[
{
"name": "Run in VirtualEnv",
"shell_cmd": "source $project_path/.env/bin/activate && python -u $file"
}
]
}
Tools > Build System > Run in VirtualEnv
Tools > Build
Please note that this was tested with Sublime Text 3.
Note comments about this solution being incorrect.
You have to edit your sublime-project file and add the following:
"build_systems":
[
{
"name": "Run Tests",
"cmd": ["source", "/path/to/your/virtualenv/bin/activate"],
"working_dir": "/path/to/to/you/django_project",
"cmd": ["python", "manage.py", "test"]
}
]