5

I have two Python packages where one needs to be imported by the other. The directory structure is like follows:

workspace/
  management/
    __init__.py
    handle_management.py
    other_management.py
  utils/
    __init__.py
    utils_dict.py

I'm trying to import functionality from the utils project in the handle_management.py file:

import utils.utils_dict

Error I'm getting when trying to run handle_management.py:

ImportError: No module named utils.utils_dict

I've read a lot about how to resolve this problem and I can't seem to find a solution that works.

I started with Import a module from a relative path - I tried the applicable solutions but none worked.

Is the only solution to make workspace/ available via site_packages? If so, what is the best way to do this?

EDIT:

I've tried to add the /home/rico/workspace/ to the PYTHONPATH - no luck.

EDIT 2:

I was able to successfully use klobucar's solution but I don't think it will work as a general solution since this utility is going to be used by several other developers. I know I can use some Python generalizations to determine the relative path for each user. I just feel like there is a more elegant solution.

Ultimately this script will run via cron to execute unit testing on several Python projects. This is also going to be available to each developer to ensure integration testing during their development.

I need to be able to make this more general.

EDIT 3:

I'm sorry, but I don't really like any of these solutions for what I'm trying to accomplish. I appreciate the input - and I'm using them as a temporary fix. As a complete fix I'm going to look into adding a script available in the site_packages directory that will add to the PYTHONPATH. This is something that is needed across several machines and several developers.

Once I build my complete solution I'll post back here with what I did and mark it as a solution.

EDIT 4:

I feel as though I didn't do a good job expressing my needs with this question. The answers below addressed this question well - but not my actual needs. As a result I have restructured my question in another post. I considered editing this one, but then the answers below (which would be very helpful for others) wouldn't be meaningful to the change and would seem out of place and irrelevant.

For the revised version of this question please see Unable to import Python package universally

Community
  • 1
  • 1
Rico
  • 5,692
  • 8
  • 46
  • 63
  • You cant just add that relative path to the PYTHONPATH. You need to add the full path. – jdi May 14 '12 at 16:57

3 Answers3

3

You have 2 solutions:

Either put workspace in your PYTHONPATH:

import sys
sys.path.append('/path/to/workspace')

from utils import utils_dict

(Note that if you're running a script inside workspace, that is importing handle_management, most probably workspace is already in your PYTHONPATH, and you wouldn't need to do that, but it seems it's not the case HERE).

Or, make "workspace" a package by adding an empty (or not) __init__.py file in the workspace directory. Then:

from ..utils import utils_dict

I would prefer the second, because you would have a problem if there's another module called "utils" in you PYTHONPATH

Apart from that, you are importing wrong here: import utils.utils_dict.py. You don't need to include the extension of the file ".py". You are not importing the file, you are importing the module (or package if it's a folder), so you don't want the path to that file, you need its name.

jadkik94
  • 7,000
  • 2
  • 30
  • 39
  • Sorry about the import statement...that was a typo (fixed). I was not importing the file. – Rico May 14 '12 at 17:04
0

Put "workspace/" in your PYTHONPATH to make the packages underneath available to you when it searches.

This can be done from your shell profile (if you are on *nix, or environment variables on windows.

For instance, on OSX you might add to your ~/.profile (if workspace is in your home directory):

export PYTHONPATH=$HOME/workspace:$PYTHONPATH

Another option is to use virtualenv to make your project area its own contained environment.

jdi
  • 90,542
  • 19
  • 167
  • 203
  • I just tried this and it didn't do the job. I'm using Ubuntu 12.04. `echo $PYTHONPATH` gives `:/home/rico/workspace:` Is this not correct? – Rico May 14 '12 at 16:47
  • Yes, my home directory (not the home directory). That is `/home/rico/workspace` NOT `/home/workspace` – Rico May 14 '12 at 16:59
  • @Rico: Then it should work by adding that path to your PYTHONPATH – jdi May 14 '12 at 17:02
0

What you need to do is add workspace to your import path. I would make a wrapper that does this for you in workspace or just put workspace in you PYTHONPATH as an environment variable.

import sys
# Add the workspace folder path to the sys.path list
sys.path.append('/path/to/workspace/')

from workspace.utils import utils_dict
klobucar
  • 6,307
  • 1
  • 13
  • 15
  • This did work, but I may have trouble using this as a complete fix for my problem. I'll explain in my second edit. Thank you. – Rico May 14 '12 at 16:56
  • This requires explicit path references between the packages, so its not ideal. It would be better for them to assume they are in the same package, and rely on the PYTHONPATH – jdi May 14 '12 at 16:57