Force use of inconsistent file import paths in Python (/ Django)

I recently had problems with my import in Django (Python) ... It’s better to explain the use of file diagrams:

- project/ - application/ - file.py - application2/ - file2.py 

In project/application/file.py , I have the following:

 def test_method(): return "Working" 

The problem occurs in project/application2/file2.py when I try to import a method from above:

 from application.file import test_method 

Usually works, but sometimes not.

 from project.application.file import test_method 

It works, but it contradicts the recommendations for Django portability, since the project folder should always be the same.

I would not mind, but it is a fact that this question is inconsistent, most of the time when it misses the project , it’s fine, but sometimes not (and, as far as I can see, for no reason).

I can pretty much guarantee that I'm doing something stupid, but has anyone experienced this? Would it be better if I put project in front of all the relevant imports so that everything is in order? Honestly, it is unlikely that the name of the project folder will ever change, I just want everything to be as recommendable as possible.

+4
source share
3 answers

To import, to find a module, it must either be in sys.path. This usually includes "so it searches for the current directory. If you download the" application "from the project, it will find it, since it is in the current directory.

Well, this is obvious stuff. The confusing bit is that Python remembers which modules are loaded. If you download the application, then you download the application2, which imports the application, the "application" module is already loaded. It does not need to be searched on disk; it just uses the one that is already loaded. On the other hand, if you have not downloaded the application yet, it will search for it - and will not find it, because it is not in the same directory as the download of it ("."), Or anywhere else in the path.

This can lead to a strange case where imports sometimes work and sometimes not; it only works if it is already loaded.

If you want to be able to load these modules as an “application”, you need to organize a project /, which will be added to sys.path.

(Relative imports affect the sound, but it looks like application and application2 are separate packages - relative imports are used to import within the same package.)

Finally, be sure to sequentially process all of this as a package or sequentially process each application as its own package. Do not mix or mix. If the package / is in the path (for example, sys.path includes the package / ..), then you can really do "from package.application import foo", but if you also do "from the application import foo", this is possible for Python does not understand that this is one and the same thing - their names are different, and they are in different ways - and as a result, two different copies are downloaded that you definitely do not need.

+4
source

If you delve into the django philosophy, you will find that the project is a collection of applications. Some of these applications may depend on other applications, and this is normal. However, you always want your applications to connect so you can move them to another project and use them there. To do this, you need to remove all the things in the code associated with your project, so when you import, you will do it.

 from aplication.file import test_method 

This will be django's way of doing this. Glenn replied why you get your mistakes, so I won’t go into this part. When you run the command to start a new project:

  django-admin.py startproject myproject 
This will create a folder with a bunch of files django needs, manage.py, py ect options, but it will do one more thing for you. It will put the "myproject" folder in your python path. In short, this means that any application that you put in this folder, you can import, as shown above. You do not need to use django-admin.py to start the project, since nothing magical happens, it's just a shortcut. Thus, you can place the application folders anywhere, you just need to have them in the python path, so you can directly import them and make your project independent of the code so that it can easily be used in any future project, observing the DRY principle that is built django.
+2
source

It is best to always import using the same path - say, using project.app.models - because otherwise you may find that your module is imported twice, which sometimes leads to dark errors, as described in this question .

0
source

All Articles