New and trying to get python set up with Sublime Text [duplicate] - python

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.

Related

Python using wrong version on Mac

I'm new to Python, I'm using version 3.8.2 on Sublime Text and Mac.
I aliased the python version from 2.7 to 3 on the command terminal and it works fine whenever I check for the version on the terminal window. However, when I run some code on Sublime Test and check for the path used, it uses the 2.7 version.
I tried to run the script using the terminal window but it couldn't find the script because of system path issues. I'm now sure how to edit or where to change that.
I tried editing the Sublime Build settings with the below but it didn't work and I'm not sure if this is how to fix it.
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"quiet": true
}
My script is located in the User > Documents > Python folder. Python is installed in Mac's Application folder.
Attached is a sample of my code after running it and the terminal after running it.
Cmd Terminal - running my_module.py script
Sublime Text - running courses.py and checking system path
You need to change your location where python is. Python 3 is not on your bin.
Go to Tools > Build System > New Build System and paste this in and save it as Python3.sublime-build
{
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.8/bin/python3", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"
}
Then go back and change your build system to the new Python3.
Tools > Build System > Python3
The run this test script with Command B
thing = 'world'
print(f'hello {thing}')
and if it runs correctly, then it is running python 3.

Sublime Text 3 Build System issue

I have just started playing around with Sublime Text 3 and i am trying to create a build system for python 3.6.3. I have created a .sublime-build file with the name Python3.6.3.sublime-build which contains the following code :
{
"cmd": ["C:/Python32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"quiet": true
}
Whenever I try to build a python file I receive this message at the bottom of the Sublime, in the place where the results from the program should be displayed:
[WinError 2] Finding the specified file was not possible by the system
[cmd: ['C:/Python32/python.exe', '-u',
'C:\\Users\\user\\Desktop\\String_examples.py']]
[dir: C:\Users\user\Desktop]
[path: C:\Program Files (x86)\ARM\ADSv1_2\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\MATLAB\R2012b\runtime\win64;C:\Program Files\MATLAB\R2012b\bin]
Sorry but the [WinError 2] message was translated in english so it may not be the exact error text.
Moreover when I open sublime I receive this error window:
Sublime Text
I am running Windows 7 if that plays any role.
Could someone please explain what did i do wrong because i am kind of stuck with this?
Thanks in advance...!
After several hours of search on the Internet i found that we need to find the right path of the python.exe file and use double backslash instead of single backslash (\) when declaring the file's path. The following answers refer to Windows users.
So the correct code is:
{
"cmd": ["C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"quiet": true
}
If we have the following it will not work:
{
"cmd": ["C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"quiet": true
}
A second way to create the python build system is with this code :
{
"cmd": ["python", "-u", "$file"],
"file_regex": "^ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"
}
In this code notice that the file's path is absent. We only refer the file's name. So Windows will not know where to find the python.exe file and we will have an error message on the Sublime console like the one I describe in my question above. So we need to do the following steps:
1.Go here C:\Users\user\AppData\Local\Programs\Python\Python36-32 and copy this path
Note: Here user is the user name of my computer. It will be different in your case.
(AppData folder maybe hidden check the show hidden files option)
Now right click on Computer icon and click on PROPERTIES's option. Then select ADVANCED SYSTEM settings in left sidebar.
Now click on [Environment Variables] and then under [System variables] > [select variable] with name [Path].
Click Edit button and then in [Path] value field keep the already existing data and go to end of the line. Type semicolon ( ; ) and don't erase anything just paste the path of Python directory that you copied after the semicolon at the end of the line.
( It should be something like this ;C:\Users\user\AppData\Local\Programs\Python\Python36-32 )
Then save the changes and close the Sublime Text. Re-open Sublime and it should be ok.
Notice that the code "selector": "source.python"
was not included in the first explation above. Instead there was this code
"quiet": true
but it does not play any role in the solution of this problem.
After completing the procedure explained second, we have a bonus result. We can use the command python on the cmd window without receiving an error because now Windows knows where to find the python.exe file (the path for this file is now included in the PATH parameter of the Windows system).

linking sublimetext 2 to anaconda python mac os [duplicate]

I want to set up a complete Python IDE in Sublime Text 2.
I want to know how to run the Python code from within the editor. Is it done using build system? How do I do it ?
Tools -> Build System -> (choose) Python then:
To Run:
Tools -> Build
-or-
Ctrl + B
CMD + B (OSX)
This would start your file in the console which should be at the bottom of the editor.
To Stop:
Ctrl + Break or Tools -> Cancel Build
Fn + C (OSX)
You can find out where your Break key is here: http://en.wikipedia.org/wiki/Break_key.
Note: CTRL + C will NOT work.
What to do when Ctrl + Break does not work:
Go to:
Preferences -> Key Bindings - User
and paste the line below:
{"keys": ["ctrl+shift+c"], "command": "exec", "args": {"kill": true} }
Now, you can use ctrl+shift+c instead of CTRL+BREAK
Edit %APPDATA%\Sublime Text 2\Python\Python.sublime-build
Change content to:
{
"cmd": ["C:\\python27\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
change the "c:\python27" part to any version of python you have in your system.
On Mac OS X, save your file with a .py extension. Press ⌘ + B. It runs in a window below.
To RUN press CtrlB (answer by matiit)
But when CtrlB does not work, Sublime Text probably can't find the Python Interpreter. When trying to run your program, see the log and find the reference to Python in path.
[cmd: [u'python', u'-u', u'C:\\scripts\\test.py']]
[path: ...;C:\Python27 32bit;...]
The point is that it tries to run python via command line, the cmd looks like:
python -u C:\scripts\test.py
If you can't run python from cmd, Sublime Text can't too.
(Try it yourself in cmd, type python in it and run it, python commandline should appear)
SOLUTION
You can either change the Sublime Text build formula or the System %PATH%.
To set your %PATH%:
*You will need to restart your editor to load new %PATH%
Run Command Line* and enter this command: *needs to be run as administrator
SETX /M PATH "%PATH%;<python_folder>"
for example: SETX /M PATH "%PATH%;C:\Python27;C:\Python27\Scripts"
OR manually: (preferable)
Add ;C:\Python27;C:\Python27\Scripts at the end of the string.
To set the interpreter's path without messing with System %PATH% see this answer by ppy.
You can use SublimeREPL (you need to have Package Control installed first).
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",
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
[ This applies to ST3 (Win), not sure about ST2 ]
To have the output visible in Sublime as another file (+ one for errors), do this:
Create a new build system: Tools > Build Systems > New Build System...
Use the following configuration:
{
"cmd": ["python.exe", "$file", "1>", "$file_name.__STDOUT__.txt", "2>", "$file_name.__STDERR__.txt"],
"selector": "source.python",
"shell": true,
"working_dir": "$file_dir"
}
For your Python file select the above build system configuration file: Tools > Build Systems > {your_new_build_system_filename}
ctrl + b
Now, next to your file, e.g. "file.py" you'll have "file.__STDOUT__.py" and "file.__STDERR__.py" (for errors, if any)
If you split your window into 3 columns, or a grid, you'll see the result immediately, without a need to switch panels / windows
In python v3.x you should go to : Tools->Build System->New Build System.
Then, it pop up the untitled.sublime-build window in sublime text editor.Enter setting as:
{
"cmd": ["path_to_the_python.exe","-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
To see the path, Type following in terminal as:
python
>>> import sys
>>>print(sys.executable)
You can make more than one Build System but it should default save inside Packages of Sublime text with .sublime-build extension.
Then, select the new Build System and press cltr+b or other based on your os.
Cool U guys, I just found this:
http://ptomato.wordpress.com/2012/02/09/geek-tip-running-python-guis-in-sublime-text-2/
It explains (like one of the answers above) how to edit this exec.py in the default directory.
I had the problem that my PYTHON UI APPLICATION would not start. I commented out the last line from the following snipped:
# Hide the console window on Windows
startupinfo = None
if os.name == "nt":
startupinfo = subprocess.STARTUPINFO()
#startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
and, taaadaaaa, I could start my app by pressing Ctrl+B. Funny line anyways, uh? And a big thank you to whoever wrote that article ;-)
I solved this problem :
> Preferences –> Browse Packages –> Default
Open the exec.py file, near line 41-42, the code should look like this :
for k, v in proc_env.iteritems():
proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())
then delete it or edit it as :
try:
for k, v in proc_env.iteritems():
proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())
except:
print 'foobar'
I ran into the same problem today. And here is how I managed to run python code in Sublime Text 3:
Press Ctrl + B (for Mac, ⌘ + B) to start build system. It should execute the file now.
Follow this answer to understand how to customise build system.
What you need to do next is replace the content in Python.sublime-build to
{
"cmd": ["/usr/local/bin/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
}
You can of course further customise it to something that works for you.
seems the Ctrl+Break doesn't work on me, neither the Preference - User...
use keys, Alt → t → c
You can access the Python console via “View/Show console” or Ctrl+`.
I had the same problem. You probably haven't saved the file yet. Make sure to save your code with .py extension and it should work.
One thing to note about the aforementioned build system: you can write (and use) custom .sublime-build files or even per project build_systems clause (in your project settings). This allows you to do useful things like a fancy test runner with ANSI colors output.
For even more "full IDE" features, you can use the excellent SublimePythonIDE package:
code completion (intel)
jump to definition & object description
proper linting/pep8
supports different interpreters with virtualenv
Disclosure: I've contributed a PR to that package, and I use it all the time, but there are others.
Use a real python console alongside Sublime
Both Sublime's build system and SublimeREPL (the answers above) are limited in that you can't easily interact with the workspace variables after you run your file.
If you want to run a script, then work in a REPL-like fashion (like you would in an IDE), then I recommend having Sublime open alongside an IPython console. Using AutoHotKey (Windows) or AutoKey (Linux), you can set this up such that a single shortcut will copy the filename (or just the selected code) and then paste this in the console to run the file.
Detailed instructions for Linux or Windows

getting sublime text 3 to use anaconda python

So I've installed anaconda to a directory I have privileges for but I can't get sublime text 3 to recognise that the shell is now using anaconda python:
>which python
/local/home/USER/Apps/anaconda/bin/python
when I build with sublime launched from the same shell:
import astropy
print astropy.__file__
it gives a different directory: /soft/python-SL7/lib/python2.7/site-packages/astropy/init.pyc
My .tcshrc file reads:
setenv PATH /local/home/USER/Apps/anaconda/bin:${PATH}
alias subl /local/home/USER/Apps/sublime_text_3/sublime_text
My .bashrc (not that it should be using it) reads:
export PATH="/local/home/sread/Apps/anaconda/bin:$PATH"
Any ideas?
The easiest way is to create a new build system that points to your Anaconda installation. Create a new file in Sublime with JSON syntax and the following contents:
{
"cmd": ["/local/home/USER/Apps/anaconda/bin/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Save the file in your Packages/User directory (should be ~/.config/sublime-text-3/Packages/User) as Anaconda.sublime-build. Finally, select Tools → Build System → Anaconda, and when you hit CtrlB in a Python file it should now run using Anaconda.
If you want to set up SublimeREPL to use Anaconda with IPython in Sublime, you can follow the instructions here to set up the proper menu option (altering the path to suit your environment, of course), and my gist here for setting up SublimeREPL for IPython 4 and Jupyter.
The other answer is correct, but you can also have a per project setting by editing the project file and adding this:
"build_systems":
[
{
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"name": "Anaconda Python Builder",
"selector": "source.python",
"shell_cmd": "\"python3\" -u \"$file\""
}
],
This also has the advantage of not leaving too many build systems in the build menu.

How to get Python 3.4 on Sublime Text 3?

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.

Categories