程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Frequently Asked Questions about using Python on Windows

編輯:Python

使用 pip install Fix package installation issues

There are many reasons why the installation fails - 在很多情況下,The correct solution is to contact the package developer.

A common cause of problems is trying to install a package to a location you don't have permission to modify. 例如,The default installation location may require administrative privileges,但是默認情況下,Python 沒有管理權限. The best solution is to create one虛擬環境and install it there.

Some packages include native code,需要 C 或 C++ The compiler can be installed. 一般來說,Package developers should publish precompiled versions,But usually not released. 如果Installed for Visual Studio 的生成工具並選擇了 C++ 選項,then some packages may work fine,但是在大多數情況下,Need to contact the package developer.

Please follow about StackOverflow 的討論

使用 WSL 解決 pip 安裝問題

在適用於 Linux 的 Windows 子系統(WSL 或 WSL2)上使用 pip 安裝 Flask 等包時,例如使用 python3 -m pip install flask,You may specifically encounter errors like the following:

Bash復制

WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))
after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection
object at 0x7f655471da30>: Failed to establish a new connection: [Errno -3]
Temporary failure in name resolution')': /simple/flask/

While investigating this issue,You may encounter several pitfalls,None of them passed WSL Linux Distros resolve it particularly efficiently. (警告:在 WSL 上,Do not attempt to edit resolv.conf,The file is a symbolic link,Modifying it can cause tons of problems). Unless you are running a secondary market firewall,Otherwise just reinstall pip 可能就能解決問題:

Bash復制

sudo apt -y purge python3-pip
sudo python3 -m pip uninstall pip
sudo apt -y install python3-pip --fix-missing

**

什麼是 py.exe?

Since there are different types to deal with Python 項目,So you may end up with multiple versions installed on your computer Python. Since all these versions are used python 命令,So which version are you using Python 可能並不明顯. 作為標准,建議使用 python3 命令(或 python3.7 to select a specific version).

py.exe 啟動器The latest version installed will be automatically selected Python. 此外,還可以使用 py -3.7 command to select a specific version,或者使用 py --list to see the available versions. 但是,Only when using from python.org 安裝的 Python 版本時,py.exe The launcher will work normally.從 Microsoft Store 安裝 Python 時,不包含 命令. 對於 Linux、macOS、WSL 和 Microsoft Store 版本的 Python,應使用 python3(或 python3.7)命令.

為什麼運行 python.exe 會打開 Microsoft Store?

To help new users find the right one Python 安裝,我們向 Windows Added a shortcut,can go directly to Microsoft Store The latest version of the community pack released in . The package can be easily installed without admin rights,並將默認的 python 和 python3 command replaces the corresponding real command.

Running the shortcut executable with any command line arguments will return an error code,指示未安裝 Python. This is to prevent batch files and scripts from accidentally opening Store 應用.

如果使用 python.org Installer in Python 並選擇“添加到 PATH”選項,則新的 Commands will take precedence over shortcuts. 請注意,Other installers may be added at a lower priority than built-in shortcuts python.

通過從“開始”打開“管理應用執行別名”,找到“應用安裝程序”Python entry and switch it to “關閉”,無需安裝 Python Shortcuts are disabled.

when i copy paste the file path,為什麼在 Python 中不起作用?

Python Strings are used for special characters“轉義符”. 例如,To insert a newline in the string,應鍵入 \n. 由於 Windows Use backslashes on file paths,So some parts may have been converted to special characters.

To paste the path as Python 中的字符串,請添加 r 前綴. 這表示它是一個 raw 字符串,除 \” 外,No escape characters will be used(It may be necessary to remove the last backslash in the path). 因此,路徑可能如下所示:r"C:\Users\MyName\Documents\Document.txt"

在 Python 中使用路徑時,建議使用標准 pathlib 模塊. This way you can convert the string to rich Path 對象,Whether it uses forward slashes or backslashes,path operations can be performed consistently,This makes the code work better on different operating systems.

什麼是 PYTHONPATH?

Python 使用 PYTHONPATH environment variables to specify a list of directories from which modules can be imported. 運行時,可以檢查 sys.path variable to see the directories that will be searched when importing something.

要在“命令提示符”中設置此變量,請使用:set PYTHONPATH=list;of;paths.

要在 PowerShell 中設置此變量,請在啟動 Python 之前使用:$env:PYTHONPATH=’list;of;paths’.

不建議通過“環境變量”Set this variable globally,Because it could be any version using it Python,rather than the version to use.

Where to find help with packaging and deployment?

Docker:VSCode 擴展Helps with fast packaging and deployment Dockerfile 和 docker-compose.yml 模板(Generate the correct one for the project Docker 文件).

借助 Azure Kubernetes 服務 (AKS),Containerized applications can be deployed and managed while scaling resources on demand.

如果需要在不同的計算機上工作,該怎麼辦?

通過設置同步,可以使用 GitHub Sync between different installations VS Code 設置. If working on a different computer,This helps maintain a consistent environment between them.

If I'm used to using it PyCharm、Atom、Sublime Text、Emacs 或 Vim,該怎麼辦?

VSCode 擴展鍵映射Helps create a familiar environment for you.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved