如何使用PyBuilder和Behave搭建Python项目?新手遇依赖问题求助
Hey there! Let's walk through setting up your Behave project with PyBuilder to fix that dependency issue and get things running smoothly. Here's a step-by-step guide tailored to your setup:
First, make sure PyBuilder is installed on your system. Run this terminal command:pip install pybuilder
Navigate to your project root (the folder containing your features directory) and run:pyb --start-project
This will create core PyBuilder files like build.py and a src folder. Don't worry about the src folder—we’ll adjust config to work with your existing Behave structure.
Open the generated build.py file in your editor. Replace its default content with this configuration, which adds your required dependencies and points PyBuilder to your existing Behave files:
from pybuilder.core import use_plugin, init # Load essential PyBuilder plugins use_plugin("python.core") use_plugin("python.install_dependencies") use_plugin("python.behave") # Plugin to run Behave tests name = "your-project-name" # Replace with your project's name default_task = "publish" @init def set_project_properties(project): # Add dependencies: Behave and the missing pynput module project.depends_on("behave") project.depends_on("pynput") # Tell PyBuilder where your Behave files live project.set_property("behave_features_dir", "features") project.set_property("behave_steps_dir", "features/steps")
Run this command to install Behave, pynput, and any other required packages:pyb install_dependencies
This will resolve the ModuleNotFoundError: No module named 'pynput' error for good.
You can now execute your tests directly via PyBuilder:pyb behave
Or run a full build (including dependency checks and tests) with:pyb
Since you already have your features directory with .feature files, steps folder, and environment.py (note: you mentioned environment.pys—make sure this is named environment.py for Behave to recognize it), PyBuilder will pick these up automatically thanks to the properties we set. No need to move or rename your existing files!
If you still run into dependency issues, double-check that you’re using the correct virtual environment (if you’re using one). Sometimes dependencies install to the global Python instance instead of your project’s venv—activate your venv first before running PyBuilder commands.
内容的提问来源于stack exchange,提问作者Ana Franco

