1

I'm trying to append to my PYTHONPATH the location of two Django modules (version 1.4) to use their ORM models. Here is the code:

sys.path.append('/path/project1')
sys.path.append('/path/project2')

When I try to set up the Django Enviroment as follows:

import settings 
setup_environ(settings)

Only settings of project1 are loaded. I think the problem is that both models contains a file called settings.py respectively, therefore, names collide. I cannot modify modules.

How can I add both modules settings to the Django Enviroment?

floatingpurr
  • 7,749
  • 9
  • 46
  • 106

1 Answers1

2

Note:

I undeleted this answer for future reference. The solutions below allow importing two different modults from files with the same name, but don't allow running two Django projects in one interpreter (see this comment and this comment).

Solution #1

You could try turning projects into python packages.
1. Add an empty __init__.py to /path/project1/ and /path/project2/
2.

sys.path.append('/path/')
sys.path.append('/path/project1/')
sys.path.append('/path/project2/')

import project1.settings
import project2.settings 
setup_environ(project1.settings)
setup_environ(project2.settings)

Solution #2

1) Make a directory in your /path/ containing symbolic links to the setting files

cd /path/
mkdir setting_links
cd setting_links
ln ../project1/settings.py settings_1.py
ln ../project2/settings.py settings_2.py

2) Put both projects and the directory onto the sys.path

sys.path.append('/path/setting_links/')
sys.path.append('/path/project1/')
sys.path.append('/path/project2/')

import settings_1
import settings_2
setup_environ(settings_1)
setup_environ(settings_2)
  • It could work but I still have problems with relative imports inside projects modules. Let `baz` and `foo` be sub modules of `project1` e.g. `project1.foo.bar`. When I try something like `from project1.foo import bar`, I get an error since inside `bar` there are imports like `from foo import blahblah`. The error is `ImportError: No module named foo` – floatingpurr Sep 21 '16 at 15:54
  • @superciccio14 The first thing that comes to mind is using `from project1.foo import blahblah` instead of `from foo import blahblah`. Is it going to over complicate things? – Андрей Беньковский Sep 21 '16 at 16:00
  • That's right. But that statement is inside the module `project1.foo.bar` and I cannot modify `project1`'s code – floatingpurr Sep 21 '16 at 16:02
  • I do not understand if adding projects back to `sys.path` will do the trick. I have to test it better. – floatingpurr Sep 21 '16 at 16:21
  • 1
    I just tested. Both solutions seam to work on my computer in python 2 and 3. – Андрей Беньковский Sep 21 '16 at 16:32
  • @АндрейБеньковский Solution #1 seems to work but there is still a problem in `setup_environ()`. The `settings_2` is not loaded at all. In fact when I load models of **project 2**, I get this error: `django.db.utils.DatabaseError: (1146, "Table 'project1.table_name_of_project_2' doesn't exist")`. I think the problem is the double `setup_environ()` call – floatingpurr Sep 22 '16 at 11:29
  • Another consideration: probably Django do not understand that it has to use db1 for models of project1 (as confiugred in settings_1) and db2 for models of project2 (as confiugred in settings_2). But Django uses only db1 – floatingpurr Sep 22 '16 at 11:41
  • 1
    @superciccio14 Ok. I thought this issue can be solved with just my python knowledge, but I'm reading the Django code right know. BTW what version of Django are you using? – Андрей Беньковский Sep 22 '16 at 15:01
  • I'm using the 1.4. In alternative, i could use also 1.7 – floatingpurr Sep 22 '16 at 15:02
  • @superciccio14, as you probably already know 1.5 is the last version that has the `setup_environ()` function that was deprecated since 1.4, so switching to 1.7 is going to make the original question and my solutions irrelevant. – Андрей Беньковский Sep 22 '16 at 15:10
  • I know, so let's use 1.4 – floatingpurr Sep 22 '16 at 15:13
  • 1
    @superciccio14 Actually if the rest of your code is compatible with 1.7 switching is probably a better idea. Also I think now I figured out a solution that should be compatible with Django 1.4 - 1.10+. I'm going to install django and see if it actually works out. – Андрей Беньковский Sep 22 '16 at 15:22