0

I have been working with Learn Python The Hard Way and I'm stuck at example 48. In example 47 I had to create directories that look like this:

skeleton
|--ex47
   |--module.py
   |--__init__.py
|--tests
   |--ex47_tests.py
   |--__init__.py

From now on I had to import ex47/module.py into tests/ex47_tests.py. I received the 'No module named ex47' error. The solution for this problem was to add path of ex47 directory to site-packages by adding two lines of code into module.py:

import sys
sys.path.append('./ex47')

And this worked fine. I could import module.py to ex47_tests.py and I can import it anywhere on my computer.

After moving to example 48 I created exactly the same directories, files, I added path to ex48/ and I keep receiving the 'No module named 48'. I searched internet for different solutions, none of them is working. Adding __init__.py into skeleton doesn't help.

This issue is super basic matter, however it is not introduced to new python programmers. By the way, I want a solution that would work on any computer that would work with my code.

Do such issues occur in Linux?

1 Answer 1

3

All you need to see is where from, you are invoking the python program. I have the following files.

C:\Users\kumarvivek\Desktop>tree /f skeleton
Folder PATH listing for volume ????
Volume serial number is 6AE1-4919
C:\USERS\KUMARVIVEK\DESKTOP\SKELETON
│   __init__.py
│
├───ex47
│       mod.py
│       mod.pyc
│       __init__.py
│       __init__.pyc
│
└───tests
        ex47_tests.py
        __init__.py


C:\Users\kumarvivek\Desktop>

With the following contents:

C:\Users\kumarvivek\Desktop>type skeleton\ex47\mod.py
import os
x = "C:\\Users\\kumarvivek\\Desktop\\skeleton\\ex47\\module.py"
directoryPath= os.path.dirname(x)
fileName = os.path.basename(x)
print "\nFilePath:      %s\nDirectoryPath: %s\nFileName:      %s\n" %(x, directo
ryPath, fileName)
C:\Users\kumarvivek\Desktop>

And

import sys

# If the Current Working directory is skeleton
# C:\Users\kumarvivek\Desktop\skeleton>python C:\Users\kumarvivek\Desktop\skeleton\tests\ex47_tests.py
#
# sys.path.append(r"..\skeleton")

# If the Current Working directory is any of these "tests" or "ex47"
# C:\Users\kumarvivek\Desktop\skeleton\tests>python C:\Users\kumarvivek\Desktop\skeleton\tests\ex47_tests.py
# C:\Users\kumarvivek\Desktop\skeleton\ex47>
#
# sys.path.append(r"..\..\skeleton")

sys.path.append(r"..\..\skeleton")


from ex47 import mod

print mod.x , mod.directoryPath, mod.fileName
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.