0

yes I know my question sounds like a duplicate but I have tried everything I have found.

I am trying to add a module to my python flask app. This module is located at ../../clients/api relative to my flask app. I haven't had any problems adding a module before, but for some reason this just doesn't want to work.

So far, I have tried:

  1. sys.path.append("mypath") (worked temporarily but as soon as the shell was closed it disappeared)
  2. adding a .pth file in lib/site-packages
  3. adding it directly to my environment variables
  4. adding in in my IDE (Wing 101)

Aside from 1., none of them had any effect whatsoever.

$ flask run
 * Serving Flask app "main.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: While importing "main", an ImportError was raised:

Traceback (most recent call last):
  File "c:\python37-32\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
  File "my_flask_app_path", line 20, in <module>
from clients.api import Client as client
ModuleNotFoundError: No module named 'api'

Running this on python v-3.7.1, Windows 10, both git bash and cmd. Please try not to be blunt :)

eles
  • 31
  • 7
  • 2
    Can you add the file structure of your project? Did you create a `__init__.py` in `../../clients/api`? – caverac Dec 17 '18 at 10:18
  • Can you quote your `import` from the code here? –  Dec 17 '18 at 12:15
  • @caverac yes I did. File structure is `pipeline/clients/api` and `pipeline/server/main.py` – eles Dec 18 '18 at 01:38
  • @reportgunner currently it is `from pipeline.clients.api import Client as client`, but I have also tried the `..` notation – eles Dec 18 '18 at 01:40
  • I'm afraid what you are trying to do is not possible - importing modules from a "neighboring" or "sibling" directory. see [this question and answer](https://stackoverflow.com/questions/32927127/how-to-correct-use-import-when-importing-neighbor-module) You will have to move your `main.py` one directory higher, i.e. to `pipeline` –  Dec 18 '18 at 11:50
  • @reportgunner really? Because this is company-wide code, and everyone else's code appears to work. Mine is the only Windows computer here so I assume it is an enviromental issue. – eles Dec 19 '18 at 02:03
  • sorry, not "impossible". you can apparently do it by messing with `sys.path`, see [this answer](https://stackoverflow.com/a/40612922/10417531) –  Dec 19 '18 at 12:34
  • @reportgunner which I have already tried – eles Dec 20 '18 at 02:25

0 Answers0