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?
-
Don't use periods in package directory names.Tim Pietzcker– Tim Pietzcker2014年07月09日 17:31:55 +00:00Commented Jul 9, 2014 at 17:31
-
@TimPietzcker: Sorry.. that was a typo. Thanksfrazman– frazman2014年07月09日 17:34:23 +00:00Commented Jul 9, 2014 at 17:34
1 Answer 1
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.