• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

78
Views
Import packages from outside django project

Context: The package being considered is an application that was developed by another team and I want to use the functionality exposed as part of an API call in my django project.

Directory layout:

<repo>
├── org_wide_django_code
│   ├── my_django_project
│   │   ├── my_django_project
│   │   ├── manage.py
│   │   ├── requirements.txt
│   │   └── my_application
├── frontend
│   └── application
├── other_team_work
│   ├── README.md
│   ├── __init__.py
│   ├── implemented_logic

What is the best way for me to use other_team_work in my django project my_django_project?

Prior reading:

  • Manipulating PYTHONPATH or sys.path is an option
  • Setting up a .whl or .egg to install other_team_work (also need to add a setup.py there)
3 months ago ·

Santiago Trujillo

2 answers
Answer question

0

I am not sure there is a best way since this depends a lot on the internal tooling of your organisation. However the main thing to pay attention to IMO are release and development cycles. Ideally you want both teams to be able to release new features, functionalities without impacting the other. (for instance if other_team makes a change to their code for a third team you would like this to have no possible impact on your application. Similarly other_team should not need to know how and when you use their module to make changes to their application.

One way to do this would be to have other_team_work be packaged and installable (using a setup.py) and then pushed to a package registery (for instance gitlab's package registery or github packages). This way you can use other_team_work as though it were just another python package with pip (using the extra-package-url argument) and using specific versions in a requirements.txt.

3 months ago · Santiago Trujillo Report

0

The simplest way (but can be dangerous) is add other_team_work to your project in PYTHONPATH. You can add this to django settings.py file, after BASE_DIR definition. Example:

import sys

BASE_DIR = Path(__file__).resolve().parent.parent  # default Django base dir
OTHER_TEAM_WORK_PARENT_DIR = BASE_DIR.parent  # in accordance with your directories
sys.path.append(str(OTHER_TEAM_WORK_PARENT_DIR))

This code add your directory org_wide_django_code to PYTHONPATH and you can import other_team_work in your code:

from other_team_work import implemented_logic
3 months ago · Santiago Trujillo Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.