Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

added 2504 characters in body
Source Link
JL Peyret
  • 12.2k
  • 4
  • 67
  • 97

One way to do that is to put your common code in a "package", say mycommoncommon and use pip to do an editable local install. via pip install -e common.

Now, writing an installable package is (削除) not that trivial, but (削除ここまで) TRIVIAL nowadays and this is likely the more robust approach, over modifying pythonpath with .pth files - been there, done that myself. Keep in mind you need the minimum necessary packaging for a pip local install, which may be quite a lot less than a package that is ready for pypi uploading and installing.

I have had good success with this approach myself, but that's on packages that I had already prepped for pypi upload.

Now, as far as what your importimports can look like in your project A, B, C code you will find that many packages do an import of constituent files in their __init__.py.

One way to do that is to put your common code in a "package", say mycommon and use pip to do an editable local install. via pip install -e common.

Now, writing an installable package is (削除) not that trivial, but (削除ここまで) TRIVIAL nowadays and this is likely the more robust approach, over modifying pythonpath with .pth files - been there, done that myself. Keep in mind you need the minimum necessary packaging for a pip local install, which may be quite a lot less than a package that is ready for pypi uploading and installing.

I have had good success with this approach myself, but that's on packages that I had already prepped for pypi upload.

Now, as far as what your import can look like you will find that many packages do an import of constituent files in their __init__.py.

One way to do that is to put your common code in a "package", say common and use pip to do an editable local install. via pip install -e common.

Now, writing an installable package is (削除) not that trivial, but (削除ここまで) TRIVIAL nowadays and this is likely the more robust approach, over modifying pythonpath with .pth files - been there, done that myself.

Now, as far as what your imports can look like in your project A, B, C code you will find that many packages do an import of constituent files in their __init__.py.

added 2504 characters in body
Source Link
JL Peyret
  • 12.2k
  • 4
  • 67
  • 97

(disclaimer, I am an admin on my mac, but none of what I am doing here required sudo permissions).

One way to do that is to put your common code in a "package", say mycommon and use pip to do an editable local install. via pip install -e common.

Now, writing an installable package is not that trivial, but(削除) not that trivial, but (削除ここまで) TRIVIAL nowadays and this is likely the more robust approach (over, over modifying path bypythonpath with .pth files, - been there, done that myself). Keep in mind you need the minimum necessary packaging for a pip local install, which may be quite a lot less than a package that is ready for pypi uploading and installing.

OK, so how much work isIS setting up a locally installed package?

One way to do that is to put your common code in a "package", say mycommon and use pip to do an editable local install. via pip install -e common.

Now, writing an installable package is not that trivial, but this is likely the more robust approach (over modifying path by .pth files, been there, done that myself). Keep in mind you need the minimum necessary packaging for a pip local install, which may be quite a lot less than a package that is ready for pypi uploading and installing.

OK, so much work is setting a locally installed package?

(disclaimer, I am an admin on my mac, but none of what I am doing here required sudo permissions).

One way to do that is to put your common code in a "package", say mycommon and use pip to do an editable local install. via pip install -e common.

Now, writing an installable package is (削除) not that trivial, but (削除ここまで) TRIVIAL nowadays and this is likely the more robust approach, over modifying pythonpath with .pth files - been there, done that myself. Keep in mind you need the minimum necessary packaging for a pip local install, which may be quite a lot less than a package that is ready for pypi uploading and installing.

OK, so how much work IS setting up a locally installed package?

added 2504 characters in body
Source Link
JL Peyret
  • 12.2k
  • 4
  • 67
  • 97

One way to do that is to put your common code in a "package", say mycommon and use pip to do an editable local install. via pip install -e mycommoncommon.

After installation, your Python path is modified so that it includes the directory where mycommoncommon lives and your project-side code can then use it like:

from mycommoncommon.classa import ClassA

mycommoncommon/__init__.py:

import mycommoncommon
a = mycommoncommon.ClassA()
res = mycommoncommon.functions.foobar(42)
A
B in __init__.py

The full project structure ended up as:

.
├── common  👈 your Python code
│  ├── __init__.py 
│  └── classa.py
├── common.egg-info 👈 generated by pip install -e .
│  ├── PKG-INFO
│  ├── SOURCES.txt
│  ├── dependency_links.txt
│  └── top_level.txt
├── pyproject.toml 👈 an EMPTY file to make pip install -e work

One way to do that is to put your common code in a "package", say mycommon and use pip to do an editable local install. via pip install -e mycommon.

After installation, your Python path is modified so that it includes the directory where mycommon lives and your project-side code can then use it like:

from mycommon.classa import ClassA

mycommon/__init__.py:

import mycommon
a = mycommon.ClassA()
res = mycommon.functions.foobar(42)
A
B in __init__.py

One way to do that is to put your common code in a "package", say mycommon and use pip to do an editable local install. via pip install -e common.

After installation, your Python path is modified so that it includes the directory where common lives and your project-side code can then use it like:

from common.classa import ClassA

common/__init__.py:

import common
a = common.ClassA()
res = common.functions.foobar(42)
A
B in __init__.py

The full project structure ended up as:

.
├── common  👈 your Python code
│  ├── __init__.py 
│  └── classa.py
├── common.egg-info 👈 generated by pip install -e .
│  ├── PKG-INFO
│  ├── SOURCES.txt
│  ├── dependency_links.txt
│  └── top_level.txt
├── pyproject.toml 👈 an EMPTY file to make pip install -e work
added 2504 characters in body
Source Link
JL Peyret
  • 12.2k
  • 4
  • 67
  • 97
Loading
added 264 characters in body
Source Link
JL Peyret
  • 12.2k
  • 4
  • 67
  • 97
Loading
added 264 characters in body
Source Link
JL Peyret
  • 12.2k
  • 4
  • 67
  • 97
Loading
added 880 characters in body
Source Link
JL Peyret
  • 12.2k
  • 4
  • 67
  • 97
Loading
Source Link
JL Peyret
  • 12.2k
  • 4
  • 67
  • 97
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /