I am using sublime text3 for Python on Ubuntu 18.04. But I can't print anything when I am taking input. Like I can print this
print("Hello")
but I can't print this
test= int(input())
print(test)
I give input but it doesn't show any output.
My python build code is
{
"cmd": ["/usr/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
I have followed this Sublime Text 2 console input
But not working. Here is the screenshot using SublimeRELP package
In this problem, I use sublimeREPL as like this. But again this problem occurs. Accidently I found that I was pressing my num Enter but it worked with my general Enter button. After pressing the general Enter button, it works fine.
Related
I am trying to set up a python 3 build path for sublime text 3 on a windows 10 pc. The system says that it cannot find the specified file, but I have already added the path and set the build system for python3. The error message I get when I try to build is below..
[WinError 2] The system cannot find the file specified
[cmd: ['python3', '-i', '-u', 'C:\\Users\\strinkjr\\Desktop\\Python Stuff\\errorSearch.py']]
[dir: C:\Users\strinkjr\Desktop\Python Stuff]
[path: C:\Users\strinkjr\Desktop\Python Stuff\]
[Finished]
My build environment file is as follows:
{
"cmd": ["python3", "-i", "-u", "$file"],
"file_regex": "^[ ]File \"C:/Users/strinkjr/AppData/Local/Programs/Python/Python36/python.exe\", line ([0-9]*)",
"selector": "source.python"
}
I am unsure if I set up the path incorrectly or if I set up the build environment incorrectly. (Maybe both)
There are a couple of overall problems you're having that are standing in your way here.
The first is that you didn't set the PATH correctly. The build output shows you the PATH as it's currently defined as far as the command execution is concerned:
[path: C:\Users\strinkjr\Desktop\Python Stuff\]
The PATH is the list of locations where windows will look for the program that you're trying to execute, so unless there is a python3.exe in this directory you've accidentally set the PATH to the location of the files that you're running and not the interpreter that's used to run them.
Secondly you're passing -i to the Python interpreter to get it to drop into interactive mode once it's done executing the script. Sublime doesn't let you interact with programs that you execute from within a sublime-build, so if you do this once your program finishes executing and goes into interactive mode, it's going to be effectively hung waiting for you to provide it input that you can't provide.
Your build system also contains this file_regex entry:
"file_regex": "^[ ]File \"C:/Users/strinkjr/AppData/Local/Programs/Python/Python36/python.exe\", line ([0-9]*)",
In a sublime-build file, the file_regex is used to be able to detect what lines in the program's output are errors so that Sublime can allow you to navigate between errors or flag them with inline errors if you have that option turned on.
Although this won't stop your programs from running, it will stop Sublime from being able to recognize errors because the name of the file is never going to match.
I would try a sublime-build file something like the following and see if that works better for you:
{
"shell_cmd": "python3 -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {
"PYTHONIOENCODING": "utf-8",
"PATH": "$PATH;C:/Users/strinkjr/AppData/Local/Programs/Python/Python36/"
},
}
This removes the -i argument to stop the interpreter from going interactive in order to stop any problems and uses shell_cmd instead of cmd to provide the command, which changes the format slightly.
The file_regex here is one that will match regular python errors, which is similar to the one you already provided but without the reference to the Python executable.
The big addition here is a couple of environment variables. The first one ensures that Python knows that it should use utf-8 to generate output, since that's what the Sublime console is expecting it to use. That stops you from getting potential errors if you try to display non-ascii output.
This also applies a new PATH that includes the existing path and also adds to it the path that looks like it might be where your Python is installed based on the files you were already using.
That part may need adjustment if the location is not correct; alternatively you can remove the PATH portion of the sublime-build and modify your PATH environment variable as appropriate instead. Note that you may need to restart Sublime if you do that in order for it to see the change.
This is my first question here.
I am running the following:
Sublime Text 3
OS 10
Python 3.6
I am trying to open and print on Sublime a .txt file. I am using the following code:
myfile = open("/stanford.txt", "r")
contents = myfile.read()
and this is the error message that I get:
Traceback (most recent call last):
File "/Users/me/python/Test1.py", line 11, in <module>
contents = myfile.read()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 173: ordinal not in range(128)
[Finished in 0.1s with exit code 1]
The 2 lines of code above work on Sublime Text 3 when I run it using Python 2.7. It also works when I run them on Terminal (using Python3). However, they do not work when I try them in IDLE: there I get exactly that same error message that Sublime Text 3 is giving me when I run them in Python3.
This is my build configuration on Sublime Text 3:
{
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.6/bin/python3", "$file"],
"selector": "source.python",
"file_regex": "file \"(...*?)\", line([0-9]+)"
}
I have also tried adding "PYTHONIOENCODING" but still the same error message:
{
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.6/bin/python3", "$file"],
"selector": "source.python",
"file_regex": "file \"(...*?)\", line([0-9]+)",
"env": {"PYTHONIOENCODING": "utf8"}
}
(I have also tried "utf-8" with the dash instead of the above. Same error message).
What do I need to do so that Sublime Text 3 can read the file?
Thank you
Edit: I don't think this issue is a duplicate of the other? This works on Terminal for me but not on Sublime Text 3.
Edit2: I have noticed that if I remove the apostrophes (') contained in the text file, I get to open the file with no problems. The error only occurs when I add the apostrophes back to the text file.
Ok, I finally figured it out.
I added the following to the open function:
, encoding="utf-8")
And it worked. I'm still trying to figure out a way to make this permanent to my Python3 on Sublime.
I can't get Python 3 to print out anything while running a script in Sublime Text. I can get it to print out after the script has finished, but I need it to print as it goes.
Here's some example code
import time
for x in range(10):
print("Test")
time.sleep(1)
Using Python 3 as the build system, I get nothing for 10 seconds, and then 10 "Tests" printed all at once.
If I run the same script using the Python 2 build system, I get 1 "Test" printed out each second, which is what I want.
Similarly if I run the script using "python3 script.py" in the terminal I get 1 "Test" printed out each second.
Can anyone tell me how to make the build system in Sublime Text 3 print out Python 3 while it runs?
You have to open your Python 3 build system configuration file, and ensure that the -u option is supplied to the command line to be executed.
For example:
# ~/.config/sublime-text-3/Packages/User/Python3.sublime-build
{
"cmd": ["/usr/bin/python3.5", "-u", "$file"],
"selector": "source.python",
"file_regex": "file \"(...*?)\", line ([0-9]+)"
}
Quoting the documentation:
-u
Force the binary layer of the stdout and stderr streams (which is
available as their buffer attribute) to be unbuffered. The text I/O
layer will still be line-buffered if writing to the console, or
block-buffered if redirected to a non-interactive file.
With this option, the ouptut of your Python script will be correctly displayed into Sublime Text while running.
I am using the gnuplot package from PackageControl in Sublime Text 3 on Windows. My gnuplot code successfully builds when run from Sublime Text, but if using a terminal that outputs the plot to a graphics window (such as the default wxt), the window appears and closes immediately.
Presumably there is a way to edit the gnuplot.sublime-build file to keep the plot window from closing, but I have been unsuccessful so far.
My Sublime Text build file for gnuplot currently looks like this:
{
"cmd": ["gnuplot", "$file"],
"working_dir": "${project_path:${folder}}",
"selector": "source.gnuplot",
"variants": [
{
"cmd": ["start", "gnuplot", "-persist", "$file"],
"shell": true
}
]
}
Unfortunately, the -persist has no effect.
I had this problem as well... I ended up just adding:
pause -1
To the end of my script.
I followed these steps to run Python 3 on Sublime Text 3.
Select the menu Tools > Build > New Build System and I entered the following:
{
"cmd": ["python3", "$file"]
, "selector": "source.python"
, "file_regex": "file \"(...*?)\", line ([0-9]+)"
}
After that, I saved it to the following (Mac-specific) directory: ~/Library/Application Support/Sublime Text 3/Packages/User
but I'm getting this error when I'm trying to run my code on Python 3 in Sublime:
[Errno 2] No such file or directory: 'python3'
You need to provide the full path to python3, since Sublime Text does not read your ~/.bash_profile file. Open up Terminal, type which python3, and use that full path:
{
"cmd": ["path/to/python3", "$file"],
"selector": "source.python",
"file_regex": "file \"(...*?)\", line ([0-9]+)"
}
This is the snippet that I've been using. It's a slight variation to Andrew's solution, such that python3 is dynamically located by consulting the UNIX environment's PATH setting (not unlike how you would do the same inside a Python shell script; e.g.: '#! /usr/bin/env python3').
This snippet also uses "shell_cmd" instead of "cmd", which sublime-text-3 has seemingly switched to.
{
"shell_cmd": "/usr/bin/env python3 ${file}",
"selector": "source.python",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"working_dir": "${file_path}",
}
I saved mine in ".../Packages/User/Python3.sublime-build". I hope this helps you. =:)
Thanks for your question. I began to learn python a couple days ago, and I am stuck with the same problem that you have met.Just as Andrew said above,it is “path problem”. I would like to share the code that I used to get python3 on sublime3.
For MacOS user:
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "utf8",
"path": "/usr/local/Frameworks/Python.framework/Versions/3.3/bin/"
}
and save the file as Python3.sublime-build.
I deeply recommended the book "A byte of python " for the new beginner of python.This book contributes a lot to my answer for this question.