2

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,6353 gold badges27 silver badges32 bronze badges
asked Oct 23, 2016 at 13:11
1
  • I dont see a problem with your syntax, just make sure your PYTHONPATH global variable have path for this module Commented Oct 23, 2016 at 13:46

4 Answers 4

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.

answered Oct 23, 2016 at 13:50
Sign up to request clarification or add additional context in comments.

Comments

0

Added path to your module in sys.path

import sys
sys.path.append(r'~/documents/1Webdev/LPTHW')
import ex26_test
answered Oct 23, 2016 at 14:25

Comments

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?

answered Oct 23, 2016 at 14:27

Comments

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.

answered Jul 1, 2021 at 9:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.