Reputation: 29
I have 2 Python projects:
Proj1(/var/www/proj1)
venv
requirments.txt
app
fun.py
fun2.py
app2
pdf.py
somefun2.py
Proj2(/var/www/proj2)
venv
requirments.txt
another
anotherfun.py
anotherfun2.py
someanother
someanotherfun.py
pdfproj2.py
Both work individually and both have different set of requirements.
Lets say pdf.py
from proj1
has a function generate
which will generate some PDFs. It will take all other modules(app/fun2
etc) in same project for it.
Now what I want is this functionality(pdf.py
->generate) I want to call in pdfproj2.py
in proj2
.
How is this possible?
Nb: I am not using any frameworks like flask/django etc
Upvotes: 2
Views: 251
Reputation: 20550
There are at least three approaches.
Change nothing. Pretty much.
Command line callers are already able to take advantage
of $ python proj1/app2/pdf.py arg...
, invoking generate()
.
Arrange for proj2pdf.py
to fork off a subprocess
and do exactly that.
Nothing changes in project1,
since its public API already supports this use case.
Notice that you might need to carefully finesse the PATH
& PYTHONPATH
env vars, as part of correctly invoking that pdf.py command.
That's the sort of setup that conda
and venv
are good at.
This is the quick-n-dirty approach. I do not recommend it.
Create project3, and incorporate source code from both existing projects. Take the union of all library dependencies.
Now you can call generate()
in the same address space,
the same process, as the calling python code.
Downside is: ugliness.
The bigger project's codebase is not as easily maintainable.
The "right" way to make generate()
available to project2,
or to any project, is to package it up.
Pretend you're going to publish on pypi.
Doesn't matter if you actually do,
let's just prepare for such a possibility.
Create setup.py
or similar, maybe use setuptools,
and create a wheel (or at least a tar) of project1.
There are many ways to do this, and best practices
continue to evolve, so I won't delve into details here.
Now you can list project1 as a dependency in project2's requirements.txt,
and import
it just like any other dep. Problem solved!
This is the best approach. It does involve a bit of work, and a gentle learning curve.
Upvotes: 5