Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在PowerShell中使用here字符串正确写入pip.ini和.condarc文件?_Python_Powershell_Encoding_Pip - Fatal编程技术网

Python 如何在PowerShell中使用here字符串正确写入pip.ini和.condarc文件?

Python 如何在PowerShell中使用here字符串正确写入pip.ini和.condarc文件?,python,powershell,encoding,pip,Python,Powershell,Encoding,Pip,免责声明:我越来越习惯PowerShell,但我对PowerShell缺乏经验 我想使用PowerShell和here字符串语法来编写pip.ini和.condarc配置文件,分别使用Python包管理器pip和conda。 对于.condarc,没有错误,但我认为我必须在记事本+中重新编写它才能真正工作-我认为这是一个文件编码问题: mkdir 'C:\ProgramData\conda\.condarc' echo @" show_channel_urls: true allow_

免责声明:我越来越习惯PowerShell,但我对PowerShell缺乏经验

我想使用PowerShell和here字符串语法来编写
pip.ini
.condarc
配置文件,分别使用Python包管理器pip和conda。 对于
.condarc
,没有错误,但我认为我必须在
记事本+
中重新编写它才能真正工作-我认为这是一个文件编码问题:

mkdir 'C:\ProgramData\conda\.condarc'
echo @"
show_channel_urls: true
allow_other_channels: false
report_errors: false
remote_read_timeout_secs: 120
"@ > C:\ProgramData\conda\.condarc
由于[global],下面给出了pip.ini的一个错误:

mkdir 'C:\ProgramData\pip\pip.ini'
echo @"
[global]
index = https://xxxx/nexus/repository/xxxx/pypi
index-url = https://xxxx:8443/nexus/repository/xxxx/simple
trusted-host = xxxx:8443
"@ > C:\ProgramData\pip\pip.ini
Get Content pip.ini
工作正常,但
pip config list-v
返回:

PS C:\Program Files> pip config list -v
Configuration file could not be loaded.
File contains no section headers.
file: 'C:\\ProgramData\\pip\\pip.ini', line: 1
'ÿþ[\x00g\x00l\x00o\x00b\x00a\x00l\x00]\x00\n'
备注:xxxx代表敏感的公司信息,因此将替换真实文本

我还试图用`,避开方括号,但没有成功


是否有一种方法可以指定某些文件编码,如上面的UTF-8,或者可以通过另一种自动化方式解决此问题?

Out file
Windows PowerShell 5.1中的默认值是PowerShell Core 7中的Unicode+它是UTF8

您正在使用
mkdir
创建一个对我来说毫无意义的文件

无论如何,不需要预先创建文件。因此,结合上述内容可能类似于:

@"
show_channel_urls: true
allow_other_channels: false
report_errors: false
remote_read_timeout_secs: 120
"@ | Out-File  C:\ProgramData\conda\.condarc

@"
[global]
index = https://xxxx/nexus/repository/xxxx/pypi
index-url = https://xxxx:8443/nexus/repository/xxxx/simple
trusted-host = xxxx:8443
"@ | Out-File C:\ProgramData\pip\pip.ini

Out文件
Windows PowerShell 5.1中的默认值是PowerShell Core 7中的Unicode+它是UTF8

您正在使用
mkdir
创建一个对我来说毫无意义的文件

无论如何,不需要预先创建文件。因此,结合上述内容可能类似于:

@"
show_channel_urls: true
allow_other_channels: false
report_errors: false
remote_read_timeout_secs: 120
"@ | Out-File  C:\ProgramData\conda\.condarc

@"
[global]
index = https://xxxx/nexus/repository/xxxx/pypi
index-url = https://xxxx:8443/nexus/repository/xxxx/simple
trusted-host = xxxx:8443
"@ | Out-File C:\ProgramData\pip\pip.ini
在Windows Powershell中,重定向操作符使用Unicode编码(换句话说,),这就是为什么您会看到如此奇怪的文件内容。或者

  • 从PowerShell Core(
    pwsh.exe
    )运行代码,或
  • 将cmdlet与其
    编码
    参数一起使用,而不是
    重定向程序
但是,请注意,Windows PowerShell倾向于使用以下代码段添加:

$MyRawString = @"
[global]
index = https://xxxx/nexus/repository/xxxx/pypi
index-url = https://xxxx:8443/nexus/repository/xxxx/simple
trusted-host = xxxx:8443
"@
$MyPath = "C:\ProgramData\pip\pip.ini"
$MyRawString | Out-file -FilePath $MyPath -Encoding utf8
针对Windows PowerShell的解决方案。使用
$False
并将其传递给构造函数似乎有效(从以下位置窃取):

解释
获取有关重定向的帮助

  • Windows PowerShell
    PowerShell.exe
写入文件时,重定向操作符使用Unicode 编码。如果文件具有不同的编码,则输出可能不会 格式必须正确。要将内容重定向到非Unicode文件,请使用 使用其
编码
参数的cmdlet

  • PowerShell核心
    pwsh.exe
    ,版本6+)
写入文件时,重定向操作符使用
UTF8NoBOM
编码。如果文件具有不同的编码,则输出 可能格式不正确。以不同的方式写入文件 编码,将cmdlet与其
编码一起使用
参数

请注意,在Windows Powershell的for Powershell 5.1…

中有一个错误,重定向操作符使用
Unicode
编码(换句话说,),这就是为什么您会看到如此奇怪的文件内容。或者

  • 从PowerShell Core(
    pwsh.exe
    )运行代码,或
  • 将cmdlet与其
    编码
    参数一起使用,而不是
    重定向程序
但是,请注意,Windows PowerShell倾向于使用以下代码段添加:

$MyRawString = @"
[global]
index = https://xxxx/nexus/repository/xxxx/pypi
index-url = https://xxxx:8443/nexus/repository/xxxx/simple
trusted-host = xxxx:8443
"@
$MyPath = "C:\ProgramData\pip\pip.ini"
$MyRawString | Out-file -FilePath $MyPath -Encoding utf8
针对Windows PowerShell的解决方案。使用
$False
并将其传递给构造函数似乎有效(从以下位置窃取):

解释
获取有关重定向的帮助

  • Windows PowerShell
    PowerShell.exe
写入文件时,重定向操作符使用Unicode 编码。如果文件具有不同的编码,则输出可能不会 格式必须正确。要将内容重定向到非Unicode文件,请使用 使用其
编码
参数的cmdlet

  • PowerShell核心
    pwsh.exe
    ,版本6+)
写入文件时,重定向操作符使用
UTF8NoBOM
编码。如果文件具有不同的编码,则输出 可能格式不正确。以不同的方式写入文件 编码,将cmdlet与其
编码一起使用
参数


请注意,在for PowerShell 5.1中有一个错误。

echo
写入输出的别名。
…我的错误将修复…@Steven没有真正解决我的问题,而JosefZ的答案确实解决了这个问题。关于mkdir:关于控制台,您是对的,但现在当我将命令集成到PowerShell脚本中时,我似乎需要它。
echo
写入输出的别名。
我的错误将修复…@Steven没有真正解决我的问题,而JosefZ的答案确实如此。关于mkdir:关于控制台,您是对的,但现在当我将命令集成到PowerShell脚本中时,我似乎需要它。这有帮助,但不需要。我确实需要去掉字节顺序标记,而且我也不确定global末尾引入的“\n”,第一行现在被解释为“ï”?[global]\n'@mgross答案已更新
\n
是一个新行的名字。谢谢,这非常有效:
pip--version
现在可以与您的解决方案一起使用。我会很快接受你的回答,但为了正确起见,我会等上一天或一天,以防有更好的答案出现(我不这么认为,但人们永远不知道),这会有所帮助,但我确实需要得到r