Creating and Finding a Python file

Joined
Aug 17, 2010
Messages
247
Reaction score
8
Points
18
Your Mac's Specs
2011 Mac mini and 2010 13" MacBook Pro, iPhone 4 and iPad
I picked up the O'Reilly Learning Python book the other day because I'm interested in learning how to write python code and now I'm stuck. I've written a file with a few basic statements the book provided and I can't figure out how to make Python find the file from system command line using Terminal. I've created the file in Text Wrangler and saved it with a .py extension and its in my documents folder under my user name. I can't seem to get the file to load or work. The statements in the file I created named script1.py are as follows
Code:
print(2*10)
x = 'Spam '!
print (x*8)
The statements I wrote run fine in the interactive python. Here is what I'm doing to open the file.
Code:
opened terminal - python script1.py
python: can't open file 'script1.py': [Errno 2] No such file or directory
This is odd for me because that is what the book is saying to do but it's won't find the file. I understand the code and creating files and such, I have a problem using it to locate the directory or file on my Mac. I'm not sure if this is the same thing or not but it also mentions importing files as well. Here's what I did to try to import the file
Code:
opened terminal - python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import script1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named script1
>>>
The book mentions something about setting a PATH and I'm sure that is what I'm missing here. Can anyone help me out?
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
You need to either be in the directory or give Python the full path to the script. So, do one of the following:
Code:
cd ~/Documents
python script1.py
or
Code:
python ~/Documents/script1.py
 
Joined
Aug 2, 2005
Messages
1,229
Reaction score
75
Points
48
Your Mac's Specs
2.6GHz Core i7 15" MacBook Pro - 8GB DDR3 SDRAM - 750GB 7200 RPM HDD - GeForce 650M GT 1GB VRAM
Code:
python ~/Documents/script1.py
You should probably read a little about how the UNIX command line interface works before trying to use Python from Terminal. You can skim the basics of basics here. Just ignore the first part about how to open a new window.
 
OP
Lbatson21
Joined
Aug 17, 2010
Messages
247
Reaction score
8
Points
18
Your Mac's Specs
2011 Mac mini and 2010 13" MacBook Pro, iPhone 4 and iPad
Awesome. Got it working. Thanks for the advise and link
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Moved to a more appropriate forum.

What might actually be helpful too is to use an IDE or editor that let's you run code instead of having to use the Terminal (as a *nix nerd though, I don't want to push people away from the CLI!). You mention that you're using TextWrangler, which I also use, so I can tell you that you can run your code from there. Simply go to the "#!" menu > Run. You can even assign it a keyboard shortcut in Preferences > Menus > find the Run menu item > "Set Key..."
 
OP
Lbatson21
Joined
Aug 17, 2010
Messages
247
Reaction score
8
Points
18
Your Mac's Specs
2011 Mac mini and 2010 13" MacBook Pro, iPhone 4 and iPad
Yea thats great that I can use the editor for that. I'm also glad I've figured out file directories and how the terminal works now. Up until now it confused the bajeezus out of me.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top