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"]
}
]
Related
I have the different virtualenv's and to be able to specify which virtualenv to use with each project I used package Virtualenv (https://packagecontrol.io/packages/Virtualenv) in Sublime Text 3. That was so to the day when through the update I became the owner of the Sublime Text 4. The package Virtualenv stopped working (it can be use only with Sublime Text 3).
I manage now the virtualenv's in Sublime Text 4 in the other way. I use the virtualenv through Tools/Build System. I run my script directly without leaving Sublime like with package Virtualenv. To build the new Build System for the existing virtualenv I first run Tools/Build System/New Build System and in the newly opened file I paste:
{
"shell_cmd": "C://python_projects/my_project/env/Scripts/python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
after this I save it under the name of the project (here my_project_python) in the dir User. It create a new file named my_project_python.sublime-build (composed of the build system name followed by .sublime_build). After doing this, I go to Tools -> Build System and I can see a new option my_project_python. The different virtualenv's only differ in the path C://python_projects/my_project/env/Scripts/python and names of the file .sublime-build.
That's nice but when we have a big number of folders with different virtualenv's this can be a bit frustrating to have so many names in Tools -> Build System. I think that the package Virtualenv manage the virtualenv's with more grace.
So I would like to ask the community if there is another package like Virtualenv that can work in Sublime Text 4? Maybe someone has some way to run the package Virtualenv in Sublime Text 4?
There is a package Virtualenv for ST3, you can downgrade. Currently it doesn't work with ST4 (issue).
You can specify build system per project:
Create sublime project or add existing directory to sublime project
Goto Project -> Edit Project
Add this and save in root of you project:
{
"build_systems":
[
{
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", # highlight line on error
"name": "<proj-name>",
"selector": "source.python",
"shell_cmd": "/path/to/virtualenv/.venv/bin/python -u \"$file\""
},
],
"folders":
[
{
"path": "."
},
],
}
If you don't want to store .sublime-project files in project's root dir, specify full path to project's root dir in: "path": "/path/to/proj"
Still, you need to chose build system before running scripts. In this case it'll be <proj-name>
You can generate this config programmatically.
More options for Project config here.
If you find something flexible for venvs, hit me up)
There is a build system variable $folder which gives the path to the top level folder open in the sidebar. If you specify a working directory in the project file this will be the project folder.
Add the path to your project file:
{
"folders":
[
{
"name": "ProjectName",
"path": "/path/to/your/project"
}
]
Then create a file Py3Venv.sublime-build:
{
"cmd": ["$folder/.venv/bin/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "utf8",
}
This file needs to be placed in User subfolder of the Sublime packages directory. Easiest way to find is "Preferences > Browse Packages" (macOS, ymmv).
ProjectManager plugin stores project files in SublimeText settings folder, rather than the project folder. Worth checking if you don't already use.
I would like to configure a second build system for Python on Sublime text 3. By default it use Python 2.7. for the time being, if I want to use Python 3.5, I execute activate py35 and it works like a charm.
Here is my original build system :
{
"cmd": ["start", "cmd", "/k", "python", "-u", "-i", "$file"],
"selector": "source.python",
"shell": true,
"working_dir": "$file_dir"
}
I added this line just before :
"cmd": ["activate", "py35"],
I've found this question but it didn't help, which command isn't recognized, even if my env is activated :
You can't add another "cmd". JSON doesn't allow duplicate keys.
You also don't need to activate anything. Give the full path to the Python executable
For example
"cmd": ["start", "cmd", "/k", "C:/Users/name/path/py35/Scripts/python",
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 able to set python3.2 for the build command in sublime text 2(and build with python3.2), but when invoking the console with cmd-` the interpreter is mac's default 2.6 version.
Any help is greatly appreciated!
The console in Sublime Text 2 uses the internal version of Python, which is 2.6. There is no way to change it without breaking a whole bunch of stuff. There is a workaround, though. If you just want a Python console within ST2, use the awesome SublimeREPL package, which can also be installed through Package Control. Among many other things, you can send selections or whole files to be interpreted through a REPL of your choice, including Python 3. Create Packages/User/SublimeREPL/config/Python/Main.sublime-menu with the following contents:
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "r",
"id": "SublimeREPL",
"children":
[
{"caption": "Python",
"id": "Python",
"children":[
{"command": "repl_open",
"caption": "Python 3",
"id": "repl_python3",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python3", "-i", "-u"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
}
]
}
]
}]
}
]
changing the "cmd" option to the full path to your python3 binary. This way your changes will survive any SublimeREPL upgrades. BTW, this path works for any package, so you can feel free to customize away without fear of accidentally losing it all.
Cmd+` is supposed to open Sublime Text's embedded interpreter console, i.e. the one that you use when developing or debugging Sublime plug-ins. You can verify that by noticing that the sublime module is available.
If you really want Python 3 console there, upgrade to Sublime Text 3 which embeds Python 3.3. Alternatively, use a dedicated plug-in like SublimeREPL (see #MattDMo's answer).
And BTW: If you want a nice environment for interactive Python work, I suggest to disregard the above and give IPython Notebook a shot.
You could try setting the environment variables something similar to the following in Tools > Build System > New Build System
{
"path": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"env":
{
"PYTHONPATH":"/usr/local/lib/python:/usr/local/username/python/3.2/lib/python3.2/"
},//this comma!
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
What you're seeing is the Python that comes bundled with Sublime Text. I wouldn't upgrade it, but if you want to use your own you could do something like:
ln -s $HOME/.pythonbrew/pythons/Python-2.6/lib/python2.6 /Your_Sublime_Install_Path/lib/python2.6
I often have many files open in multiple tabs in SublimeText2. Say I have 2 files, main.py and helper.py. While I am editing helper.py, how can I build main.py without switching tab to the file I want to build?
Create a new Build System and replace $file with your "main.py".
In " Tools > Build System > New Build System... " put this code in:
{
"cmd": ["python", "-u", "main.py"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Save it with some name, e.g. "Python(main.py).sublime-build". Now Python(main.py) will appear in your Build System list. Select it and hit cmd+B to build, now you will always build using your "main.py" :)
What #Hlung said will work, but I think it would be better if you created a project specific build system (which are stored inside project configurations). This way, your always-execute-main-file build system is only active for when you have that project open. If you are just editing single scripts that you want to execute, you wont have to manually switch the build system to run them.
Here's an example of a *.sublime-project file that I use which contains a project specific build system:
{
"folders":
[
{
"path": "app",
"file_exclude_patterns": ["*.sublime-*"],
"folder_exclude_patterns": ["__pycache__"]
}
],
"settings":
{
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"build_systems":
[
{
"name": "app",
"cmd": ["python", "-u", "${project_path}/main.py"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
]
}
You can find more info on build systems here