0

My directory structure looks like this:

directory_structure

I have some utility functions in util/misc.py that I want to import in compose_dataset.py. However, I cannot get the import statement to work.

I'm working on Windows Python3.5.4, so from what I've read I don't need __init__.py files anymore. The project folder is a child of my PYTHONPATH that points solely to E:\Python. So far, I tried:

from misc import *
from util import *
from util.misc import *
from ..util.misc import *

and either received ImportError: No module named 'xyz' or ImportError: attempted relative import with no known parent package. I also tried adding init-files but I have no experience with those and simply added one to every directory, but (surprisingly) that didn't work either.

What am I missing here??

bhnn
  • 225
  • 5
  • 17
  • You need the `src` folder to be on your `PYTHONPATH` in in your working directory, otherwise `from src.util.misc import *` should work. – Peter Wood Jan 22 '19 at 18:28
  • Did you try the `__init__.py` in the `util` folder anyways ? – Hans Daigle Jan 22 '19 at 18:29
  • `PYTHONPATH` is now "E:\Python, "E:\Python\Project\src"" and I still get `No module named 'src'`. Also, is adding every single project folder's src folder to the path variable really a good long-term solution? – bhnn Jan 22 '19 at 18:40
  • @bhnn if you add `src` to `PYTHONPATH` you then don't need to include it in the import: `from util.misc import *` – Peter Wood Jan 22 '19 at 19:56
  • @PeterWood I tried that but it didn't work. If I just add `src` to `PYTHONPATH`, my `sys.path` variable later contains `/src` and `/src/data` during runtime, but not `/src/util`. That has been causing the problems. If I add it manually like I commented below, it works fine, but the linter still shows an error because the correction obviously only happens at runtime. Do you know what would cause this weird behaviour? – bhnn Jan 22 '19 at 20:03
  • `sys.path` shouldn't contain `/src/util` it should contain `/src` then you say `from util import misc` or similar. – Peter Wood Jan 23 '19 at 16:25
  • But when `sys.path` only contains `/src` the import statement does not work, only when `/src/util` is added manually to the path. What you're saying is what most sources state as the correct way, so there has to be something wrong with the `sys.path` that makes it malfunction right? – bhnn Jan 24 '19 at 17:07

2 Answers2

1

Try this:

import sys
sys.path.insert(0, '../')
from util.misc import *

You may also want to take a look at this post: How to access a module from outside your file folder in Python?

Aida
  • 2,174
  • 2
  • 16
  • 33
  • `PYTHONPATH`now contains the `'../',` path, but the import statement still does not recognise `util` – bhnn Jan 22 '19 at 18:59
  • Did you add `__init__.py` to your sub folders? This way Python treats your folders as modules. An empty `__init__.py` would do. – Aida Jan 22 '19 at 19:17
  • Yep, there's an init file in the root directory and every sub-directory right now. Still the same ImportError. – bhnn Jan 22 '19 at 19:27
  • Could you try `sys.path.append` instead of `insert` just like the link I provided up there in my answer? – Aida Jan 22 '19 at 19:41
  • 1
    Hm, looks like for some reason `/src` and `/src/data` were automatically included into the `PYTHONPATH` just from putting the absolute path to `/src` into it. It was missing a link to `/src/util` though. Using `sys.path.append(os.path.abspath(os.path.join('./src', 'util')))` fixed it. Weird though, that it would automatically recognise the other subfolder but not this one... – bhnn Jan 22 '19 at 19:57
0

Set your PYTHONPATH to ., and add __init__.py files into each directory, where you want to import from, in your case add it into src, data, util directories. Assuming that you run your "entry point" script from the root directory of your project (same where your README.md file is), to import use this code:

# compose_dataset.py file
from src.util.misc import function_name
vagifm
  • 397
  • 2
  • 10