Shared library / django classes

I am new to django and have been looking for advice on where to put my shared library. I plan to create classes that I want to use in all of my applications within the project. Where would be the best place to stay?

ex abstract patterns

Yours faithfully,

+7
source share
4 answers

We usually set up our projects as follows:

/site/ __init__.py manage.py settings.py urls.py /apps/ __init__.py /appA/ __init__.py /appB/ __init__.py /lib/ __init__.py /django-lib/ __init__.py /shared-lib/ __init__.py 

Just make sure your site directory is in your python path:

 import sys sys.path.append('/path/to/site/') 

Also, make sure init.py exists on the site, in applications, and in lib, so you can think of them as modules using dot notation import (import site.lib.shared-lib)

Edit:

In answer to your question regarding your python path, all this is related to where your manage.py file or equivalent is located. If it is in the / site / directory (next to applications and lib), then PYTHONPATH should be fine.

You need to make sure that each directory contains an empty file named __init__.py . This tells Python to treat this directory as a module. See New and Improved ASCII Art above.

+10
source

What I learned from Two Scoops of Django is that you put common code inside the Django application that you create yourself, core .

To use the core application, it is similar to how you cross-use classes from other applications.

To learn more, go to chapter 28 of a book entitled "What are these random utilities?"

+3
source

Anywhere, you can import them if they are in PYTHON_PATH.

0
source

If the shared library will be used in other projects, you can make the installer using distutils . But if it is only for applications in the project, you can take the answer AntonioP.

Remember that the root of your project (the folder containing manage.py) is always in your PYTHON_PATH when you start the django project, so you can create a deps or dependencies or extra folder or whatever it contains your shared libraries.

0
source

All Articles