Sublime Text 3 Build Python Code with virtualenv - python

I am trying to using sublime text 3 for python development with virtualenv!
I‘ve read the answer that i can find on Stackoverflow and baidu(our national search engine ),but i am still very confused. i hope you can help me,tank you very much!
My computer system:ubuntu 17.04
My sublime configuration file:
{
"folders":
[
{
"follow_symlinks": true,
"path": "."
}
],
"build_systems":
[
{
"name":"my_flask_env",
"cmd":["python","-u","$file"],
"path":"/myproject/venv/bin:$PATH",
"file_regex":"^[ ]*File \"(...*?)\", line ([0-9]*)",
"env":{
"PYTHONIOENCODING":"utf8",
"PYTHONHOME":"",
},
"selector": "source.python"
}
]
}
the code of test.py:
import sys,os,flask
print(sys.executable)
#print(inspect.getsoutcefile(flask))
when i run test.py ,the error like this:
ImportError: No module named site
[Finished in 0.0s with exit code 1]
[cmd: ['python', '-u', '/home/bole/myproject/flask_test/test.py']]
[dir: /home/bole/myproject/flask_test]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]
So,What should I do now that it will work properly .
My English just so so,i hope it's clear.

Using Terminus and Virtualenv in Sublime Text 3 can be easy. Virtualwrapper is installed and outside Sublime I am using 'workon myenv'. Workon can be used in a sublime-build.
My venv is called boringStuff.
{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "workon boringStuff;python3 -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"working_dir": "$file_path",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
}

Related

How to run python code in Sublime Text 3?

So I'm trying to run python code from Sublime Text 3, but I'm not sure how. Even if it was only from the console, that would be fine. Anybody know how???
Tools->Build System->Python
or Ctrl+B
Need to install package to run python from sublime Python + Sublime
Try Anaconda plugin which will help you to run python on sublime
Setup Sublime for python
You can use this package in sublime text: https://packagecontrol.io/packages/Terminal to open a terminal at the specific file or folder.
Sublime Text 3 will run your python code inside the integrated console when you use Ctrl + B
if you want to run your code at own terminal, but still get some error information inside to integrated console, you need to build your own builder, or use plugins.
One way: https://github.com/Wilhox/Integrated-builder.git
This answer is for fellow googlers who want to run python scripts within their sublime. As other answers explains, all you need is a sublime build system, here after a bit of struggle I got it worked for Linux Systems.
{
"cmd": ["gnome-terminal", "--", "/bin/bash", "-c", "python3 -u \"$file\" echo;echo;echo Press Enter to exit...;read"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
This is by far the simplest I believe. Hope it helps.
If you need non-interactive Build System, just follow the Official Guide.
If you plan to run code that contains something like input(), or you have any other way of interacting with your program you would need additional setup - Plugin + simple config.
The steps of having proper/full build system are:
Install "Package Control":
Win/Linux: ctrl+shift+p, Mac: cmd+shift+p ▶ Type: Install Package
Control ▶ ENTER
Install Terminus via Package Control:
Win/Linux: ctrl+shift+p, Mac: cmd+shift+p ▶ Type: Package Control: Install Package ▶ ENTER ▶ Type: Terminus ▶ ENTER
Create Build System for Python
Tools ▶ Build System ▶ New Build System… menu item or the Build: New Build System ▶ Paste one of the following sections and edit respectively.
For Windows, obviously you should change the path to your Python:
{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -m py_compile \"${file}\"",
}
]
}
For Mac/Linux, do not forget to change the path to your Python.:
{
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -m py_compile \"${file}\"",
}
]
}
Name the file e.g: Python Custom.sublime-build
Select the newly created Build System:
Tools ▶ Build System ▶ Python Custom
6. Execute your code:
Ctrl/CMD + B
SOURCE
#Thayakorn Rakwetpakorn's answer is correct
Ctrl+ B, and also make sure to have saved the file as hello.py or something before trying to run it
If that doesnt work
Tools->Build system -> New build system
Delete the existing code and paste the code I have shown below
{
//"shell_cmd": "make"
"cmd": ["C:\\Users\\Programs\\Python\\Python37\\python.exe","-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Then change the file location to where your python.exe file location is, in the above code
"C:\\Users\\Programs\\Python\\Python37\\python.exe"
Instead of the code lines of my file location path

Set PYTHONPATH in Sublime 3

I'm using Sublime editor for writing Python programs, with the Anaconda plugin.
I have a Python project which relies on some custom python lib I wrote in another directory.
I first modified the MyProject.sublime-project file in the build_systems section, it seemed to work but after some time, the sublime-project file is automaticcally overwritten.
I then found that the right way to do it is to add a settings/extra_paths section (from here) and my sublime-project file looks like this :
{
"build_systems":
[
{
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"name": "Anaconda Python Builder",
"selector": "source.python",
"shell_cmd": "\"python\" -u \"$file\""
}
],
"folders":
[
{
"path": "."
}
],
"settings":
{
"extra_paths":
[
"C:\\path\\to\\my_lib"
]
}
}
But it doesn't work. Not only my libs import aren't found by the Python interpreter, but if I type :
import sys
print sys.path
I can't see my directory listed.
What am I doing wrong ?

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"

ImportError when trying to import a module in Sublime Text 2 (Python)

I downloaded xlwings online, and I've been trying to get it to work within Sublime Text 2. When I type in import xlwings into IDLE, it works perfectly. However, when I do the same in Sublime Text 2, it says "ImportError: No module named xlwings."
The xlwings folder is installed in C:/Python27/Lib/site-packages/xlwings. Here is what I have for python.sublime-build:
{
"cmd": ["C:\\python27\\python.exe", "-u", "$file"],
"env":
{
"PYTHONPATH":"C:\\Python27\\Lib\\site-packages"
},
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"target": "console_exec"
}
Thanks for any help!

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 :)

Categories

Resources