1

I am a python newbie and am using the book Learn Python the Hard Way. Exercise 25 from the book asks us to import the python module into the python interpreter.

I used "/usr/bin/python" to invoke the interpreter:

S-MacBook-Air:~ s$ /usr/bin/python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

But i get an error when importing the file:

>import ex26_test
>>Traceback (most recent call last):

  >>File "< stdin >", line 1, in < module >

>ImportError: No module named ex26_test

the file sits on the following folder (~/documents/1Webdev/LPTHW/ex26_test.py)

How can i import the module "ex26_test" into the interpreter please?

thank you in advance! SD

Copperfield
  • 8,131
  • 3
  • 23
  • 29
Sahil
  • 11
  • 1
  • 2

4 Answers4

0

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

1) The directory containing the input script (or the current directory when no file is specified).

2) PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH). 3) The installation-dependent default.

python module import documentation

so you can cd to ~/documents/1Webdev/LPTHW and run python from there.

ori
  • 369
  • 2
  • 6
  • 17
0

Added path to your module in sys.path

import sys
sys.path.append(r'~/documents/1Webdev/LPTHW')
import ex26_test
Skycc
  • 3,496
  • 1
  • 12
  • 18
0

you need to open python in the same folder where the file is located because python can't see folders that aren't in the sys.path. There is of course way to add that folder temporary or permanently to sys.path that I will show in a moment

solution 1: move to that folder, use de cd (change directory) command to do it, that should look something similar to this:

$ cd ~/documents/1Webdev/LPTHW
$ ~/documents/1Webdev/LPTHW: python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex26_test
>>>

note that if your python is correctly configured you should be able to call it be just typing python

solution 2: add that folder to sys.path to do it, open python and do the follow

>>> import sys
>>> sys.path.append("~/documents/1Webdev/LPTHW")
>>> import ex26_test
>>>

this is the temporary solution, but it will work regardless where you open python

solution 3: permanently add that folder to sys.path, to do this you need to modify/create the environ variable PYTHONPATH, I don't know how to do that in mac, but it should not be that difficult, in that regard some of those should be helpful: https://stackoverflow.com/search?q=pythonpath+on+mac in particular this looks like the correct one: How can I edit PYTHONPATH on a Mac?

Community
  • 1
  • 1
Copperfield
  • 8,131
  • 3
  • 23
  • 29
0

Ok, it should be easy, but previous answers don't give all the solution. You can see where python find its modules using the sys.path function, but before you can use it, you have to import the module sys :

>>> import sys
>>> print(sys.path)

Next you have to add the folder where your module is with the commande path.append

>>> sys.path.append("~/documents/1Webdev/LPTHW")

And to load your module :

>>> from ex26_test import *

Now you can access all functions defined in your ex26_test.py file.

JL M
  • 1,378
  • 1
  • 10
  • 14