0

I have a bunch of files containing python functions, used in a web-serving apache2, they are all called from PHP as shell scripts just with "python3 'file' 'args'" (without the -m, that is) and return data to PHP via JSON strings, are all debugged since a long time, and work well.

They are all in a single directory, and most of them import other files simply using

 from file_with_lower_level_functions import gimme_this_table 

many imported functions in turn import other ones, and some import chains may be 4 or 5 links long.

Trying to import this into Django is nightmarish. All of my "froms" are replied to by "manage.py check" with "module not found". I did obtain partial success by changing the top level froms above to

 from .file_with_lower_level_function import gimme_this_table 

adding the leading dot. But first, changing the "from"s like that in every single module would make them unworkable in the old situation, and I do not want to do this change.

And, next, this gives problems with 2nd links, when one of my functions imports another one: applying the same cure to the 2nd one doesn't work: "Huh uh! module not found"

Is this kind of approach even possible with Django? Is there a common form which would give me a code usable in BOTH django and PHP?

I thought of playing to Django the same trick I did to PHP, that, is calling my code as a shell script, and that would probably work; but since Django is already python, that does not appeal much to me: I would sweat a job and learn nothing.

I tried calling from django views my old functions, and I was expecting that to work without (many) problems.

2
  • You should be able to set PYTHONPATH to somehow solve this. I'd suggest to instead make your code into a package and use the package in your Django application. Commented May 12, 2024 at 16:38
  • Well the functions already are in a package: there is an empty init.py files in the same directory, and that should be all that's needed (or sn't it?) That should leave me the task to export PYTHONPATH, will try that and post the progress, if any Commented May 13, 2024 at 9:09

1 Answer 1

0

Yes, the comment I received gave me the clue. I wish it was an answer, I would have upvoted it.

Setting up one's own python code to be called in Django's views was (in my case at least) simply a matter of:

  1. having my modules in one directory (the funny init.py empty file was not even needed)

  2. telling Django the absolute path to this one directory, with either

export PYTHONPATH=/home/my_own_self/my_directory 

if you are running Django's own runserver or sticking it at the end in here:

WSGIDaemonProcess youpi_server python-home=/(some path)/django_venv python-path=(...path to views...):/home/my_own_self/my_directory 

if you are serving it in Apache, and

  1. inserting in the views lines like from my_own_module import my_own_function where my_own_module.py is a file in my_directory and my_own_function is a "def" in it.

Thank you Abdul Aziz Barkat!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.