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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With