Set Access Control to File/Folder (ACL)

本文提供了使用C#为指定文件和文件夹添加及移除访问控制条目的示例代码。通过这些示例,读者可以了解到如何针对特定帐户设置文件系统权限。
 

The following Code is from MSDN:

Set Access Control to File

using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string fileName = "test.xml";//注意,這裡要寫完整路徑或虛擬路徑

                Console.WriteLine("Adding access control entry for "
                    + fileName);

                // Add the access control entry to the file.
                AddFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Removing access control entry from "
                    + fileName);

                // Remove the access control entry from the file.
                RemoveFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        // Adds an ACL entry on the specified file for the specified account.
        public static void AddFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {


            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Add the FileSystemAccessRule to the security settings.
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);

        }

        // Removes an ACL entry on the specified file for the specified account.
        public static void RemoveFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {

            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Remove the FileSystemAccessRule from the security settings.
            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);

        }
    }
}
===================================================================

Set Access Control to Folder

using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class DirectoryExample
    {
        public static void Main()
        {
            try
            {
                string DirectoryName = "TestDirectory";//注意,這裡要寫完整路徑或虛擬路徑

                Console.WriteLine("Adding access control entry for " + DirectoryName);

                // Add the access control entry to the directory.
                AddDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Removing access control entry from " + DirectoryName);

                // Remove the access control entry from the directory.
                RemoveDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }

        // Adds an ACL entry on the specified directory for the specified account.
        public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new DirectoryInfo(FileName);

            // Get a DirectorySecurity object that represents the
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.
            dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                            Rights,
                                                            ControlType));

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);

        }

        // Removes an ACL entry on the specified directory for the specified account.
        public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new DirectoryInfo(FileName);

            // Get a DirectorySecurity object that represents the
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.
            dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
                                                            Rights,
                                                            ControlType));

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);

        }
    }
}

Set-PythonEnv: C:\Users\Administrator\Documents\PowerShell\Microsoft.PowerShell_profile.ps1:5 Line | 5 | Set-PythonEnv -EnvName global | ~~~~~~~~~~~~~ | The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. | Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Set-PythonEnv: C:\Users\Administrator\Documents\PowerShell\Microsoft.PowerShell_profile.ps1:5 Line | 5 | Set-PythonEnv -EnvName global | ~~~~~~~~~~~~~ | The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. | Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> cd E:\PyTorch_Build\pytorch PS E:\PyTorch_Build\pytorch> python -m venv rtx5070_env Error: [Errno 13] Permission denied: 'E:\\PyTorch_Build\\pytorch\\rtx5070_env\\Scripts\\python.exe' PS E:\PyTorch_Build\pytorch> .\rtx5070_env\Scripts\activate (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 1. 修复依赖问题 (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\fix_pytorch_deps.ps1 Set-PythonEnv: E:\PyTorch_Build\pytorch\fix_pytorch_deps.ps1:2 Line | 2 | Set-PythonEnv -EnvName pytorch | ~~~~~~~~~~~~~ | The term 'Set-PythonEnv' is not recognized as a name of a cmdlet, function, script file, or executable program. | Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting typing-extensions Downloading https://pypi.tuna.tsinghua.edu.cn/packages/18/67/36e9267722cc04a6b9f15c7f3441c2363321a3ea07da7ae0c0707beb2a9c/typing_extensions-4.15.0-py3-none-any.whl (44 kB) Collecting ninja Downloading https://pypi.tuna.tsinghua.edu.cn/packages/29/45/c0adfbfb0b5895aa18cec400c535b4f7ff3e52536e0403602fc1a23f7de9/ninja-1.13.0-py3-none-win_amd64.whl (309 kB) Installing collected packages: typing-extensions, ninja Attempting uninstall: typing-extensions Found existing installation: typing_extensions 4.15.0 Uninstalling typing_extensions-4.15.0: Successfully uninstalled typing_extensions-4.15.0 Attempting uninstall: ninja Found existing installation: ninja 1.13.0 Uninstalling ninja-1.13.0: Successfully uninstalled ninja-1.13.0 Successfully installed ninja-1.13.0 typing-extensions-4.15.0 Using pip 25.2 from E:\PyTorch_Build\pytorch\rtx5070_env\lib\site-packages\pip (python 3.10) Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Obtaining file:///E:/PyTorch_Build/pytorch Running command Checking if build backend supports build_editable Checking if build backend supports build_editable ... done Running command Preparing editable metadata (pyproject.toml) Building wheel torch-2.9.0a0+git2d31c3d E:\PyTorch_Build\pytorch\rtx5070_env\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:82: SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated !! ******************************************************************************** Please use a simple string containing a SPDX expression for `project.license`. You can also use `project.license-files`. (Both options available on setuptools>=77.0.0). By 2026-Feb-18, you need to update your project and remove deprecated calls or your builds will no longer be supported. See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details. ******************************************************************************** !! corresp(dist, value, root_dir) running dist_info creating C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch.egg-info writing C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch.egg-info\PKG-INFO writing dependency_links to C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch.egg-info\dependency_links.txt writing entry points to C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch.egg-info\entry_points.txt writing requirements to C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch.egg-info\requires.txt writing top-level names to C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch.egg-info\top_level.txt writing manifest file 'C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch.egg-info\SOURCES.txt' reading manifest file 'C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching 'BUILD' warning: no files found matching '*.BUILD' warning: no files found matching 'BUCK' warning: no files found matching '[Mm]akefile.*' warning: no files found matching '*.[Dd]ockerfile' warning: no files found matching '[Dd]ockerfile.*' warning: no previously-included files matching '*.o' found anywhere in distribution warning: no previously-included files matching '*.obj' found anywhere in distribution warning: no previously-included files matching '*.so' found anywhere in distribution warning: no previously-included files matching '*.a' found anywhere in distribution warning: no previously-included files matching '*.dylib' found anywhere in distribution no previously-included directories found matching '*\.git' warning: no previously-included files matching '*~' found anywhere in distribution warning: no previously-included files matching '*.swp' found anywhere in distribution adding license file 'LICENSE' adding license file 'NOTICE' writing manifest file 'C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch.egg-info\SOURCES.txt' creating 'C:\Users\Administrator\AppData\Local\Temp\pip-modern-metadata-lws7t0u7\torch-2.9.0a0+git2d31c3d.dist-info' Preparing editable metadata (pyproject.toml) ... done Collecting filelock (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/42/14/42b2651a2f46b022ccd948bca9f2d5af0fd8929c4eec235b8d6d844fbe67/filelock-3.19.1-py3-none-any.whl (15 kB) Requirement already satisfied: typing-extensions>=4.10.0 in e:\pytorch_build\pytorch\rtx5070_env\lib\site-packages (from torch==2.9.0a0+git2d31c3d) (4.15.0) Collecting sympy>=1.13.3 (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/a2/09/77d55d46fd61b4a135c444fc97158ef34a095e5681d0a6c10b75bf356191/sympy-1.14.0-py3-none-any.whl (6.3 MB) Link requires a different Python (3.10.10 not in: '>=3.11'): https://pypi.tuna.tsinghua.edu.cn/packages/eb/8d/776adee7bbf76365fdd7f2552710282c79a4ead5d2a46408c9043a2b70ba/networkx-3.5-py3-none-any.whl (from https://pypi.tuna.tsinghua.edu.cn/simple/networkx/) (requires-python:>=3.11) Link requires a different Python (3.10.10 not in: '>=3.11'): https://pypi.tuna.tsinghua.edu.cn/packages/6c/4f/ccdb8ad3a38e583f214547fd2f7ff1fc160c43a75af88e6aec213404b96a/networkx-3.5.tar.gz (from https://pypi.tuna.tsinghua.edu.cn/simple/networkx/) (requires-python:>=3.11) Link requires a different Python (3.10.10 not in: '>=3.11'): https://pypi.tuna.tsinghua.edu.cn/packages/3f/a1/46c1b6e202e3109d2a035b21a7e5534c5bb233ee30752d7f16a0bd4c3989/networkx-3.5rc0-py3-none-any.whl (from https://pypi.tuna.tsinghua.edu.cn/simple/networkx/) (requires-python:>=3.11) Link requires a different Python (3.10.10 not in: '>=3.11'): https://pypi.tuna.tsinghua.edu.cn/packages/90/7e/0319606a20ced20730806b9f7fe91d8a92f7da63d76a5c388f87d3f7d294/networkx-3.5rc0.tar.gz (from https://pypi.tuna.tsinghua.edu.cn/simple/networkx/) (requires-python:>=3.11) Collecting networkx>=2.5.1 (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/b9/54/dd730b32ea14ea797530a4479b2ed46a6fb250f682a9cfb997e968bf0261/networkx-3.4.2-py3-none-any.whl (1.7 MB) Collecting jinja2 (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/62/a1/3d680cbfd5f4b8f15abc1d571870c5fc3e594bb582bc3b64ea099db13e56/jinja2-3.1.6-py3-none-any.whl (134 kB) Collecting fsspec>=0.8.5 (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/47/71/70db47e4f6ce3e5c37a607355f80da8860a33226be640226ac52cb05ef2e/fsspec-2025.9.0-py3-none-any.whl (199 kB) Collecting mpmath<1.4,>=1.1.0 (from sympy>=1.13.3->torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/43/e3/7d92a15f894aa0c9c4b49b8ee9ac9850d6e63b03c9c32c0367a13ae62209/mpmath-1.3.0-py3-none-any.whl (536 kB) Collecting MarkupSafe>=2.0 (from jinja2->torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/44/06/e7175d06dd6e9172d4a69a72592cb3f7a996a9c396eee29082826449bbc3/MarkupSafe-3.0.2-cp310-cp310-win_amd64.whl (15 kB) Building wheels for collected packages: torch Running command Building editable for torch (pyproject.toml) Building wheel torch-2.9.0a0+git2d31c3d -- Building version 2.9.0a0+git2d31c3d Traceback (most recent call last): File "<string>", line 1024, in check_pydep File "E:\Python310\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1050, in _gcd_import File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked ModuleNotFoundError: No module named 'yaml' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "E:\PyTorch_Build\pytorch\rtx5070_env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 389, in <module> main() File "E:\PyTorch_Build\pytorch\rtx5070_env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) File "E:\PyTorch_Build\pytorch\rtx5070_env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 303, in build_editable return hook(wheel_directory, config_settings, metadata_directory) File "E:\PyTorch_Build\pytorch\rtx5070_env\lib\site-packages\setuptools\build_meta.py", line 468, in build_editable return self._build_with_temp_dir( File "E:\PyTorch_Build\pytorch\rtx5070_env\lib\site-packages\setuptools\build_meta.py", line 404, in _build_with_temp_dir self.run_setup() File "E:\PyTorch_Build\pytorch\rtx5070_env\lib\site-packages\setuptools\build_meta.py", line 317, in run_setup exec(code, locals()) File "<string>", line 1750, in <module> File "<string>", line 1628, in main File "<string>", line 971, in build_deps File "<string>", line 1026, in check_pydep RuntimeError: Missing build dependency: Unable to `import yaml`. Please install it via `conda install pyyaml` or `pip install pyyaml` error: subprocess-exited-with-error × Building editable for torch (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. full command: 'E:\PyTorch_Build\pytorch\rtx5070_env\Scripts\python.exe' 'E:\PyTorch_Build\pytorch\rtx5070_env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py' build_editable 'C:\Users\ADMINI~1\AppData\Local\Temp\tmp54c6camf' cwd: E:\PyTorch_Build\pytorch Building editable for torch (pyproject.toml) ... error ERROR: Failed building editable for torch Failed to build torch error: failed-wheel-build-for-install × Failed to build installable wheels for some pyproject.toml based projects ╰─> torch (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 2. 验证环境状态 (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\venv_validator.ps1 ❌ 错误的环境路径: E:\PyTorch_Build\pytorch\rtx5070_env False (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 如果第2步失败,执行系统级修复 (rtx5070_env) PS E:\PyTorch_Build\pytorch> .\system_env_fixer.ps1 Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: pip in e:\pytorchbuild_secure\rtx5070_env\lib\site-packages (22.3.1) Collecting pip Using cached https://pypi.tuna.tsinghua.edu.cn/packages/b7/3f/945ef7ab14dc4f9d7f40288d2df998d1837ee0888ec3659c813487572faa/pip-25.2-py3-none-any.whl (1.8 MB) Requirement already satisfied: setuptools in e:\pytorchbuild_secure\rtx5070_env\lib\site-packages (65.5.0) Collecting setuptools Using cached https://pypi.tuna.tsinghua.edu.cn/packages/a3/dc/17031897dae0efacfea57dfd3a82fdd2a2aeb58e0ff71b77b87e44edc772/setuptools-80.9.0-py3-none-any.whl (1.2 MB) Collecting wheel Using cached https://pypi.tuna.tsinghua.edu.cn/packages/0b/2c/87f3254fd8ffd29e4c02732eee68a83a1d3c346ae39bc6822dcbcb697f2b/wheel-0.45.1-py3-none-any.whl (72 kB) Installing collected packages: wheel, setuptools, pip Attempting uninstall: setuptools Found existing installation: setuptools 65.5.0 Uninstalling setuptools-65.5.0: Successfully uninstalled setuptools-65.5.0 Attempting uninstall: pip Found existing installation: pip 22.3.1 Uninstalling pip-22.3.1: Successfully uninstalled pip-22.3.1 Successfully installed pip-25.2 setuptools-80.9.0 wheel-0.45.1 Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting typing-extensions Using cached https://pypi.tuna.tsinghua.edu.cn/packages/18/67/36e9267722cc04a6b9f15c7f3441c2363321a3ea07da7ae0c0707beb2a9c/typing_extensions-4.15.0-py3-none-any.whl (44 kB) Collecting ninja Using cached https://pypi.tuna.tsinghua.edu.cn/packages/29/45/c0adfbfb0b5895aa18cec400c535b4f7ff3e52536e0403602fc1a23f7de9/ninja-1.13.0-py3-none-win_amd64.whl (309 kB) Installing collected packages: typing-extensions, ninja Successfully installed ninja-1.13.0 typing-extensions-4.15.0 Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Obtaining file:///E:/PyTorch_Build/pytorch Checking if build backend supports build_editable ... done Preparing editable metadata (pyproject.toml) ... done Collecting filelock (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/42/14/42b2651a2f46b022ccd948bca9f2d5af0fd8929c4eec235b8d6d844fbe67/filelock-3.19.1-py3-none-any.whl (15 kB) Requirement already satisfied: typing-extensions>=4.10.0 in e:\pytorchbuild_secure\rtx5070_env\lib\site-packages (from torch==2.9.0a0+git2d31c3d) (4.15.0) Collecting sympy>=1.13.3 (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/a2/09/77d55d46fd61b4a135c444fc97158ef34a095e5681d0a6c10b75bf356191/sympy-1.14.0-py3-none-any.whl (6.3 MB) Collecting networkx>=2.5.1 (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/b9/54/dd730b32ea14ea797530a4479b2ed46a6fb250f682a9cfb997e968bf0261/networkx-3.4.2-py3-none-any.whl (1.7 MB) Collecting jinja2 (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/62/a1/3d680cbfd5f4b8f15abc1d571870c5fc3e594bb582bc3b64ea099db13e56/jinja2-3.1.6-py3-none-any.whl (134 kB) Collecting fsspec>=0.8.5 (from torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/47/71/70db47e4f6ce3e5c37a607355f80da8860a33226be640226ac52cb05ef2e/fsspec-2025.9.0-py3-none-any.whl (199 kB) Collecting mpmath<1.4,>=1.1.0 (from sympy>=1.13.3->torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/43/e3/7d92a15f894aa0c9c4b49b8ee9ac9850d6e63b03c9c32c0367a13ae62209/mpmath-1.3.0-py3-none-any.whl (536 kB) Collecting MarkupSafe>=2.0 (from jinja2->torch==2.9.0a0+git2d31c3d) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/44/06/e7175d06dd6e9172d4a69a72592cb3f7a996a9c396eee29082826449bbc3/MarkupSafe-3.0.2-cp310-cp310-win_amd64.whl (15 kB) Building wheels for collected packages: torch Building editable for torch (pyproject.toml) ... error error: subprocess-exited-with-error × Building editable for torch (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [32 lines of output] Building wheel torch-2.9.0a0+git2d31c3d -- Building version 2.9.0a0+git2d31c3d Traceback (most recent call last): File "<string>", line 1024, in check_pydep File "E:\Python310\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1050, in _gcd_import File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked ModuleNotFoundError: No module named 'yaml' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "E:\PyTorchBuild_Secure\rtx5070_env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 389, in <module> main() File "E:\PyTorchBuild_Secure\rtx5070_env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) File "E:\PyTorchBuild_Secure\rtx5070_env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 303, in build_editable return hook(wheel_directory, config_settings, metadata_directory) File "E:\PyTorchBuild_Secure\rtx5070_env\lib\site-packages\setuptools\build_meta.py", line 468, in build_editable return self._build_with_temp_dir( File "E:\PyTorchBuild_Secure\rtx5070_env\lib\site-packages\setuptools\build_meta.py", line 404, in _build_with_temp_dir self.run_setup() File "E:\PyTorchBuild_Secure\rtx5070_env\lib\site-packages\setuptools\build_meta.py", line 317, in run_setup exec(code, locals()) File "<string>", line 1750, in <module> File "<string>", line 1628, in main File "<string>", line 971, in build_deps File "<string>", line 1026, in check_pydep RuntimeError: Missing build dependency: Unable to `import yaml`. Please install it via `conda install pyyaml` or `pip install pyyaml` [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building editable for torch Failed to build torch error: failed-wheel-build-for-install × Failed to build installable wheels for some pyproject.toml based projects ╰─> torch Test-VirtualEnv: E:\PyTorch_Build\pytorch\system_env_fixer.ps1:30 Line | 30 | Test-VirtualEnv | ~~~~~~~~~~~~~~~ | The term 'Test-VirtualEnv' is not recognized as a name of a cmdlet, function, script file, or executable | program. Check the spelling of the name, or if a path was included, verify that the path is correct and try | again. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 最终测试 (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import torch; print(f'CUDA可用: {torch.cuda.is_available()}')" Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\PyTorch_Build\pytorch\torch\__init__.py", line 1001, in <module> raise ImportError( ImportError: Failed to load PyTorch C extensions: It appears that PyTorch has loaded the `torch/_C` folder of the PyTorch repository rather than the C extensions which are expected in the `torch._C` namespace. This can occur when using the `install` workflow. e.g. $ python -m pip install --no-build-isolation -v . && python -c "import torch" This error can generally be solved using the `develop` workflow $ python -m pip install --no-build-isolation -v -e . && python -c "import torch" # This should succeed or by running Python from a different directory. (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import torch; x = torch.rand(5, device='cuda'); print(x)" Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\PyTorch_Build\pytorch\torch\__init__.py", line 1001, in <module> raise ImportError( ImportError: Failed to load PyTorch C extensions: It appears that PyTorch has loaded the `torch/_C` folder of the PyTorch repository rather than the C extensions which are expected in the `torch._C` namespace. This can occur when using the `install` workflow. e.g. $ python -m pip install --no-build-isolation -v . && python -c "import torch" This error can generally be solved using the `develop` workflow $ python -m pip install --no-build-isolation -v -e . && python -c "import torch" # This should succeed or by running Python from a different directory. (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 检查Python环境 (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import sys; print(f'\nPython路径: {sys.prefix}\n模块搜索路径:')" Python路径: E:\PyTorchBuild_Secure\rtx5070_env 模块搜索路径: (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import sys; print('\n'.join(sys.path))" E:\Python310\python310.zip E:\Python310\DLLs E:\Python310\lib E:\Python310 E:\PyTorchBuild_Secure\rtx5070_env E:\PyTorchBuild_Secure\rtx5070_env\lib\site-packages (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 检查PyTorch构建 (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import torch; print(f'\nCUDA可用: {torch.cuda.is_available()}')" Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\PyTorch_Build\pytorch\torch\__init__.py", line 1001, in <module> raise ImportError( ImportError: Failed to load PyTorch C extensions: It appears that PyTorch has loaded the `torch/_C` folder of the PyTorch repository rather than the C extensions which are expected in the `torch._C` namespace. This can occur when using the `install` workflow. e.g. $ python -m pip install --no-build-isolation -v . && python -c "import torch" This error can generally be solved using the `develop` workflow $ python -m pip install --no-build-isolation -v -e . && python -c "import torch" # This should succeed or by running Python from a different directory. (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "from torch.utils.cpp_extension import CUDA_HOME; print(f'\nCUDA_HOME: {CUDA_HOME}')" Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\PyTorch_Build\pytorch\torch\__init__.py", line 1001, in <module> raise ImportError( ImportError: Failed to load PyTorch C extensions: It appears that PyTorch has loaded the `torch/_C` folder of the PyTorch repository rather than the C extensions which are expected in the `torch._C` namespace. This can occur when using the `install` workflow. e.g. $ python -m pip install --no-build-isolation -v . && python -c "import torch" This error can generally be solved using the `develop` workflow $ python -m pip install --no-build-isolation -v -e . && python -c "import torch" # This should succeed or by running Python from a different directory. (rtx5070_env) PS E:\PyTorch_Build\pytorch> (rtx5070_env) PS E:\PyTorch_Build\pytorch> # 检查关键模块 (rtx5070_env) PS E:\PyTorch_Build\pytorch> python -c "import typing_extensions; print('\ntyping_extensions版本:', typing_extensions.__version__)" Traceback (most recent call last): File "<string>", line 1, in <module> AttributeError: module 'typing_extensions' has no attribute '__version__' (rtx5070_env) PS E:\PyTorch_Build\pytorch>
09-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值