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

In summary, the conversation is about the confusion the person is having with setting up Python on their computer. They are using SublimeText as an editor and following instructions from a book. The book initially told them to configure Sublime for Python3 by typing in a specific code and saving it in the User file. However, when the book later tells them to save a document in a specific folder, they are unable to do so in Sublime. The person is also unsure of how to properly ask for help due to their limited knowledge. They are using a Mac OS and are trying to stick to the instructions given by the book, but may need to seek help from other sources."
  • #1
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: 493
Technology news on Phys.org
  • #2
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]
 
  • #3
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: 483
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 
  • #7
Ok great thanks again Mark
 
  • #8
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.
 
  • #9
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.
 

1. What is Python and why is it used?

Python is an interpreted, high-level programming language that is widely used for web development, data analysis, and scientific computing. It is known for its simple syntax, readability, and powerful libraries, making it a popular choice for beginners and professionals alike.

2. How do I install Python on my computer?

To install Python, you can go to the official Python website and download the latest version for your operating system. Alternatively, you can use a package manager like Anaconda, which also includes popular libraries and tools for data science.

3. What are the basic data types in Python?

Python has four built-in data types: integers, floats, strings, and booleans. Integers are whole numbers, floats are numbers with decimals, strings are sequences of characters, and booleans represent truth values (True or False).

4. How do I write and run a Python program?

To write a Python program, you can use a text editor or an integrated development environment (IDE) like PyCharm. Once you have written your code, you can save it with a .py extension and run it from the command line by typing "python filename.py".

5. Where can I find help for Python programming?

There are many resources available for learning and getting help with Python, such as online tutorials, documentation, forums, and online communities. The official Python website also has a comprehensive documentation section with tutorials, guides, and references for different versions of Python.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
6
Views
916
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
8
Views
876
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top