skip to Main Content

I am trying to check my home edition of OneDrive for duplicate files.
On https://developer.microsoft.com/en-us/graph/graph-explorer
I can see results for simple queries after giving consent. Yay?

But constantly getting this error in VS Code on Windows 11, Python v3.11:

"Import "msgraph.core" could not be resolvedPylancereportMissingImports"

I am not in any conda environment.
Installed it uninstalled then did this:

PS C:UsersTest> pip show msgraph-core     
>>
**Name: msgraph-core
Version: 1.0.0
Summary: Core component of the Microsoft Graph Python SDK
Home-page: 
Author: 
Author-email: Microsoft <[email protected]>
License: MIT License**
.....blah blahc blah
Required-by: msgraph-sdk

I have not filled out the AZ client ID (because it is a home version, I can’t find that..yet)
but should it still be giving me the msgraph.core error – it seems it can’t find it?

Code:

from msgraph.core import GraphClient
from azure.identity import InteractiveBrowserCredential
import hashlib

# Azure app registration details
CLIENT_ID = 'YOUR_CLIENT_ID'  # Replace with your Azure AD client ID

# Scopes required by the app
SCOPES = ['Files.ReadWrite.All']  # Scope for OneDrive access

# Authentication with Interactive Browser Credential
credential = InteractiveBrowserCredential(client_id=CLIENT_ID)
graph_client = GraphClient(credential=credential, scopes=SCOPES)

# Create an empty dictionary to store the hashes and file names
hashes = {}

# Function to process files and find duplicates in a folder
def process_files(folder_id):
    request_url = f'me/drive/items/{folder_id}/children'
    drive_items = graph_client.get(request_url).json()

    for item in drive_items['value']:
        # Check if the item is a file
        if 'file' in item:
            # Get the file content
            file_content = graph_client.get(item['@microsoft.graph.downloadUrl']).content

            # Create a hash object using md5
            hash_obj = hashlib.md5()
            hash_obj.update(file_content)
            hash_digest = hash_obj.hexdigest()

            if hash_digest in hashes:
                print(f"{item['name']} is a duplicate of {hashes[hash_digest]}")
            else:
                hashes[hash_digest] = item['name']
        elif 'folder' in item:
            # If the item is a folder, process its contents
            process_files(item['id'])

# Start processing from the root folder
root_folder = graph_client.get('me/drive/root').json()
process_files(root_folder['id'])

I was expecting NOT to get:

"Import "msgraph.core" could not be resolved"

and perhaps some code error.

2

Answers


  1. This means module msgraph.core was not found in your current Python interpreter.

    1. You can try to use shortcuts Ctrl+Shift+P and type "Python: Select Interpreter" to choose the python interpreter which you installed this module.
    2. If you just want to disable this warning. You could add the following codes to your settings.json:
    "python.analysis.diagnosticSeverityOverrides": {
        "reportMissingImports": "none"
    }
    
    Login or Signup to reply.
  2. I had a similar problem when upgrading to the newly released 1.0.0.
    Change your import to msgraph_core. But be aware that GraphClient is no longer available.
    You will have to downgrade your installation to 0.2.2 if you want to use that import and class.
    See the package history for more information.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search