0

I have a flask app with the following structure (using a shared virtual environment):

|-Directory
|----venv/lib/python2.7/site-packages/example-package
|----------module.py
|----app
|----------static
|----------templates
|----------models.py
|--------------...

I need to access some classes in my app/models.py module within the virtual environment directory, for example as per the above, I need to do the following in venv/lib/python2.7/site-packages/example-package/module.py

from models import TestClass

However, I keep getting a 'ImportError: No module named models' error, even when I try setting the path to the models.py module.

I've tried:

path = 'absolute path to models'
import os
os.chdir(path)

(I also get an error here about 'config not found)

and,

import sys
sys.path.append('absolute path to models')

with similar results

The app works fine otherwise, but I need to import the respective module.

horcle_buzz
  • 2,101
  • 3
  • 30
  • 59
  • *"I need to access some classes in my `app/models.py` module within the virtual environment "* - why? I generally wouldn't even put the virtualenv in the same directory as the project. – jonrsharpe Jun 28 '16 at 16:05
  • Not helpful. For all practical purposes assume venv is not in the project folder and that it is shared across projects. – horcle_buzz Jun 28 '16 at 16:09
  • 1
    But, again, *why?* What is the problem you're trying to solve? If you're sharing a virtual environment, why bother using them at all? – jonrsharpe Jun 28 '16 at 16:16
  • I have to hack into a library in my virtual env to override a method. If there is a way to easily override it outside the virtual env, then I would be hip to that. – horcle_buzz Jun 28 '16 at 16:18
  • 2
    Could we go down another layer of *"why"*? Why not just monkeypatch it? Or fork and patch the library and install the fork? What needs overriding, and for what reasons? Some context would be really useful here. – jonrsharpe Jun 28 '16 at 16:19
  • I am now trying a monkeypatch. – horcle_buzz Jun 28 '16 at 16:34
  • Monkeypatching wworked. Thanks for the suggestion – horcle_buzz Jun 28 '16 at 16:46
  • This is not an 'xy problem.' I was just using a bad approach to override an installed package's method. All is well. – horcle_buzz Jun 28 '16 at 19:49
  • *"This is not an 'xy problem.' I was just using a bad approach"* - that's pretty much what XY problem means! Glad you sorted it, though; please ether write up an answer explaining how you did so or delete the question if you don't think others will find it useful. – jonrsharpe Jun 29 '16 at 06:11
  • Done... (I think my initial misunderstanding and the process driven towards a rational solution is worth maintaining this question) – horcle_buzz Jun 29 '16 at 15:05

2 Answers2

0

The answer to my question was simple (and I was steered in the right direction by @jonrsharpe). As per monkey-patch-python-class I added the desired class to the root of the library with the module that needed the override and used this syntax to get the desired result maintaining the original and providing the alternate method as needed

import . module # added my class to a module located at library's root

class ReplaceClass(object): ... # class to override

module.TestClass = ReplaceClass 

voila...

Community
  • 1
  • 1
horcle_buzz
  • 2,101
  • 3
  • 30
  • 59
0

I faced similar kind of issue on my raspberry environment

In my case I found flask in the following dist_packages folder. The web server flask would run in normal environment but not in virtual environment

/usr/lib/python3/dist-packages

After copying these folders (flask, jinga2, werkzeug, markupsafe,itsdangerous,click and corressponding .egg-info folders) into site-packages(subfolder within virtual environment), it started working

/home/pi/.virtualenvs/cv/lib/python3.5/site-packages

these folders may be hidden. hence while searching, make sure you specify to search for hidden files or folders as well.

I am new to these environments and hence not aware of a cleaner solution. Ideally there should be a way to specify a list of folders to search in for libraries, in case its missing in a specific environment.