forked from MicrosoftDocs/windows-dev-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaqs.yml
109 lines (80 loc) · 8.78 KB
/
faqs.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
### YamlMime:FAQ
metadata:
title: "FAQ's about using Python on Windows"
description: "Get help by reviewing answers to frequently asked questions (FAQs) about using Python on Windows for development."
ms.topic: article
keywords: python, windows 10, microsoft, pip, py.exe, file paths, PYTHONPATH, python deployment, python packaging
ms.localizationpriority: medium
ms.date: 07/19/2019
title: Frequently Asked Questions about using Python on Windows
summary: |
sections:
- name: Ignored
questions:
- question: |
Trouble installing a package with pip install
answer: |
There are a number of reasons why an installation will fail--in many cases the right solution is to contact the package developer.
A common cause for trouble is trying to install into a location that you do not have permission to modify. For example, the default install location might require Administrative privileges, but by default Python will not have them. The best solution is to create a [virtual environment](./web-frameworks.md#create-a-virtual-environment) and install there.
Some packages include native code that requires a C or C++ compiler to install. In general, package developers should publish pre-compiled versions, but often do not. Some of these packages might work if you [install Build Tools for Visual Studio](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019) and select the C++ option, however in most cases you will need to contact the package developer.
[Follow the discussion on StackOverflow](https://stackoverflow.com/questions/4750806/how-do-i-install-pip-on-windows/12476379)
- question: |
Trouble installing pip with WSL
answer: |
When installing a package (like Flask) with pip on Windows Subsystem for Linux (WSL or WSL2), for example `python3 -m pip install flask`, you may specifically encounter an error like this:
```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/
```
When researching this problem you may be led down several rabbit holes, none of which are particularly productive with a WSL linux distribution. (Warning: on WSL do not try editing `resolv.conf`, that file is a symbolic link and modifying it is a can of worms). Unless you are running an aftermarket firewall, the likely solution is to simply re-install pip:
```bash
sudo apt -y purge python3-pip
sudo python3 -m pip uninstall pip
sudo apt -y install python3-pip --fix-missing
```
**Further discussion in the [WSL product repo on GitHub](https://github.com/microsoft/WSL/issues/4020). Thanks to our user community for [contributing this issue](https://github.com/MicrosoftDocs/windows-uwp/issues/2679) to the docs.*
- question: |
What is py.exe?
answer: |
You may end up with multiple versions of Python installed on your machine because you are working on different types of Python projects. Because these all use the `python` command, it may not be obvious which version of Python you are using. As a standard, it is recommended to use the `python3` command (or `python3.7` to select a specific version).
The [py.exe launcher](https://docs.python.org/3/using/windows.html#launcher) will automatically select the most recent version of Python you've installed. You can also use commands like `py -3.7` to select a particular version, or `py --list` to see which versions can be used. **HOWEVER**, the py.exe launcher will only work if you are using a version of Python installed from [python.org](https://www.python.org/downloads/windows/). When you install Python from the Microsoft Store, the `py` command is **not included**. For Linux, macOS, WSL and the Microsoft Store version of Python, you should use the `python3` (or `python3.7`) command.
- question: |
Why does running python.exe open the Microsoft Store?
answer: |
To help new users find a good installation of Python, we added a shortcut to Windows that will take you directly to the latest version of the community's package published in the Microsoft Store. This package can be installed easily, without administrator permissions, and will replace the default `python` and `python3` commands with the real ones.
Running the shortcut executable with any command-line arguments will return an error code to indicate that Python was not installed. This is to prevent batch files and scripts from opening the Store app when it was probably not intended.
If you install Python using the installers from [python.org](https://www.python.org/downloads/windows/) and select the "add to PATH" option, the new `python` command will take priority over the shortcut. Note that other installers may add `python` at a _lower_ priority than the built-in shortcut.
You can disable the shortcuts without installing Python by opening "Manage app execution aliases" from Start, finding the "App Installer" Python entries and switching them to "Off".
- question: |
Why don’t file paths work in Python when I copy-paste them?
answer: |
Python strings use “escapes” for special characters. For example, to insert a new line character into a string, you would type `\n`. Because file paths on Windows use backslashes, some parts might be being converted into special characters.
To paste a path as a string in Python, add the `r` prefix. This indicates that it is a `raw` string, and no escape characters will be used except for \” (you might need to remove the last backslash in your path). So your path might look like:
`r"C:\Users\MyName\Documents\Document.txt"`
When working with paths in Python, we recommend using the standard pathlib module. This will let you convert the string to a rich Path object that can do path manipulations consistently whether it uses forward slashes or backslashes, making your code work better across different operating systems.
- question: |
What is PYTHONPATH?
answer: |
The PYTHONPATH environment variable is used by Python to specify a list of directories that modules can be imported from. When running, you can inspect the `sys.path` variable to see which directories will be searched when you import something.
To set this variable from the Command Prompt, use: `set PYTHONPATH=list;of;paths`.
To set this variable from PowerShell, use: `$env:PYTHONPATH=’list;of;paths’` just before you launch Python.
Setting this variable globally through the **Environment Variables** settings is **not** recommended, as it may be used by any version of Python instead of the one that you intend to use.
- question: |
Where can I find help with packaging and deployment?
answer: |
[Docker](https://code.visualstudio.com/docs/azure/docker): [VSCode extension](https://code.visualstudio.com/docs/azure/docker) helps you quickly package and deploy with Dockerfile and docker-compose.yml templates (generate the proper Docker files for your project).
[Azure Kubernetes Service (AKS)](/azure/aks/) enables you to deploy and manage containerized applications while scaling resources on demand.
- question: |
What if I need to work across different machines?
answer: |
[Settings Sync](https://marketplace.visualstudio.com/items?itemName=Shan.code-settings-sync) allows you to synchronize your VS Code settings across different installations using GitHub. If you work on different machines, this helps keep your environment consistent across them.
- question: |
What if I'm used to using PyCharm, Atom, Sublime Text, Emacs, or Vim?
answer: |
The VSCode extension [Keymaps](https://marketplace.visualstudio.com/search?target=VSCode&category=Keymaps&sortBy=Downloads) can help your environment feel right at home.
- question: |
How do Mac shortcut keys map to Windows shortcut keys?
answer: |
Some of the keyboard buttons and system shortcuts are slightly different between a Windows machine and a Macintosh. This [Mac to Windows transition guide](../dev-environment/mac-to-windows.md) covers the basics.