How do I save a file in Sublime to a specific folder?

  • Thread starter Thread starter DS2C
  • Start date Start date
  • Tags Tags
    Initial Python
Click For Summary

Discussion Overview

The discussion revolves around saving a Python file in Sublime Text to a specific folder, as guided by a learning resource. Participants share their experiences and challenges with the process, including issues with file paths and the functionality of Sublime Text as a text editor versus a Python interpreter.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes following a book's instructions to configure Sublime Text for Python3 but encounters difficulties saving a file to a specific folder.
  • Another participant shares an error message indicating that the specified file cannot be found, suggesting a potential issue with the file path.
  • Some participants clarify the distinction between Sublime Text as a text editor and the Python interpreter, emphasizing that Sublime does not execute code.
  • There is a discussion about the use of different text editors and the necessity of saving files with a .py extension for Python to recognize them.
  • Questions arise regarding the role of the terminal in executing Python files and the function of IDLE as an alternative environment for running Python code.

Areas of Agreement / Disagreement

Participants express varying levels of understanding about the use of Sublime Text and the Python interpreter, with some confusion remaining about how to properly save and execute Python files. There is no consensus on the best approach to resolve the saving issue.

Contextual Notes

Some participants mention limitations in their knowledge of Python and the specific configurations for different operating systems, which may affect their ability to follow instructions accurately.

Who May Find This Useful

Individuals learning Python programming, particularly those using Sublime Text or similar text editors, may find this discussion relevant as it addresses common challenges faced by beginners.

DS2C
Hi guys. Just wanted to see if I could get a point on this problem I'm having.
I'm using a book to learn Python3 and I'm using SublimeText as an editor.
The book initially told me to configure Sublime for Python3 by typing this into a blank Sublime file:
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
}

and to save it wherever it prompted me to (which was User file)

Now the book tells me to File > Save as as document called hello_world.py in a particular folder that I made.
However, when I try to save it to this location in Sublime, it doesn't give me this option. So in Sublime, I save it to Documents. Then I went into Documents and dragged it into the folder that I made.
It then tells me to type this snippet command into Sublime and I should get something out of it. But as you can see in the screenshot, I just get an error. I cropped out my name where it occurred in the error for privacy reasons.
Can someone tell me how to save the file in Sublime into the folder that I created so that I can get this to work?
 

Attachments

  • Screen Shot 2017-11-20 at 7.53.53 PM.png
    Screen Shot 2017-11-20 at 7.53.53 PM.png
    21.4 KB · Views: 578
Technology news on Phys.org
A copy and paste of the code to make it easier to see:

/Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python: can't open file '/Users/NAME/Documents/hello_world.py': [Errno 2] No such file or directory
[Finished in 0.0s with exit code 2]
[cmd: ['/usr/local/bin/python3', '-u', '/Users/NAME/Documents/hello_world.py']]
[dir: /Users/NAME/Documents]
[path: /Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
 
Also, when it's going through the User file, the snippet program will work, as seen in this screenshot. But on the last tab, it just says it's in Documents, and not the folder than I dragged it into.
 

Attachments

  • Screen Shot 2017-11-20 at 8.09.27 PM.png
    Screen Shot 2017-11-20 at 8.09.27 PM.png
    3.5 KB · Views: 551
You don't need an IDE (integrated development environment) or special editor. Any old editor, even Notepad, will work just fine. After you have installed the Python system, you can use your editor to type a source code file. When you're done, save it as, say, test.py. Then open a command prompt window and enter python test.py.
For this to work, you need to have the directory that contains the python interpreter (python.exe) in your Path. The Python documentation tells you what you need to do in the topic "Using the Python Interpreter".

I'm running Windows 7. It appears you're running Linux, and they talk about setting up Python for both Windows and Linux.
 
Hmm not sure what you mean. It sounds similar to what the book says though. What it had me do what to find the location of Python in my terminal for OS X, then save a file in the interpreter which I assume tells the interpreter to use Python. However it then says to make a new folder where I can save my work. But when I "Save As" in the interpreter, it doesn't give me the option to save to where it should (where it told me to save a file in the last step, which was the User file). The reason I'm using this interpreter is because all of the instructions are based off of this one, even though it does mention that other ones work also. I'm just trying to stick to exactly what it says as I don't have enough knowledge to veer off in any way yet.
I hope this makes sense. My knowledge is so limited that I don't even know how to properly ask the question.
 
DS2C said:
Hmm not sure what you mean. It sounds similar to what the book says though. What it had me do what to find the location of Python in my terminal for OS X, then save a file in the interpreter which I assume tells the interpreter to use Python.
I don't have any experience using Python on Mac OSes, so I can't offer much help on that score.

Here's a link to a post on stackexchange that might be helpful - https://stackoverflow.com/questions/23257984/python3-4-on-sublime-text-3.
DS2C said:
However it then says to make a new folder where I can save my work. But when I "Save As" in the interpreter, it doesn't give me the option to save to where it should (where it told me to save a file in the last step, which was the User file).
The reason I'm using this interpreter is because all of the instructions are based off of this one, even though it does mention that other ones work also. I'm just trying to stick to exactly what it says as I don't have enough knowledge to veer off in any way yet.
I hope this makes sense. My knowledge is so limited that I don't even know how to properly ask the question.
 
Ok great thanks again Mark
 
DS2C said:
What it had me do what to find the location of Python in my terminal for OS X, then save a file in the interpreter which I assume tells the interpreter to use Python. However it then says to make a new folder where I can save my work. But when I "Save As" in the interpreter, it doesn't give me the option to save to where it should (where it told me to save a file in the last step, which was the User file).

You seem a bit confused about the different components. Sublime is not an interpreter. It is a text editor (like TextEdit, and like Notepad on Windows), except it has a lot of features helpful to programmers. The Python interpreter is a separate program. It does not have a feature to let you edit or save a file. You use a text editor for that. The Python interpreter is responsible only for running Python commands.

If you have Python 3 properly installed you should be able to run it by typing "python3" in a terminal. You should see a prompt that looks a bit like this:
Code:
$ python3
Python 3.6.2 (default, Oct  2 2017, 16:51:32)
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
(this is on Linux, you'll see slightly different information on Mac OS). You can then type in Python commands at the ">>>" prompt, e.g.:
Code:
>>> 1 + 1
2
>>> print("hello")
hello
>>>
Like @Mark44 said, you can also run a file full of Python commands (code). Python code should be saved in text files, except you should use the extension ".py" instead of ".txt". If you have a file named "test.py" containing Python code (created using any text editor and saved somewhere), then the basic way to run it is to 1) open a terminal, 2) use the "cd" command to change to the folder where you saved the "test.py" file, and 3) type "python3 test.py" to run Python on the file.

This means that, in the basic environment for programming in Python, you would normally be running two programs at the same time: the text editor (to write the Python code and save it) and the terminal (to run Python on text files containing Python code that you've written).

Some text editors specialised for programming have the ability to directly start the interpreters for programming languages, so that you don't need to open a terminal to run the code. It sounds like your book is explaining how to configure the Sublime editor to do this. I'd go along with @Mark44's advice to not worry about this. It's a useful feature once you have some experience, but it's an unnecessary distraction if you're a beginner and in your case it just seems to be confusing you and obscuring what Python is and how it works.
 
Very helpful, thank you.
So to my understanding, you can use ANY "notepad" to write code. You then save the file with a .py extension which says that it is a Python file. Then, you would open said file with Terminal, and Terminal will run the program because the file has a .py extension?
So what then, is an interpreter? Is this what Terminal is for OS X?
 
  • #10
Also, what is IDLE through Python? Is this a text file also? I tried a print command and it executed it in IDLE. Would this eliminate the need for using Terminal?
I tried using TextEdit and Notes to write the same command and save it as a .py file, but it wouldn't let me.
 
  • #11
DS2C said:
Very helpful, thank you.
So to my understanding, you can use ANY "notepad" to write code. You then save the file with a .py extension which says that it is a Python file. Then, you would open said file with Terminal, and Terminal will run the program because the file has a .py extension? ...

Yes, but two things you may be missing - first you must make sure the file is "executable". I don't recall how to do that on OS X (I've been on Linux for a while now), but it can be done from the terminal with:

Below from http://blog.manbolo.com/2014/09/27/use-python-effectively-on-os-x#p5

If you want to call directly your script without typing python, add the proper permission on your script:

Code:
 $ chmod u+x myscript.py

Second, you need to include a "shebang" as the firt line of the text file - this will instruct the Terminal where the Python resources are on your computer . For OS X it is:

And add the following shebang at the beginning of your script:
Code:
 #!/usr/bin/env python

So what then, is an interpreter? Is this what Terminal is for OS X?

So you see from above, the Python interpreter resides on your system. The terminal will call upon it when it opens your <name>.py file and sees that shebang line.
DS2C said:
Also, what is IDLE through Python? Is this a text file also? I tried a print command and it executed it in IDLE. Would this eliminate the need for using Terminal? ...

I think that IDLE automatically knows to call on those Python resources, since it is a Python specific application. That is convenient during development, but later, you will probably want to run from the terminal, and.or from cron (starts a program at a scheduled date/time).

I use Sublime on my Linux system, and I didn't have any of the configuration problems you mentioned, I was able to save my script anywhere I wanted. And then run it from terminal, by loading from that location. Maybe there are some OS X specific config that needs attention, you might try to find a Sublime forum or OS X related forum for those details?
 
  • #12
DS2C said:
So what then, is an interpreter? Is this what Terminal is for OS X?
An interpreter is a program (application) that reads your code, interprets it and executes it line by line. On Mac OS this would be /usr/bin/python if the instructions NTL2009 gave are correct. The Mac OS Terminal is an application that runs a shell that gives you a text-based command line from which you can enter commands to a Linux-like system. In effect, the Terminal gives you a window into a Linux-like text-based command-line environment.

Back in the old days when graphical OS's like Mac OS and Windows and the various Linux graphical environments didn't exist, programmers used text-only video terminals that provided a command-line interface like this. In the really ancient days they used teletype terminals that displayed everything by printing it on a continuous roll of paper, like a typewriter.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
4
Views
5K