Python对接BitBucket遇库初始化报错,求助Rest API使用方案
TypeError: __init__() got an unexpected keyword argument 'strict' in Python BitBucket Integrations Hey there, let's break down how to fix that annoying error you're hitting when working with BitBucket in Python, plus cover the REST API approach you mentioned since you're new to it.
First off, that error isn't actually about BitBucket itself—it's a dependency version conflict between requests and urllib3 in your environment. Older versions of urllib3 expect a strict parameter when creating connections, but newer requests builds don't pass this anymore, causing the mismatch. On top of that, your BitBucket client setup has a small URL issue we need to fix. Let's walk through solutions step by step:
1. Resolve the Dependency Version Conflict
First, let's get your requests and urllib3 versions aligned to a compatible pair:
- Uninstall the current conflicting versions:
pip uninstall -y requests urllib3
- Install a tested, compatible combination:
pip install requests==2.27.1 urllib3==1.26.15
2. Fix Your BitBucket Client Initialization
Your original code pointed the BitBucket URL to a specific project/repo path, but the client expects the root URL of your BitBucket instance. Here's the corrected code:
from atlassian import BitBucket import getpass username = input("What is your username: ") password = getpass.getpass(prompt="Enter your password?: ") # Only use the base BitBucket URL, no project/repo suffix bitbucket = BitBucket( url="https://website.com:port", username=username, password=password ) # Fetch and print the project list project_data = bitbucket.project_list() print(project_data)
3. Alternative: Use BitBucket REST API Directly
If you still run into issues with third-party libraries, using the BitBucket REST API directly with requests is straightforward—no extra dependencies needed. Here are common operations you asked about:
Fetch Project List
import requests import getpass username = input("What is your username: ") password = getpass.getpass(prompt="Enter your password?: ") bitbucket_base_url = "https://website.com:port" # Call the BitBucket API to get projects response = requests.get( f"{bitbucket_base_url}/rest/api/1.0/projects", auth=(username, password), verify=False # Remove this if your BitBucket uses a trusted SSL certificate ) if response.status_code == 200: projects = response.json().get("values", []) print("Available Projects:") for proj in projects: print(f"- {proj['name']} (Key: {proj['key']})") else: print(f"Failed to fetch projects. Status Code: {response.status_code}") print(f"Error Details: {response.text}")
Clone a Repository (Pull Content)
For pulling code, using git directly via Python's subprocess is simpler than API calls:
import subprocess # Replace with your repo URL (include credentials if needed) repo_url = "https://username:password@website.com:port/projects/demo_projects/repos/your_repo.git" local_folder = "./my_local_repo" # Clone the repo subprocess.run(["git", "clone", repo_url, local_folder], check=True)
Push Changes to a Repository
Again, using git commands via subprocess is the standard approach:
import subprocess import os local_repo_path = "./my_local_repo" os.chdir(local_repo_path) # Stage, commit, and push changes subprocess.run(["git", "add", "."], check=True) subprocess.run(["git", "commit", "-m", "Automated update from Python script"], check=True) subprocess.run(["git", "push"], check=True)
内容的提问来源于stack exchange,提问作者Alehandro

