0

I get that error message when I run python testscript.py I tried the recommendations given in link but was unsuccessful. My directory structure is as follows:

> bin
> - __init__.py
> - testscript.py
> 
> test2
> - __init__.py
> - printstring_module.py

And my code in testscript.py is as follows:

import sys
sys.path.append('C:\Users\KMF\Exercises\Projects\kevin_game\test2')
from test2 import printstring_module
printstring_module.printstrings("this is a test")
print("test over")

And the code for printstring_module.py is as follows:

def printstrings(string="you didn't provide a string"):
 print(string)

When I place both testscript.py and printstring_module.py in the same directory eg. bin folder the code works just fine.

Thanks and happy new year!

asked Dec 31, 2015 at 18:12

1 Answer 1

1

You are included the folder test2 in your path, so you should use something like this:

from printstring_module import printstrings

And then use printstrings function directly:

printstrings("this is a test")

Also rename init.py to __init__.py

EDIT:
You should also scape \ character. Try this code:

sys.path.append(r'C:\Users\KMF\Exercises\Projects\kevin_game\test2')

Note r i included before the opening quote.

answered Dec 31, 2015 at 18:19
Sign up to request clarification or add additional context in comments.

6 Comments

I just tried this and resulted in this error message ImportError: No module named printstring_module
Also you should have an empty __init__.py in your folder. I edited my post, check it @ferry
Hey my __init__.py are fine. Just that when I marked them as codes, SO removed the underscores. Code still doesn't work.
Put a r before the opening single quote to scape the backslash character. @ferry
Wow, that worked. Could you please explain to me what the r did?
|

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.