4

I have a Django project for which I have created a virtual environment with the packages it requires.

Now for development, I was looking for a tool where I could try some code before including it to my project. I had a good experience with Jupyter a little while ago and I thought that would be nice to work with this tool again.

In order to avoid cluttering the minimal virtual environment with the dependencies of Jupyter, I duplicated it and installed jupyter alongside with django-extensions.

In my settings.py, I have:

if os.environ.get("VENV_NAME") == "jupyter-sandbox":
    INSTALLED_APPS += ['django_extensions']

so that I can still be able to use the minimal virtual environment without django_extensions.

It works fairly well for now apart from the fact that I cannot run the server from my Jupyter-enabled virtual environment. This is because my project uses django-images and django can't find a migration file in this environment (in sites-packages/django_images/migrations). The error message is below:

raise NodeNotFoundError(self.error_message, self.key, origin=self.origin)
django.db.migrations.exceptions.NodeNotFoundError: Migration core.0001_initial dependencies reference nonexistent parent node ('django_images', '0002_auto_20170710_2103')

Would it be a good idea to create a symlink so that both virtual environments share the same django-images migrations folder or would it mess up completely my project?

I am not utterly confident with migrations yet and would appreciate some advice on this.

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
  • so when you use `django_extensions` you need `django_images` too ? – alfonso.kim Jul 19 '17 at 23:43
  • Yes I use `django_images` in my models. – Jacques Gaudin Jul 19 '17 at 23:45
  • If you've _duplicated_ your original virtualenv and then installed jupyter and django-extensions, then it should still have django-images installed and not complain about those migrations missing. – A. J. Parr Jul 19 '17 at 23:46
  • I duplicated the venv with `pip install -r requirements.txt`. Now `django_images` is installed in both environments but there is a migration file missing in the second. – Jacques Gaudin Jul 19 '17 at 23:50
  • If you are worried about messing up virtualenvs, then you are doing something wrong. They should be disposable and easy to re-create at any point in time. – Grimmy Jul 19 '17 at 23:53

1 Answers1

3

I think the confusion here is what you should do. In any standard project, you should have a hand-cultivated list of project dependencies. Most django users put that list into requirements.txt (usually recommended) or setup.py. You can always have multiple requirements: requirements-test.txt, requirements-dev.txt, etc. Use -r requirements.txt at the top of the other files to "import" other requirements:

# requirements.txt
django=1.11.3

and then...

# requirements-test.txt
-r requirements.txt
pytest
tox

and finally...

# requirements-dev.txt
-r requirements-test.txt
ipython
pdbpp
jupyter

Your goal is to have whatever you need for your project to run within the first file. It doesn't matter if your virtual environment has more than enough. And additionally, you should use something like tox to test out if your requirements.txt actually contains exactly what it needs. Hope that helps.

Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55