-
Notifications
You must be signed in to change notification settings - Fork 14
Fix/pythonpath #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix/pythonpath #22
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fc1b0d4
Add PYTHONPATH setup script, remove old .env file
simonkurtz-MSFT 4a6c26c
Add PYTHONPATH setup
simonkurtz-MSFT 268cd02
Format
simonkurtz-MSFT 4ac0b47
Merge branch 'main' into fix/pythonpath
simonkurtz-MSFT 2415045
Update Python environment setup instructions and configuration
anotherRedbeard 38121e9
Sort the settings
simonkurtz-MSFT 5ea4512
Minor setup copy improvements
simonkurtz-MSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,23 @@ | ||
| { | ||
| "python.analysis.extraPaths": [ | ||
| "${workspaceFolder}/shared/python" | ||
| ], | ||
| "python.analysis.completeFunctionParens": true, | ||
| "plantuml.diagramsRoot": "diagrams/src", | ||
| "plantuml.exportFormat": "svg", | ||
| "plantuml.exportOutDir": "diagrams/out", | ||
| "plantuml.java": "C:\\Program Files\\OpenJDK\\jdk-22.0.2\\bin\\java.exe", | ||
| "plantuml.render": "Local", | ||
| "python.analysis.autoIndent": true, | ||
| "python.analysis.completeFunctionParens": true, | ||
| "python.analysis.diagnosticSeverityOverrides": { | ||
| "reportDuplicateImport": "warning", | ||
| "reportUndefinedVariable": "information", | ||
| "reportUnusedVariable": "information" | ||
| }, | ||
| "python.analysis.extraPaths": [ | ||
| "${workspaceFolder}/shared/python" | ||
| ], | ||
| "python.defaultInterpreterPath": "./venv/bin/python", | ||
| "python.envFile": "${workspaceFolder}/.env", | ||
| "plantuml.render": "Local", | ||
| "plantuml.exportFormat": "svg", | ||
| "python.terminal.activateEnvironment": true, | ||
| "terminal.integrated.env.windows": { | ||
| "PATH": "${env:PATH}" | ||
| }, | ||
| "plantuml.java": "C:\\Program Files\\OpenJDK\\jdk-22.0.2\\bin\\java.exe", | ||
| "plantuml.diagramsRoot": "diagrams/src", | ||
| "plantuml.exportOutDir": "diagrams/out" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "version": "2.0.0", | ||
| "tasks": [ | ||
| { | ||
| "label": "Setup Python Environment", | ||
| "type": "shell", | ||
| "command": "${config:python.pythonPath}", | ||
| "args": [ | ||
| "setup/setup_python_path.py", | ||
| "--generate-env" | ||
| ], | ||
| "group": "build", | ||
| "presentation": { | ||
| "echo": true, | ||
| "reveal": "always", | ||
| "focus": false, | ||
| "panel": "shared", | ||
| "showReuseMessage": true, | ||
| "clear": false | ||
| }, | ||
| "problemMatcher": [], | ||
| "detail": "Configure PYTHONPATH for cross-platform compatibility" | ||
| } | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Python Environment Setup | ||
|
|
||
| Configures cross-platform PYTHONPATH for APIM Samples. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```shell | ||
| python setup/setup_python_path.py --generate-env | ||
| ``` | ||
|
|
||
| This script auto-detects the project root and generates a `.env` file that VS Code uses for Python path configuration. If for some reason the `python` command is not found, please try adding your virtual environment's `bin` or `Scripts` directory to your system's PATH variable. An example command to do this for a virtual environment named `venv` is: | ||
|
|
||
| ```shell | ||
| source .venv/bin/activate | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| """ | ||
| Cross-platform PYTHONPATH setup for APIM Samples. | ||
|
|
||
| This script automatically detects the project root and configures PYTHONPATH | ||
| to include shared Python modules. Cross-platform compatibility is achieved by: | ||
| - Using pathlib.Path for all file operations (handles Windows/Unix path separators) | ||
| - Using absolute paths (eliminates relative path issues across platforms) | ||
| - Using UTF-8 encoding explicitly (ensures consistent file encoding) | ||
| - Using Python's sys.path for runtime PYTHONPATH configuration | ||
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path # Cross-platform path handling (Windows: \, Unix: /) | ||
|
|
||
|
|
||
| def get_project_root() -> Path: | ||
| """ | ||
| Get the absolute path to the project root directory. | ||
|
|
||
| Cross-platform strategy: | ||
| - Uses pathlib.Path for consistent path operations across OS | ||
| - Searches upward from script location to find project indicators | ||
| - Returns absolute paths that work on Windows, macOS, and Linux | ||
|
|
||
| Returns: | ||
| Path: Absolute path to project root directory | ||
| """ | ||
|
|
||
| # Start from script's parent directory (since we're in setup/ folder) | ||
| # Path(__file__).resolve() gives absolute path, .parent.parent goes up two levels | ||
| start_path = Path(__file__).resolve().parent.parent | ||
|
|
||
| # Project root indicators - files that should exist at project root | ||
| # These help identify the correct directory regardless of where script is run | ||
| indicators = ['README.md', 'requirements.txt', 'bicepconfig.json'] | ||
| current_path = start_path | ||
|
|
||
| # Walk up the directory tree until we find all indicators or reach filesystem root | ||
| while current_path != current_path.parent: # Stop at filesystem root | ||
| # Check if all indicator files exist in current directory | ||
| if all((current_path / indicator).exists() for indicator in indicators): | ||
| return current_path | ||
| current_path = current_path.parent | ||
|
|
||
| # Fallback: if indicators not found, assume parent of script directory is project root | ||
| # This handles cases where the project structure might be different | ||
| return Path(__file__).resolve().parent.parent | ||
|
|
||
|
|
||
| def setup_python_path() -> None: | ||
| """ | ||
| Add shared Python modules to PYTHONPATH for runtime import resolution. | ||
|
|
||
| This modifies sys.path in the current Python session to enable imports | ||
| from the shared/python directory. Cross-platform compatibility: | ||
| - Uses pathlib for path construction (handles OS-specific separators) | ||
| - Converts to string only when needed for sys.path compatibility | ||
| - Uses sys.path.insert(0, ...) to prioritize our modules | ||
| """ | ||
|
|
||
| project_root = get_project_root() | ||
| # Use pathlib's / operator for cross-platform path joining | ||
| shared_python_path = project_root / 'shared' / 'python' | ||
|
|
||
| if shared_python_path.exists(): | ||
| # Convert Path object to string for sys.path compatibility | ||
| shared_path_str = str(shared_python_path) | ||
|
|
||
| # Check if path is already in sys.path to avoid duplicates | ||
| if shared_path_str not in sys.path: | ||
| # Insert at beginning to prioritize our modules over system modules | ||
| sys.path.insert(0, shared_path_str) | ||
| print(f"Added to PYTHONPATH: {shared_path_str}") | ||
simonkurtz-MSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def generate_env_file() -> None: | ||
| """ | ||
| Generate .env file with cross-platform absolute paths for VS Code integration. | ||
| Creates a .env file that VS Code's Python extension reads to configure | ||
| the Python environment. Cross-platform features: | ||
| - Uses absolute paths (no relative path issues) | ||
| - Explicit UTF-8 encoding (consistent across platforms) | ||
| - pathlib handles path separators automatically (\\ on Windows, / on Unix) | ||
| - Works with VS Code's python.envFile setting | ||
| """ | ||
|
|
||
| project_root = get_project_root() | ||
| shared_python_path = project_root / 'shared' / 'python' | ||
|
|
||
| # Create .env file content with absolute paths | ||
| # These paths will be automatically correct for the current platform | ||
| env_content = f"""# Auto-generated PYTHONPATH for VS Code - Run 'python setup/setup_python_path.py' to regenerate | ||
| PROJECT_ROOT={project_root} | ||
| PYTHONPATH={shared_python_path} | ||
| """ | ||
|
|
||
| env_file_path = project_root / '.env' | ||
|
|
||
| # Use explicit UTF-8 encoding for cross-platform text file compatibility | ||
| # This ensures the file reads correctly on all operating systems | ||
| with open(env_file_path, 'w', encoding='utf-8') as f: | ||
| f.write(env_content) | ||
|
|
||
| print() | ||
| print(f"Generated .env file : {env_file_path}") | ||
| print(f"PROJECT_ROOT : {project_root}") | ||
| print(f"PYTHONPATH : {shared_python_path}\n") | ||
| print("All done!\n") | ||
|
|
||
|
|
||
| # Script entry point - handles command-line arguments | ||
| if __name__ == "__main__": | ||
| # Check for --generate-env flag to create .env file for VS Code | ||
| if len(sys.argv) > 1 and sys.argv[1] == "--generate-env": | ||
| generate_env_file() | ||
| else: | ||
| # Default behavior: modify current session's PYTHONPATH | ||
| setup_python_path() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.