1

So, here's my (okay.. messy) dir:

.
├── app
│   ├── __init__.py
│   ├── analyze_text.py
│   ├── images.py
│   ├── main.py
│   ├── messages.py
│   ├── process_text.py
│   ├── requirements.txt
│   ├── response.py
│   └── tests
│       ├── __init__.py
│       ├── analyze_text_test.py
│       ├── test_process_text.py
│       └── unit_tests.py
└── setup.py # no idea what's going on with this

All I want to do is, simply use

from analyze_text import AnalyzeText

in the analyze_text_test.py file without seeing

"You're an idiot and you don't know what you're doing" in the terminal.. a.k.a:

ImportError: No module named (whatever)

I found this solution:

https://stackoverflow.com/a/11158224/2738183

import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir) 

import mymodule

Which works, but it's Janky.

Why?

Because..

I'm using it in a Janky way. I don't wanna have to re-paste this code to every single one of my unittests in the tests folder (which are in different files.)

(So what if i just paste it once in init.py? You get an error that's what. But I did randomly try that just to see what happened)

So what is the most elegant way to approach this problem? ( without repasting code (or just sticking it in a function and calling it multiple times) )

Edit: The comments so far haven't solved anything, so I'll try to make this a bit more clear. I found a solution that works. So in each file in the tests directory I have to re-paste that solution (or call the same function as many times as there are files.) That's exactly what I'm trying to avoid. I'd like a solution that can apply to every file in the test directory so that I can use imports from the parent directory like normal, instead of appending the parent path inside every single one of those files.

  • Does `from ..analyze_text import AnalyzeText` works? – Moinuddin Quadri Sep 26 '17 at 16:13
  • I'll edit to include that info. I thought I stated that. –  Sep 26 '17 at 16:14
  • What error do you get if you put the `sys.path.insert` lines into your __init__.py? should work as far as i know – Rafael T Sep 26 '17 at 16:14
  • Yes, actually, I did state which file. After using the solution in the link, yes, that works, prior, no, I get an import error. (Sorry if that wasn't clear) –  Sep 26 '17 at 16:15
  • @JillRussek I see now, since you're a directory down you'll need to use the full path eg `from app.analyze_text import AnalyzeText` – MrAlexBailey Sep 26 '17 at 16:16
  • @Jkdc, no that's not the issue, I already tried that before the solution and it didn't work, now I have a solution that works, I just don't want to repaste it five times –  Sep 26 '17 at 16:18
  • Have you considered using the `imp` module for your imports? something along `mymodule = imp.load_module("app.analyze_text", None, "../", None)` ? – Rafael T Sep 26 '17 at 16:19
  • @Rafael T , as I stated previously, "(So what if i just paste it once in init.py? You get an error that's what. But I did randomly try that just to see what happened)" –  Sep 26 '17 at 16:20
  • @RafaelT does it make a difference that I'm using python 2.7? –  Sep 26 '17 at 16:21
  • Nah this is a problem as well in py 3.x – Rafael T Sep 26 '17 at 16:21
  • @RafaelT I just mean I've never seen load_module in python 2.7, so wondering if that was a 3.5 thing –  Sep 26 '17 at 16:23
  • huh? The documentation for imp module does list the `load_module` function https://docs.python.org/2/library/imp.html – Rafael T Sep 26 '17 at 16:24
  • 1
    @RafaelT Pfft. ... like I read documentation ;) lol. jk –  Sep 26 '17 at 16:25
  • RTFM!!! https://xkcd.com/293/ or http://dilbert.com/strip/2009-03-15 – Rafael T Sep 26 '17 at 16:28
  • @Rafael T both .. also I was joking. I did read the manual. I started reading about setup.py and all that, which wasn't working for me either when I applied it. If everyone was required to read all documentation before code was ever written, no code would get written, we'd forget what we learned and we'd have to look it up again ;) –  Sep 26 '17 at 16:40
  • @RafaelT p.s. imp.load_module doesn't even solve my issue, because I need a generic solution that applies to all of my files in the test directory, not just one. I don't want to copy and paste or call a function a bunch of times.. there's some way to do it, and that's what I'm looking for –  Sep 26 '17 at 16:43

1 Answers1

0

Never mind. I found an elegant solution on stack overflow that I like.

Which amounted to placing

import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir) 

inside a file within the tests directory named env.py

and simply adding

import env

before importing as usual

see: https://stackoverflow.com/a/23386287/2738183