Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python multiple project practice

So I have multiple projects like

DevelFolder/
   ├──MyuniversalModule/
      ├── .git
      ├── __init__.py
      └── mymodule.py
   ├──project01/
      └── myproject01.py
   └──project02/
      └── myproject02.py

Now, In my 2 projects I would like to import "MyuniversalModule" which I have been developing separately and it is still work in progress and i will be changing it for needs of my projects. After finishing those projects, I will add this module but at this moment I would like to develop and test it separately, as it may be used in multiple places.

I was searching for solutions, like importing from parent directory

from ... import module

or

import imp
foo = imp.load_source('module.name', '/path/to/file.py')
foo.MyClass()

or

import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))

But what is the best practise of working in such situation ?

like image 742
tmdag Avatar asked Nov 25 '25 11:11

tmdag


1 Answers

Relative imports are usually only used for submodules which are unique to your project. If you have a module which will be shared by multiple projects, a common practice is to package it separately as a pip package, and then in each new project use

pip install local/path/to/my/other/package 

to install it as a requirement. You can also use pip install git+ssh to install from a private git server.

like image 62
maxymoo Avatar answered Nov 27 '25 00:11

maxymoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!