可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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"]
}
]
回答2:
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.
回答3:
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"]
}
]
回答4:
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.
回答5:
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
}
回答6:
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!!
回答7:
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
回答8:
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
回答9:
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).
回答10:
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
回答11:
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"]
}
回答12:
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.
回答13:
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
}
回答14:
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
回答15:
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.