如何让Python脚本使用本地steampy库并解决导入报错问题
Hey there! Let's figure out how to get your Python script using your local steampy clone instead of the globally installed version—plus fix those annoying pylint import errors you're seeing. This is totally analogous to npm link in Angular, just with Python's own tools and workflows.
1. Use pip install -e (the Python equivalent of npm link)
This is the cleanest way to link your local steampy directory to your Python environment, so your script automatically uses the local version (and any changes you make to the local code will take effect immediately). Here's how:
- Open your terminal, navigate to the root folder of your local steampy clone (the one containing
setup.pyorpyproject.toml). - Run this command:
pip install -e .
The -e flag stands for "editable"—it creates a symbolic link from your local directory to Python's site-packages, so your system treats the local code as the installed version.
2. Manually add the local path to your script
If you don't want to mess with pip's editable install, you can directly tell Python to look for the local steampy directory first. Add these lines at the very top of your script:
import sys # Replace with the actual path to your local steampy root folder sys.path.insert(0, "/absolute/path/to/your/local/steampy")
By inserting the path at index 0, you're making Python check this directory before looking for the globally installed version.
3. Fixing the pylint import errors
Pylint is throwing those errors because it doesn't know about your local steampy directory—here's how to fix it:
- Option 1: Create a
.pylintrcfile
In your project's root directory, make a file named.pylintrcand add this section:
This tells pylint to add the local path every time it runs.[MASTER] init-hook='import sys; sys.path.insert(0, "/absolute/path/to/your/local/steampy")' - Option 2: Configure VS Code (if you're using it)
Open your settings (Ctrl+, or Cmd+), search for "Python > Analysis: Extra Paths", and add the absolute path to your local steampy folder. This will make the VS Code Python extension (and pylint) recognize the local imports. - Option 3: Set the PYTHONPATH environment variable
In your terminal, run this before running pylint or your script:
This temporarily adds the local path to Python's search path for that terminal session.export PYTHONPATH="/absolute/path/to/your/local/steampy:$PYTHONPATH"
One quick check: Make sure your local steampy directory has an __init__.py file in the root and in subdirectories like steampy/exceptions—Python needs these files to recognize folders as importable packages. Most well-maintained libraries (like steampy) already have these, but it's worth double-checking if you've modified anything.
内容的提问来源于stack exchange,提问作者iamdlm




