Template for setting up new python projects
Clone the repository and navigate to the project directory:
git clone [repository URL] project-name
cd project-name
Rename the src\template
to your desired project name.
Remove the old git and initialize a new git repository
rm -rf .git
git init
The project project-name consists of source code files located in the src folder, executable scripts in the scripts folder, and test files in the tests folder, facilitating development, execution, and testing of the project's functionality.
project-name/
├── LICENSE
├── pyproject.toml
├── README.md
├── src/
│ └── project-name/
│ ├── __init__.py
│ └── my_source_file1.py
├── scripts/
│ ├── script1.py
│ └── ...
└── tests/
├── test_my_source_file.py
└── ...
Create your source files in the src/project/ directory. These files should contain your methods, functions, and classes without any standalone execution.
Place your executable script files in the scripts/ directory. These files will serve as entry points to your package's functionality.
The scripts require the source files to be installed as if you were using the package as a third-party user. To install the project locally in editable mode, allowing you to make changes to the source code without reinstalling, run the following command:
pip install . -e
Create your test files in the tests/ directory. These files should contain your unit tests.
To run the tests, make sure you have the necessary testing framework installed (e.g., pytest). If not, install it using:
pip install pytest
Once installed, run the following command from the project's root directory:
pytest
This will execute all the tests in the tests/ directory.
Ensure that the requirements.txt
file is upto date and accurate for the project.
In pyproject.toml
adjust the name of the project.
Run the following commands to update the build process and build the project.
python3 -m pip install --upgrade build
python3 -m build
This command should output a lot of text and once completed should generate two files in the dist directory:
dist/
├── project-name-0.0.1-py3-none-any.whl
└── project-name-0.0.1.tar.gz
The tar.gz file is a source distribution whereas the .whl file is a built distribution. Newer pip versions preferentially install built distributions, but will fall back to source distributions if needed. You should always upload a source distribution and provide built distributions for the platforms your project is compatible with.