Use a django project with virtualenv from Scrapy


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # This snippet is useful when you want to use a django project which runs in a Python virtual environment.
  2.  
  3. """
  4. This code should be executed before the imports you need from your django project
  5. For example, you can place it at the end of your settings.py
  6. """
  7.  
  8. import sys, os
  9.  
  10. # Add the directory containing your django project 'myproj' to the path
  11. sys.path.insert(0, '/django-projs/')
  12.  
  13. # Set the django settings environment variable
  14. os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'
  15.  
  16. # Activate the virtual environment used in the django project
  17. execfile('/path/to/virtual/env/bin/activate_this.py',
  18. {'__file__': '/path/to/virtual/env/bin/activate_this.py'})
  19.  
  20. # Now you can import any model, for example
  21. from myproj.myapp.models import MyModel
  22.  
  23. # Snippet imported from snippets.scrapy.org (which no longer works)
  24. # author: anibal
  25. # date : Aug 16, 2010
  26.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.