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>"
It says to begin the Python shell in the same directory as where the file is saved. I am not sure what this means.
from <module> import *
from tryme3 import *
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.
metulburr@ubuntu:~$ mkdir temp
metulburr@ubuntu:~$ cd temp
metulburr@ubuntu:~/temp$ sudo vim test_module.py
[sudo] password for metulburr:
metulburr@ubuntu:~/temp$ cat test_module.py
return x + y
metulburr@ubuntu:~/temp$ python test_module.py
metulburr@ubuntu:~/temp$ sudo vim test.py
metulburr@ubuntu:~/temp$ cat test.py
total = test_module.adder(1,3)
metulburr@ubuntu:~/temp$ python test.py
test_module.py test_module.pyc test.py
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
['test.py', 'test_module.pyc', 'test_module.py']
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)
and neither the python shell nor the built in terminal displays what I quoted above.
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?
bash-3.2$ ls tryme3.py
from tryme3 import *
bash-3.2$ mkdir thinkpython
bash-3.2$ cd thinkpython/
bash-3.2$ mv ~/Desktop/*.py ./
Users browsing this forum: No registered users and 2 guests