Importing modules using relative path

Dec 15, 2013 at 11:41 PM
Hi! I'm new to PTVS. I just noticed that the intellisense doesn't work unless I type the project name first followed by the folder name, then followed by the python file.

I have a project with several folders. Inside the folders are python files. The structure is like this:
projName
   folder1
      module1.py
      module2.py
   folder2
      module3.py
Inside module1, I need to import module3. When I type
from projName.folder2.module3 import something
it works. But it doesn't work when I start with relative path like this
from folder2.module3 import something
What should I do in order for me to import modules without specifying the project name first? so that non-PTVS users can also use my code (because it only uses relative paths).
Coordinator
Dec 16, 2013 at 4:39 AM
Do you have any __init__.py files? Your behaviour makes sense if you have three of them (one in projName, folder1 and folder2) but you probably don't want one in projName.

This layout should behave as you expect:
projName\
   (no files)
   folder1\
      __init__.py
      module1.py
      module2.py
   folder2\
      __init__.py
      module3.py
Also, do those imports work when you run the code in Python? If so, have you set any search paths or PYTHONPATH values? Our aim is to match what Python does, and with default settings we should be correct for this case. However, we don't track all the possible ways that exist to add extra search paths to Python (and some are simply impossible), so it may be that your code works because of one of these.
Dec 16, 2013 at 9:20 PM
Hi Zooba, thank you very much!

You are right, the intellisense definitely works after I deleted the __init__.py file inside projName.

To answer your question, yes the code runs ok when I use from folder2.module3 import something. I did specify "." in the search paths which I assume is the solution directory. I have also set some values in PYTHONPATH, i.e. the directory of my python installation and library packages.