Where to save my scripts using OS X so i can Import them?

A forum for general discussion of the Python programming language.

Where to save my scripts using OS X so i can Import them?

Postby chrisngn90 » Mon Jul 08, 2013 11:34 pm

Hello, I am using OS X.

Where do I save my scripts so that I can import them? I get an error when I try to import: "ImportError: no module named <sample>"
Last edited by Yoriz on Mon Jul 08, 2013 11:37 pm, edited 1 time in total.
Reason: Improved the title
chrisngn90
 
Posts: 6
Joined: Mon Jul 08, 2013 11:22 pm

Re: Where to save my scripts using OS X so i can Import them

Postby chrisngn90 » Tue Jul 09, 2013 12:44 am

I am using this book to learn: http://www.openbookproject.net/thinkcs/ ... /ch03.html

It says to begin the Python shell in the same directory as where the file is saved. I am not sure what this means. Just search for "directory" to see the instructions.
chrisngn90
 
Posts: 6
Joined: Mon Jul 08, 2013 11:22 pm

Re: Where to save my scripts using OS X so i can Import them

Postby metulburr » Tue Jul 09, 2013 12:53 am

make sure to read
viewtopic.php?f=6&t=145

When you post regarding code, amke sure to post the code you tried, with the related traceback (error) that you get ( the entire error)

Where do I save my scripts so that I can import them? I get an error when I try to import: "ImportError: no module named <sample>"

that depends on what modules you are importing, where you put them, etc. If you are importing a module from the same directory, just
Code: Select all
import <module>
(where <module> is the name of the script without the .py extension)
if you are trying to import from stdlib, then also the same:
Code: Select all
import time

(where time is the module example)

for more info regarding imports check out the modules tutorial


It says to begin the Python shell in the same directory as where the file is saved. I am not sure what this means.

http://lifehacker.com/5633909/who-needs ... t-anything

EDIT: oh yeah i forgot to mention, the
Code: Select all
from <module> import *
is bad practice. It floods the namespace
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Where to save my scripts using OS X so i can Import them

Postby chrisngn90 » Tue Jul 09, 2013 1:57 am

To import, I used:
Code: Select all
from tryme3 import *


Traceback:
Code: Select all
  File "<stdin>", line 1, in <module>
ImportError: No module named function


My task was just to import a script that the book suggested I write. I saved it on my Desktop and I also tried saving it in My Documents, but the result is the same, the error. I do not know how to save my script in the same directory as the Python shell, so that I can import it like the example in the book. And thanks for clearing up how to post well.
chrisngn90
 
Posts: 6
Joined: Mon Jul 08, 2013 11:22 pm

Re: Where to save my scripts using OS X so i can Import them

Postby metulburr » Tue Jul 09, 2013 2:38 am

My task was just to import a script that the book suggested I write. I saved it on my Desktop and I also tried saving it in My Documents, but the result is the same, the error. I do not know how to save my script in the same directory as the Python shell, so that I can import it like the example in the book. And thanks for clearing up how to post well.


ok here is an example of creating/using a module. This is all done via terminal in linux. vim is the text editor i used to write the files, and the cat displays the file (for you to see what I wrote). All of these files are in the directory "temp" that i created. The ls command given displays the 3 files in the directory, the 2 i wrote and the .pyc that python wrote when the module was imported. All these files are in the same directory. The last command given starts up the python interpreter. the directory i am in when i start it is the directory the interpreter is in, (shown via os.listdir)
Code: Select all
metulburr@ubuntu:~$ mkdir temp
metulburr@ubuntu:~$ cd temp
metulburr@ubuntu:~/temp$ ls
metulburr@ubuntu:~/temp$ sudo vim test_module.py
[sudo] password for metulburr:
metulburr@ubuntu:~/temp$ cat test_module.py
def adder(x,y):
    return x + y

metulburr@ubuntu:~/temp$ python test_module.py
metulburr@ubuntu:~/temp$ sudo vim test.py
metulburr@ubuntu:~/temp$ cat test.py
import test_module

total = test_module.adder(1,3)
print(total)
metulburr@ubuntu:~/temp$ python test.py
4
metulburr@ubuntu:~/temp$ ls
test_module.py  test_module.pyc  test.py
metulburr@ubuntu:~/temp$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.listdir('.')
['test.py', 'test_module.pyc', 'test_module.py']
>>>

New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Where to save my scripts using OS X so i can Import them

Postby chrisngn90 » Tue Jul 09, 2013 11:12 am

ok here is an example of creating/using a module. This is all done via terminal in linux. vim is the text editor i used to write the files, and the cat displays the file (for you to see what I wrote). All of these files are in the directory "temp" that i created. The ls command given displays the 3 files in the directory, the 2 i wrote and the .pyc that python wrote when the module was imported. All these files are in the same directory. The last command given starts up the python interpreter. the directory i am in when i start it is the directory the interpreter is in, (shown via os.listdir)


[quote]metulburr@ubuntu:~$ mkdir temp
metulburr@ubuntu:~$ cd temp
metulburr@ubuntu:~/temp$ ls
metulburr@ubuntu:~/temp$ sudo vim test_module.py
[sudo] password for metulburr:
metulburr@ubuntu:~/temp$ cat test_module.py [/quote

I am running on Mac OS X, and neither the python shell nor the built in terminal displays what I quoted above. From what I understand, you created some modules and then imported them. I don't know what you mean by the directory "temp" that you created. I don't know what a "cat" is. But I did get the python shell to import my scripts!

First, I imported os:
Code: Select all
import os


Then, I just imported whatever scripts I have written:
Code: Select all
import tryme3


When I did this, my file "tryme3.py" was converted into "tryme3.pyc". I don't know the difference. Should I be saving files ending in .pyc from now on? Also, please explain to me why "import os" allowed me to import other scripts?
chrisngn90
 
Posts: 6
Joined: Mon Jul 08, 2013 11:22 pm

Re: Where to save my scripts using OS X so i can Import them

Postby metulburr » Tue Jul 09, 2013 1:03 pm

and neither the python shell nor the built in terminal displays what I quoted above.

both linux and mac are unix based, and hence should have bash and such. Those commands should work in a mac terminal. I dont know though for sure as i would never buy a mac, and hence cannot test it.

When I did this, my file "tryme3.py" was converted into "tryme3.pyc". I don't know the difference. Should I be saving files ending in .pyc from now on?

http://lmgtfy.com/?q=python+what+are+.pyc+files

Also, please explain to me why "import os" allowed me to import other scripts?

it doesn't
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Where to save my scripts using OS X so i can Import them

Postby chrisngn90 » Tue Jul 09, 2013 6:43 pm

Well thanks!
chrisngn90
 
Posts: 6
Joined: Mon Jul 08, 2013 11:22 pm

Re: Where to save my scripts using OS X so i can Import them

Postby ochichinyezaboombwa » Tue Jul 09, 2013 10:10 pm

If you are using this book and didn't skip anything, you must be working in a UNIX shell (on MAC, it's the app called "Terminal").

In the shell, you must have a prompt -- something like this:
Code: Select all
bash-3.2$


That's where you enter commands, such as:
Code: Select all
bash-3.2$ python
>>>


Directory is a place on your hard disk. You are probably familiar with such directories as /Applications/ and /Applications/Utilities/. Desktop is also just a directory.

At any moment, when you are using Terminal and are in a UNIX shell, you in fact are in some directory.
Find out what is your current directory:
Code: Select all
bash-3.2$ pwd

Find out what is in your current directory:
Code: Select all
bash-3.2$ ls

Find out whether a particular file is in your current directory:
Code: Select all
bash-3.2$ ls tryme3.py

If the resul is the name of the file, then
Code: Select all
from tryme3 import *
will work, otherwise ("No such file or directory") it won't.

Directories are a great mechanism of keeping things organized. A librarian puts books belonging to different categories to different shelf for a good reason: it would be a mess / impossible to find anything otherwise.
For the same reason, it is a really bad habit of lazy people to keep things in their Desktop directory. Instead,
Code: Select all
bash-3.2$ cd
bash-3.2$ mkdir thinkpython
bash-3.2$ cd thinkpython/
bash-3.2$ mv ~/Desktop/*.py ./

and work in this directory while learning Python.


Read more about Unix file system and basic UNIX commands, e.g. here.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Where to save my scripts using OS X so i can Import them

Postby chrisngn90 » Wed Jul 10, 2013 4:48 pm

Thanks! That clears up a lot!
chrisngn90
 
Posts: 6
Joined: Mon Jul 08, 2013 11:22 pm


Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

cron