Repository: activescott/python-package-example (language: python)

Issue:
FAQ Request
Cheers! Helped me get an idea about the python packaging.

I have a query. Would be great if we could answer that here! Thanks!

Suppose
I create a folder: `python-package-example/package-project/src/boopackage/xyz`
I create a python file in folder xyz: `test_xyz.py`

How do I import a function from `boomodule.py`?
Thanks!


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 0000000..4da5021
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,53 @@
+# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
+# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
+
+name: Python package
+
+on:
+  push:
+    branches: [ $default-branch ]
+  pull_request:
+    branches: [ $default-branch ]
+
+jobs:
+  build:
+
+    runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        python-version: [3.7, 3.8, 3.9]
+
+    steps:
+    - uses: actions/checkout@v2
+    - name: Set up Python ${{ matrix.python-version }}
+      uses: actions/setup-python@v2
+      with:
+        python-version: ${{ matrix.python-version }}
+    - name: Install dependencies
+      run: |
+        python3 -m pip install --upgrade pip
+        python3 -m pip install flake8 pytest
+        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
+    - name: Lint with flake8
+      run: |
+        # stop the build if there are Python syntax errors or undefined names
+        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
+        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
+        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
+    - name: build package
+      run: |
+        ./build-package.sh
+    - name: install package
+      run: |
+        ./install-built-package.sh
+    - name: test consuming package
+      run: |
+        ./test-consume-package.sh
+    - name: uninstall package
+      run: |
+        ./uninstall-package.sh
+
+
+
+
+
diff --git a/build-package-egg.sh b/build-package-egg.sh
deleted file mode 100755
index 5fb338c..0000000
--- a/build-package-egg.sh
+++ /dev/null
@@ -1,8 +0,0 @@
-#/bin/bash
-# see http://peak.telecommunity.com/DevCenter/PythonEggs
-pushd .
-cd ./package-project/src
-python ./setup.py bdist_egg
-popd .
-
-echo; echo
diff --git a/build-package.sh b/build-package.sh
index 3ab9fdb..b31cb9a 100755
--- a/build-package.sh
+++ b/build-package.sh
@@ -1,7 +1,14 @@
 #/bin/bash
+die () {
+	echo >&2 "$@"
+	exit 1
+}
+
 pushd .
 cd ./package-project/src
-python ./setup.py sdist
+# Warning: eggs are deprecated in favor of wheels, and not supported by pip. - https://setuptools.readthedocs.io/en/latest/setuptools.html#bdist-egg-create-a-python-egg-for-the-project
+# python3 ./setup.py bdist_egg || die 'python setup failed'
+python3 ./setup.py bdist_wheel || die 'python setup failed'
 popd .
 
 echo; echo
diff --git a/install-built-package.sh b/install-built-package.sh
index 0964750..0e08e75 100755
--- a/install-built-package.sh
+++ b/install-built-package.sh
@@ -3,4 +3,4 @@
 
 #only works after running build-package.sh
 
-pip install ./package-project/src/dist/boopackage-1.0.tar.gz
\ No newline at end of file
+pip3 install --no-cache-dir ./package-project/src/dist/boopackage-1.0-py2.py3-none-any.whl
diff --git a/install-devmode-package.sh b/install-devmode-package.sh
index 28a61a5..a658857 100755
--- a/install-devmode-package.sh
+++ b/install-devmode-package.sh
@@ -1,10 +1,10 @@
 #/bin/bash
-# https://packaging.python.org/distributing/#working-in-development-mode
+# https://packaging.python.org/guides/distributing-packages-using-setuptools/#working-in-development-mode
 pushd .
 cd ./package-project/src/
-pip install -e .
+pip3 install -e ./
 popd
 
-echo; echo
+echo; echo "Showing package info:"
 
-pip show boopackage
+pip3 show boopackage
diff --git a/package-project/src/boopackage/barmodule.py b/package-project/src/boopackage/barmodule.py
new file mode 100644
index 0000000..39025a1
--- /dev/null
+++ b/package-project/src/boopackage/barmodule.py
@@ -0,0 +1,5 @@
+from __future__ import print_function
+
+
+def barfunc():
+    print('barfunc')
diff --git a/package-project/src/boopackage/boomodule.py b/package-project/src/boopackage/boomodule.py
index 7f00fb5..02e42c9 100644
--- a/package-project/src/boopackage/boomodule.py
+++ b/package-project/src/boopackage/boomodule.py
@@ -1,5 +1,16 @@
 from __future__ import print_function
+# demonstrating relative verses absolute imports as described at https://www.python.org/dev/peps/pep-0328/
+from boopackage.barmodule import barfunc
+from .barmodule import barfunc as barfunc_relative
+
 
 def boofunc():
-  print('boofunc')
+    print('boofunc')
+
+    print('testing barfunc via absolute/normal import...')
+    barfunc()
+    print('testing barfunc via absolute import complete.')
 
+    print('testing barfunc via relative import...')
+    barfunc_relative()
+    print('testing barfunc via relative import complete.')
diff --git a/uninstall-package.sh b/uninstall-package.sh
index 4631330..992084e 100755
--- a/uninstall-package.sh
+++ b/uninstall-package.sh
@@ -1,3 +1,5 @@
 #/bin/bash
 # https://packaging.python.org/distributing
-pip uninstall boopackage
\ No newline at end of file
+# https://packaging.python.org/guides/distributing-packages-using-setuptools/#working-in-development-mode
+
+pip3 uninstall --yes --no-cache-dir boopackage


Test patch:
diff --git a/test-consume-package.sh b/test-consume-package.sh
index 6a5c970..df293fa 100755
--- a/test-consume-package.sh
+++ b/test-consume-package.sh
@@ -1,4 +1,4 @@
 #/bin/bash
-# https://packaging.python.org/distributing/#working-in-development-mode
+# https://packaging.python.org/guides/distributing-packages-using-setuptools/#working-in-development-mode
 
-python ./package-consumer-project/consumepackage.py
\ No newline at end of file
+python3 ./package-consumer-project/consumepackage.py