0

My current directory structure looks like the following:

 MainProject-- src---python---python_client/my_module/__init__.py
 L___/foo.py
 L_/utils.py
 |
 L_/tests/__init__.py
 L_ test_foo.py

Now in test_foo.py, I want to import my_module
Now I can make changes inside python_client dir but not on directories above that..

THe issue I am facing is import my_module is not recognized/valid.. What should I add/ modify in order to make imports like that? Thanks

Edit: I have a question.. Why does something like this works https://github.com/tweepy/tweepy/blob/master/tweepy/api.py They are importing functions from different files... But if in foo.py , I do

import my_module.utils I get an error?
asked Jul 9, 2014 at 17:26
2
  • Don't use periods in package directory names. Commented Jul 9, 2014 at 17:31
  • @TimPietzcker: Sorry.. that was a typo. Thanks Commented Jul 9, 2014 at 17:34

1 Answer 1

2

Set the PYTHONPATH environment variable.

$ export PYTHONPATH=/MainProject/src/python/python_client

You should then be able to import my_module with:

import my_module

There seems to be a confusion about a module and a package. my_module above is actually a package. foo and utils are modules inside the package my_module.

It is always a good idea to set the PYTHONPATH to the root of your project and check to make sure you are able to import your packages or modules by running the python interpreter.

More on the Python tutorial.

answered Jul 9, 2014 at 17:58
Sign up to request clarification or add additional context in comments.

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.