Setting up multi-module Python environment with X-Modules and Develop Mode
Published: 2020-12-09 10:42:18

---
canonical_url: https://support.tmatesoft.com/t/setting-up-a-multi-module-python-environment-with-git-x-modules-and-develop-mode/2517
---
Quite often a Python project consists not only of an app but also of some libraries that could also be used by other projects.
 - On one hand, it's very convenient to work with the app repository independently of libraries assuming the libraries are installed on the system ([virtual environment](https://docs.python.org/3/library/venv.html)). 
 - On the other hand, this creates a painful problem of "change the library" -> "install the library" -> "check the change from the app development setup" cycle.
 
This post shows, how you can manage this setup with [Git X-Modules](https://gitmodules.com).

For the Git server, we will use Atlassian [Bitbucket Server](https://www.atlassian.com/software/bitbucket/download)/[Data Center](https://www.atlassian.com/enterprise/data-center/bitbucket) --- one of the most popular self-hosting Git solutions. It's also convenient because there's a special [Git X-Modules App](https://marketplace.atlassian.com/apps/1223696) for it. If you are on any other Git Server, you may still use Git X-Modules as a [command-line tool](https://gitmodules.com).

**The configuration.** As an example, we'll create an app (let's call it **'qsolver'**) that solves quadratic equations. I will also create a library (**'qsolverlib**) that performs mathematical calculations, so the 'qsolver' app will use it to output the solution.

Finally, there will be a **'qsolver-project'** containing both components. This will help us to pinpoint the version of 'qsolverlib' used by 'qsolver' because otherwise 'qsolver' would use 'qsolverlib' installed on the system that could be of the wrong version. This is especially useful in real life when some problem happens to some older version of the application and it's important to deduce the library version used for that application.


![01-structure|434x264](https://support.tmatesoft.com/uploads/default/original/1X/95898270985e8c5a6ed8c9191aeaea97f4d3ae90.png) 

We will create 3 Git repositories: for 'qsolverlib', 'qsolver', and 'qsolver-project'.

**Step 1.** Create 'qsolverlib' Git repository.
Create 'qsolverlib' Git repository on Atlassian Bitbucket Server/Data Center.
![02-create-qsolverlib-repository|658x500](https://support.tmatesoft.com/uploads/default/original/1X/4f0e5f04f16fbdbccaa51bcdfca6f30165ad8cb6.png) 
Clone this empty Git repository:

```
git clone http://example.org/scm/qsolver/qsolverlib.git qsolverlib/
cd qsolverlib/
```

Inside this Git repository create the 'qsolverlib' subdirectory and create the `solve.py` file in it with the following content:
```
"""Quadratic equation solver."""

import math

def solve(a, b, c):
    """Solve equation ax^2 + bx + c = 0, return a root or a list with roots"""
    D = b * b - 4 * a * c
    if D < 0:
        return None
    elif D == 0:
        return -b / (2 * a)
    else:
        return [(-b - math.sqrt(D)) / (2 * a), (-b + math.sqrt(D)) / (2 * a)]
 
```

Also create an empty "\_\_init\_\_.py" file in the "qsolverlib" subdirectory:

```
touch qsolverlib/__init__.py
```

Finally, create "setup.py" at the root of the Git repository:
```
"""Setup file for qsolverlib."""

import setuptools

setuptools.setup(
    name="qsolverlib",
    version="0.0.1",
    description="A library solving quadratic equations",
    url="http://example.org/scm/qsolver/qsolverlib.git",
    author="Dmitry Pavlenko",
    author_email="pavlenko@tmatesoft.com",
    license="MIT",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python"
    ],
    packages=setuptools.find_packages(),
    python_requires=">=3.7"
)
```

So the resulting structure looks as follows:
```
.
├── qsolverlib
│   ├── __init__.py
│   └── solve.py
└── setup.py
```

Commit and push the package.
```
git add .
git commit -m "Initial."
git push origin master
```
![03-qsolverlib-repository-toc|560x500](https://support.tmatesoft.com/uploads/default/original/1X/f910af358fb5259f68772dbd51edb46dd433c649.png) 


**Step 2.** Create Python virtual environment and install the package there.

Python virtual environment allows installing python packages locally into some directory without requiring to install them at the system-wide level. To create the environment run (not inside the Git repository but inside it parent directory):
```
virtualenv .venv/
```
or
```
python3 -m venv .venv/
```
As result, the ".venv/" directory will be created. This environment should be activated to be used:
```
source .venv/bin/activate
```
After that "(.venv)" will be prepended to the command prompt indicating that we're working with the virtual environment.

Now install the 'qsolverlib' package into this environment:
```
pip3 install qsolverlib/
```
For me it prints:
```
...
  error: invalid command 'bdist_wheel'
  
  ----------------------------------------
  Failed building wheel for qsolverlib
...
```
This can be resolved by installing the 'wheel' package:
```
pip3 install wheel
```

Then retrying
```
pip3 install qsolverlib/
```
is successful. Now the library can be used by an app if the app is running within the same virtual environment.

The virtual environment can be de-activated with
```
deactivate
```
command.


**Step 3.** Create a 'qsolver' app.

Create 'qsolver' Git repository on Atlassian Bitbucket Server/Data Center.
![04-create-qsolver-repository|622x500](https://support.tmatesoft.com/uploads/default/original/1X/c59517831a69d5533a883b76e7f986cab7c8bd8e.png) 
Clone this empty Git repository:

```
git clone http://example.org/scm/qsolver/qsolver.git qsolver/
cd qsolver/
```

Create `app.py` with the following content:

```
from qsolverlib import solve

def main():
    print(solve.solve(1, -3, 2))

if __name__ == "__main__":
    main()
```

I.e. it solves: x^2 - 3x + 2 = 0

Commit and push this file:
```
git add app.py
git commit -m "Initial."
git push origin master
```

Check that the app works:
```
python3 app.py 
[1.0, 2.0]
```
Indeed, x^2 - 3x + 2 = (x - 1)(x - 2)


**Step 4.** Develop mode of package.

If we now want to change the "solve()" function, we have to edit solve.py in the 'qsolverlib' repository, install the package, and only then test how it works in app.py in 'qsolver'. This is too inconvenient. "Develop mode" is the solution.

To install 'qsolverlib' in develop mode run.

```
pip3 install -e qsolverlib/
```

It logically links qsolverlib installed in the virtual environment with the sources of 'qsolverlib', so any changes to 'qsolverlib' sources become effective for the app immediately.

**Step 5.** Create a parent project to pinpoint versions of 'qsolverlib' and 'qsolver'.

Create 'qsolver-project' Git repository on Atlassian Bitbucket Server/Data Center.
![06-create-qsolver-project-repository|649x500](https://support.tmatesoft.com/uploads/default/original/1X/d3cb033cbff4035b040167af4f608b111eee1f39.png) 
Now we will use [Git X-Modules](https://tmatesoft.com/x-modules), make sure you have it installed. To do that, go to `Administration | Find new apps | Search the `[Marketplace](https://marketplace.atlassian.com/apps/1223696/git-x-modules-for-bitbucket-server?hosting=server&tab=overview) and type "X-Modules".
![07-install-x-modules-app|690x203](https://support.tmatesoft.com/uploads/default/original/1X/7c874cca26baf6d433d63b89b39b0eedf20c36d1.png) 
Go to the 'qsolver-project' repository page on Bitbucket. As the app is now installed, there's an X-Modules button, click it.
![09-x-modules-button|690x181](https://support.tmatesoft.com/uploads/default/original/1X/30c200fa3dd3234e27f1782f55b570513b4dbce7.png) 
The repository is empty, so click "Create Default Branch" to create the "master" branch.
![10-x-modules-create-default-branch|690x285](https://support.tmatesoft.com/uploads/default/original/1X/bb5a571da2a319cbfaed6af9471d17934181f539.png) 
Click "Add Module" to add 'qsolverlib' as the first module.
![11-x-modules-add-first-module|690x275](https://support.tmatesoft.com/uploads/default/original/1X/46e007817169e53196cb4ad23826dfded5c33371.png) 
Choose the 'qsolverlib' repository and the master branch.
![11-x-modules-choose-qsolverlib|690x461](https://support.tmatesoft.com/uploads/default/original/1X/14119dc64241bf7500eec578a4f9fd2aafacf289.png) 
Make sure "This Repository Path" is 'qsolverlib'. It's a path for the insertion point of the 'qsolverlib' module. Click "Add Module".
![12-x-modules-add-second-module|690x427](https://support.tmatesoft.com/uploads/default/optimized/1X/0af10afe2bc5fb1a725b7cc3b3da5d7366bb3124_2_1035x640.png) 
Instead of applying changes immediately click "Add Module" again, to add the second module (for the app).

Choose the 'qsolver' repository and the master branch.
![13-x-modules-choose-qsolver|690x457](https://support.tmatesoft.com/uploads/default/original/1X/02fa96c0c74dd0042077d5a365fb56c8442c872f.png) 
Make sure "This Repository Path" is 'qsolver'. Again, this is a path for the insertion point of the 'qsolver' module.
![14-x-modules-second-module-preview|690x465](https://support.tmatesoft.com/uploads/default/original/1X/2fc6694248d657418ed479e14bad98d297bf6a09.png) 
Click "Add Module".
![15-x-modules-apply-changes|690x456](https://support.tmatesoft.com/uploads/default/optimized/1X/322f17f2a78f233877a3e62a9124f317f247f88a_2_1035x684.png) 
Now apply the changes.

Now 'qsolver-project' repository contains both 'qsolverlib' and 'qsolver' X-Modules. These are regular Git directories that are synchronized with the corresponding repositories.
![16-qsolver-project-toc|575x499](https://support.tmatesoft.com/uploads/default/original/1X/6ecb06a74c2457fb9ab017ed1d1e19c790b833af.png) 
So now one can clone the 'qsolver-project' repository and both 'qsolverlib' and 'qsolver' components will be there.

**Step 6.** Quickstart script.

Suppose you have a new colleague who wants to start working on both 'qsolverlib' and 'qsolver' as soon as possible. For this purpose, we will create a "create-virtualenv.sh" script that would set up the development environment.

Clone 'qsolver-project':
```
git clone http://example.org/scm/qsolver/qsolver-project.git qsolver-project
cd qsolver-project
```

The current structure of 'qsolver-project' is:
```
├── qsolver
│   └── app.py
└── qsolverlib
    ├── qsolverlib
    │   ├── __init__.py
    │   └── solve.py
    └── setup.py
```

It already contains 'qsolverlib' and 'qsolver' components, so one doesn't need to clone them separately.

Create "create-virtualenv.sh" script with the following content:
```
#!/bin/sh

if [ -z "$1" ]; then
    echo "Path for the virtual environment required, e.g.:"
    echo ""
    echo "    $0 .venv/"
    echo ""
    exit 1
fi

python3 -m venv "$1"
$1/bin/pip3 install wheel
$1/bin/pip3 install -e qsolverlib/

echo "Virtual environment created at $1"
```

Add, commit, and push this script:
```
git add create-virtualenv.sh
git commit -m "create-virtualenv.sh script added."
git push origin master
```

So now for a new colleague, it will be enough to clone 'qsolver-project' and run this script to start working on the project:

```
sh create-virtualenv.sh .venv/
source .venv/bin/activate
```


**Step 7.** An example: make a change to 'qsolverlib'.

Now let's improve our solve() function to handle the case a=0, i.e. to solve bx+c=0 equation.

Edit qsolverlib/qsolverlib/solve.py and qsolver/app.py:
![17-git-diff|656x500](https://support.tmatesoft.com/uploads/default/original/1X/0bea3fd99bbbb1cfbe80040c827dda0b1b9ddb11.png) 
Commit and push the changes with to both 'qsolverlib' and 'qsolver' as a single commit in 'qsolver-project'.

```
git commit -a -m "solve() improved to handle linear equations as well."
git push origin master
```

Now you can look at 'qsolverlib' and 'qsolver' histories on Bitbucket: they are updated!
![18-qsolverlib-history|690x267](https://support.tmatesoft.com/uploads/default/optimized/1X/a6b97c3b06d64a75a1beeb77211b9019c3a390ad_2_1035x400.png) ![19-qsolver-history|690x265](https://support.tmatesoft.com/uploads/default/optimized/1X/4aac5da2936ba25e2149d9e6c42a34028365b704_2_1035x397.png) 
The synchronization is bi-directional, updates of 'qsolverlib' and 'qsolver' also get into 'qsolver-project' automatically.