Reputation: 1383
Newbie question (I'm just getting started with Python and Pydev):
I've created a project "Playground" with (standard?) src/root sub-folder. In there I've created example.py.
How do I import my "example" module into Pydev's interactive console? ">>> import example" gives: "ImportError: No module named example"
Upvotes: 8
Views: 4977
Reputation: 36
Following the PYTHONPATH advice above, I used a bit of a hack to get this working. If I understand your question, you want to have the current working directory in the IPython environment set to the directory in which your active file resides. So if you are editing D:/projects/file.py, you want the pwd() command (in IPython) to return D:/projects. This is where the hacked together part of my solution comes from. All my projects are on my D drive, but all the normal python imports come from the install location on my C drive. So the following:
os.environ['PYTHONPATH'].split(os.pathsep)
results in a list on which only one path is on the D drive, and that path (from the answers above) is of my active file's directory. If you don't use the D drive, then there should be some other unique way of identifying which of the paths in that list pertains to your projects. If there isn't a way of uniquely identifying your project path, then this answer doesn't work. But in the simple case of "D:/" being enough of a unique identifier, this is my startup code in the settings (Window > Preferences > PyDev > Interactive Console)
import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
import os;os.chdir([p for p in os.environ['PYTHONPATH'].split(os.pathsep) if p.startswith("D")][0])
pwd()
Upvotes: 0
Reputation: 1383
I found the answer on the Plone website: Setting up PYTHONPATH in Eclipse project. This provides a convenient way to set PYTHONPATH on a per project basis. In my case I added "/Playground/src/root" to the Source Folders list. ">>> import example" then worked. (I'm still surprised project files aren't imported by default, though.)
Thank you jldupont for pointing me in the right direction (re. PYTHONPATH)!
Upvotes: 5
Reputation: 96836
You need to set your PYTHONPATH accordingly (Google search is your friend) or use *.pth in your installation site-packages directory pointing to your project path. Don't forget to set your interpreter details with Pydev (Window->Preferences->Pydev->interpreter).
Upvotes: 5