Cython setup (should be automatic)
Note
If you installed LightWin with pip install -e . and there was no error, you do not need to read this section.
You can check that everything works as expected by running pytest -m cython.
Cython is an optional but highly recommended tool to speed up beam dynamics calculations. Here’s how to properly install and use Cython with the project:
1. Installing Cython
Ensure Cython is installed before installing other packages like pymoo to take full advantage of its capabilities:
Using
pip:pip install cythonUsing
conda:conda install cython -c conda-forge
2. Compiling Cython modules
Some parts of LightWin, in particular the Envelope1D beam calculator, have Cython-optimized code that need to be optimized.
Follow this steps to compile the modules:
Navigate to the
LightWinbase directory:cd /path/to/LightWin/
Run the
setupscript:python setup.py build_ext --inplace
This command compiles the Cython files and places the compiled modules (.pyd or .so extensions) in the appropriate directories.
3. Handling compiled files
After compilation, the compiled files should be automatically places in the correct locations. If not, manually move the created files:
Unix (Linux/macOS):
build/lib.linux-XXX-cpython=3XX/beam_calculation/cy_envelope_1d/transfer_matrices.cpython-3XX-XXXX-linux-gnu.soWindows:
build/lib.win-XXXX-cpython-3XX/beam_calculation/cy_envelope_1d/transfer_matrices.cp3XX-win_XXXX.pyd
To:
/path/to/LightWin/src/lightwin/beam_calculation/cy_envelope_1d/.
Microsoft Visual C++ 14.0 or greater is required error is covered in the troubleshooting section.
4. Restarting Your Interpreter
If you are using an IDE like Spyder or VSCode, remember to restart the kernel after compiling the Cython modules to ensure they are correctly loaded.
5. Testing Cython Compilation
To verify that everything is set up correctly, run the test suite using pytest.
This will check if the Cython modules are properly integrated:
pytest -m cython
See also