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 ?
Related
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"},
}
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
I have installed the python 3.5 interpretor in my device (Windows).
Can anybody guide me through the process of using packages to run it like SublimeREPL?
Yes, you can use any Python version you want to run programs from Sublime - you just need to define a new build system. Select Tools -> Build System -> New Build System, then delete its contents and replace it with:
{
"cmd": ["C:/Python35/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
assuming that C:/Python35/python.exe is the correct path. If python.exe resides someplace else, just put in the correct path, using forward slashes / instead of the Windows standard backward slashes \.
Save the file as Packages/User/Python3.sublime-build, where Packages is the folder opened by selecting Preferences -> Browse Packages... - Sublime should already automatically save it in the right directory. Now, there will be a Tools -> Build System -> Python3 option that you can select for running files with Python 3.
For details on setting up SublimeREPL with Python 3, please follow the instructions in my answer here.
if you have installed python3 and SublimeREPL, you can try setting up key bindings with the correct path to the python3 file.
[
{
"keys":["super+ctrl+r"],
"command": "repl_open",
"caption": "Python 3.6 - Open File",
"id": "repl_python",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["The directory to your python3.6 file", "-i", "$file"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
}
]
You can try by copying this code into your /Sublime Text 3/Preferences/Key Bindings/
Hope this helps!
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!
I use python portable along with sublime portable of a memory stick and decided to install sublime REPL however when trying to use python portable as the build path for sublimerepl it does not specify it.
To put it simply I can't specify the sublimerepl python path like I can with straight sublime text!
I have tried adding the following into SublimeREPL.sublime-settings
"default_extend_env": {"PATH": "{PATH}:C:\\Users\\Andy\\Documents\\_Main\\M-Stick Backups\\08.07.13\\Computing\\Python Portable\\Portable Python 2.7.3.1\\App\\python.exe"},
I have also tried changing:
"cmd": ["python", "-i", "-u"],
to
"cmd": ["C:\\Users\\Andy\\Documents\\_Main\\M-Stick Backups\\08.07.13\\Computing\\Python Portable\\Portable Python 2.7.3.1\\App\\python.exe", "-u", "$file"],
within Python/Main.sublime-menu
Note in normal sublime I add the following to Python.sublime-build:
{"cmd": ["C:\\Users\\Andy\\Documents\\_Main\\M-Stick Backups\\08.07.13\\Computing\\Python Portable\\Portable Python 2.7.3.1\\App\\python.exe", "-u", "$file"],"file_regex": "^[ ]*Iile \"(...*?)\", line ([0-9]*)","selector": "source.python"}
Any ideas?
Thanks,
Andrew
Is it possible you're changing the wrong line of code in /Packages/SublimeREPL/config/Python/Main.sublime-menu? If you wish to run REPL on the current file,
{"command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "d",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["C:\\Users\\Andy\\Documents\\_Main\\M-Stick Backups\\08.07.13\\Computing\\Python Portable\\Portable Python 2.7.3.1\\App\\python.exe", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
is what the modified section should look like. (It appears you might be modifying the command which opens the interpreter in interactive mode as opposed to running the current file).
Thanks to Hower... I installed the package control, then the sublimePREPL package and, then in the keybinding (under preferences) I wrote this int the user file:
[
{"keys": ["ctrl+b"], "command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "d",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["C:\\Users\\giova\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}}
]