D
D
Dream2021-07-23 18:10:09
Python
Dream, 2021-07-23 18:10:09

What is the best way to use python libraries in multiple code files?

I have libraries, I have code on several files: is it better to store imports in one file and call this file in each code file? I just don't know how to make it more convenient, because some libraries are needed in one file, others in another

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-07-23
@dreameddd

Import as usual, in each file.
A generic file with imports will only complicate matters, but will not give any gain in code readability, since different files will require different modules.
I also advise you to follow the PEP8 standard in terms of the order of imports:
First imports of standard modules.
Then imports of third-party libraries (what I installed via pip).
Last but not least, module imports from your project.
Within each section, the elements can be ordered alphabetically, unless there is a particular reason to import them in a different order.

A
Alex F, 2021-07-23
@delvin-fil

I 'll add to Vindicar 's words :
PEP also implies exporting the PATH environment variable .
Therefore, you can do:

PATHMYLIB='/path/to/my/libs:$PATH'
export PATHMYLIB

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question