Skip to content

Add guide for working with Python on Windows #773

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

Open
wants to merge 44 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c0b16b2
Add initial guide for errors with Python on Windows
wookie184 Sep 25, 2022
2781ed9
Split up into multiple pages and add more content
wookie184 Oct 21, 2022
65a3ee2
Wrap lines and make some more changes
wookie184 Oct 21, 2022
f072093
Made some changes after proof-reading
wookie184 Oct 21, 2022
6a609a3
Move guides to a subdirectory
wookie184 Nov 2, 2022
2972b42
Edit creating-python-environment-windows.md to link to the page on in…
wookie184 Nov 2, 2022
aa1cbc4
Minor improvements to wording and layout
wookie184 Nov 2, 2022
efa1ef3
Rewrote and added more info to python on ms store article
discretegames Nov 5, 2022
952c4a5
added right aligned drake image and a few tweaks to ms store guide
discretegames Nov 5, 2022
13779f3
final tweaks to ms store article
discretegames Nov 6, 2022
3668202
Redid windows guide titles/slugs/descriptions to be consistent with p…
discretegames Nov 6, 2022
19caa0a
Rewrote safe method for adding Python to path, in own article
discretegames Nov 6, 2022
40ad001
finished detailed, screenshot full, putting python on path guide
discretegames Nov 7, 2022
a7c59dd
Proofread, tweaked, added links to common windows issues guide. (Also…
discretegames Nov 7, 2022
4d734b0
tweaks and link fixes to windows guides, still in reorg process
discretegames Nov 7, 2022
f1961a0
reorg of windows guides, moved py launcher into installing and using,…
discretegames Nov 8, 2022
4bb5c57
added toc to windows guides
discretegames Nov 8, 2022
54a596e
Finished recommended Python Windows install instructions
discretegames Nov 8, 2022
3dc24ec
completed more install info section of Windows guide
discretegames Nov 8, 2022
c2fdf87
re-added venv section to windows guide
discretegames Nov 8, 2022
790d076
finished py launcher section, finishing Windows guide reorg
discretegames Nov 8, 2022
ee5e1ba
Added WSL2 section to windows guide; other tweaks
discretegames Nov 8, 2022
c7f37e9
Proofread windows guides and added image links everywhere
discretegames Nov 8, 2022
529cc5a
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 9, 2022
6d2db8c
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 9, 2022
c6147f9
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 9, 2022
b3406ec
emphasized NOT
discretegames Nov 9, 2022
0aca0e2
Merge branch 'add-windows-guide' of https://github.com/discretegames/…
discretegames Nov 9, 2022
885eacb
added shtlrs Windows guide suggestions
discretegames Nov 9, 2022
be31bfd
Note about admin needed, and added subheading to Path guide.
discretegames Nov 9, 2022
b8bc86c
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 13, 2022
26f36b2
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 13, 2022
e3975c9
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 13, 2022
0bb9447
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 13, 2022
f612d4c
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 13, 2022
236963d
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 13, 2022
9b21ec8
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 13, 2022
db505b4
Update pydis_site/apps/content/resources/guides/python-guides/windows…
discretegames Nov 13, 2022
d065f2e
Added some of wookies Windows guide review suggestions
discretegames Nov 13, 2022
3dc5756
Merge branch 'add-windows-guide' of https://github.com/discretegames/…
discretegames Nov 13, 2022
c3bb0b5
NOT back to Not and wrapped things to 120chars
discretegames Nov 13, 2022
d59465b
removed import workaround and mentioned 32-bit download in windows gu…
discretegames Nov 13, 2022
5641448
Merge pull request #794 from discretegames/add-windows-guide
wookie184 Nov 17, 2022
0e1a5e2
Typo fixes
wookie184 Nov 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: Python on Windows
description: Guides related to installing, managing, and troubleshooting Python on Windows operating systems.
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
---
title: Common Issues Using Python on Windows
description: A list of common issues with Python on Windows and how to fix them
icon: fab fa-windows
toc: 3
---

### When I run `python` in the terminal I get no result, or the Microsoft Store opens!

By default Windows has an alias for `python` in the terminal to guide you to install it from the [Microsoft
Store](https://apps.microsoft.com/store/search/python). We don't recommend installing Python from the Microsoft
Store, see why [here](../microsoft-store). To disable this alias, search "App execution aliases" in Windows search
and click on "Manage app execution aliases". In the list you should see two options with title "App Installer" and
descriptions `python.exe` and `python3.exe`. Disable both of them.

Note that if you are following instructions telling you to run a command starting with `python3`, those instructions are
intended for Unix/macOS systems. Try just using `python` instead.

If after doing this you have an issue with the Python command not being detected, see the question below.

### When I try and run my code with `python` in the terminal I get an error saying the command was not recognized

If you have not installed Python, you will need to do that. Follow our guide [here](../installing-and-using-python).

If you have installed python and are still having the issue, it is likely that you didn't check the "Add python.exe to
PATH" checkbox on the first page of the installer. See [our guide on adding Python to PATH](../putting-python-on-path)
for how to fix this. You can also use the `py` launcher instead of `python` by just replacing `python` in your command
with `py`. See [this guide](../installing-and-using-python/#the-py-launcher) for more information on that.
Comment on lines +8 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that using the py launcher should be the first recommendation. Managing the aliases isn't necessary if you stick with using it. I'd say change the section a little where using py is the default recommendation, and another header saying "If you still want to using python instead of py

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also address here or below how to install using py by doing py -m pip install [package_name_here].


### I `pip` installed a package but when running my code I get a `ModuleNotFoundError`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, the usage of py -m pip install can be both helpful and important. If there is an issue with the PATH or other factors, python and pip may point to different versions. Using py -m pip install guarantees that whatever you pip install will go into that py version.


- #### Are you actually getting a `ModuleNotFoundError`?

If you are using a code editor such as VSCode or PyCharm you may get a squiggly line under your import saying the
module couldn't be found, it is possible that this is just an mistake by the editor, so try to actually run your
code the way it is to ensure that it actually errors.

If your code runs fine, you could try restarting your editor. If you have newly installed a module it may just not
have detected it yet. If that doesn't help, you may need to configure your editor to ensure it is looking for the
module in the correct Python environment. See [our guide on virtual
environments](../installing-and-using-python/#virtual-environments) for more information.

- #### Was the install successful?

Look out for errors when installing the module you want. If you get an error, that's probably why the import isn't
working, so you should look into it. Often a Google search of the relevant error text will help.

- #### Did you use the correct module name?

Double check that you haven't made a typo in the name you are importing, or in what you installed from
[PyPI](https://pypi.org/), you need to make sure you type the name exactly as it should be.

Also, **the name you should `import` may not be the same as the name you `pip install`**. Check the module's
documentation or PyPI page if you are unsure. Examples of this are:

- [**opencv-python**](https://pypi.org/project/opencv-python/): You need to `pip install opencv-python`, but the
import has to be `import cv2`
- [**discord.py**](https://pypi.org/project/discord.py/): You need to `pip install discord.py`, but the import has
to be `import discord`
- [**python-dotenv**](https://pypi.org/project/python-dotenv/): You need to `pip install python-dotenv`, but the
import has to be `import dotenv`
- [**Pillow**](https://pypi.org/project/Pillow/): You need to `pip install Pillow`, but the import is
usually `from PIL import Image`.

Normal imports in Python can't have hyphens (`-`) in them as it would be interpreted as subtraction. So if the
package name has a `-` in it the import will definitely be different.

- #### Are you installing to the same environment you're running your code from?

This is a very common issue. When you install a module from PyPI, you will install it into a single Python
environment, the one that the `pip` you invoked is part of. This could be different to the one you are running the
script from if it's using a different Python installation, or a [virtual environment](../installing-and-using-python/#virtual-environments).

You can tell when you're in a virtual environment because the name of the venv folder will usually be shown in
parentheses at the start of terminal prompts. You can type `deactivate` to get out of it.
[![Venv terminal prompt.](/static/images/content/python-on-windows/venv_prompt.png)](/static/images/content/python-on-windows/venv_prompt.png)

##### I'm using PyCharm

> PyCharm creates a virtual environment for each project by default, so if you're installing a module using Pycharm
> you need to make sure you also run your code through PyCharm. This also applies the other way round, if you want
> to use a module in PyCharm you need to install it through PyCharm.
>
> You can see the exact path of the current Python interpreter, and thus virtual environment, in PyCharm by hovering
> your mouse over the interpreter info menu on the bottom right:
> [![PyCharm python.exe info.](/static/images/content/python-on-windows/pycharm_python_path.png)](/static/images/content/python-on-windows/pycharm_python_path.png)
>
> It's also possible to change PyCharm to use your system environment, see [their guide on configuring your
> interpreter](https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html) for more information.

##### I installed the module using pip from the command line

> Run `pip -V` and look at the path it returned, which should be in the form
> `<PATH_TO_PYTHON>\Lib\site-packages\pip`.
>
> Then put `import sys;print(sys.executable);sys.exit(0);` at the top (above imports!) of your python file, and run
> it. The result should be in the form `<PATH_TO_PYTHON>\python.exe`.
>
> Now compare that `PATH_TO_PYTHON` to the one from `pip -V`. If they're different, this is the cause of the
> `ModuleNotFoundError`. Follow the relevant "I'm running my code with ..." section below for how to fix this.

##### I'm running my code with `python` from the terminal.

> If you are using a virtual environment, ensure you have activated it before running `pip install`. You can test
> this by running `pip -V` and checking the path is the one of your virtual environment.
>
> Alternatively, you can use the `py` command to specify the python version you want to pip install into, e.g.
> `py -3.9 -m pip install numpy`, and to specify the executable you want to run your code with, e.g.
> `py -3.9 my_script.py`.
>
> If your `pip` and `python` commands are referring to different python environments at the same time, it's possible
> your PATH is configured incorrectly. See our [guide for adding Python to PATH](../putting-python-on-path) for
> how to fix this.

##### I'm running my code with the button in VSCode

> To run your code from the same environment you installed Python to, set the VSCode Python interpreter to the one
> at the path you found when running `pip -V` by following [this
> guide](https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment) (basically
> `Ctrl+Shift+P -> Python: Select Interpreter -> select the one desired`). The path should be in the form
> `PATH_TO_PYTHON\python.exe`
>
> Alternatively, you can pip install to the currently activated environment. If you have configured VSCode to use a
> virtual environment, it should automatically activate it when you open a new terminal, so running the
> `pip install` command in the VSCode terminal should work. If you are not using a virtual environment, you can use
> the `py` launcher to specify the installation you want to install to.

### I `pip` installed a command line program but it isn't recognized
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, py launcher can help rectify some of this, including running some even when \Scripts isn't in the path. For example if you py -m pip install black, then try to just do black it may not work. But if you do py -m black it should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works well for some tools, but not others. For example, with pyinstaller you have to run py -m PyInstaller with that exact capitalisation - something that I don't believe is clearly documented anywhere, the docs just say to use pyinstaller directly so it's confusing when that doesn't work. There are also other tools that don't have any -m alternative that i've been able to find, like https://csvkit.readthedocs.io/en/latest/.

The py installer is nice, but it requires a certain amount of knowledge to know when and how to convert commands given in documentation to work with py. I think users generally want to be able to run commands in the way that docs give them.

It's annoying that stuff like this is such a minefield, there are a bunch of ways to make things work using just the py launcher - like by using pipx for tools and virtual environments when you want to just be able to python, but I wanted to try and keep this guide fairly simple.


You pip installed a command line program like [`pyinstaller`](https://pypi.org/project/pyinstaller/) or
[`black`](https://pypi.org/project/black/) that is intended to be run from the command line, but it does not work. Two
common causes for this are:

- You are using Python from the Microsoft Store ([which we don't recommend](../microsoft-store)).

To check if this is the case, type `pip -V`. If the path output includes something like
`PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0`, you are using Python from the Microsoft
Store. To fix this you will either have to use the full path to the scripts you want to use, or [uninstall the
Microsoft Store Python](../microsoft-store/#uninstalling) and [install Python
properly](../installing-and-using-python) with a [python.org installer](https://www.python.org/downloads).

- You manually modified your PATH Environment Variable incorrectly

If you added `Python` to PATH manually through environment variables, it is possible that you only added the
executable but not the `\Scripts` folder. This would cause issues with `pip` and make command line tools not
accessible. Look at our [guide on adding Python to the
PATH](../putting-python-on-path/#advanced-method-manually-edit-the-path) for how to verify and fix this.

Projects that add a command line program usually work by adding an executable to the `\Scripts` folder of your Python
install (the full path of which is `C:\Users\<user>\AppData\Local\Programs\Python\Python311\Scripts` in a default 3.11
install). If you checked "Add python.exe to PATH" when installing Python, this folder will have been added to PATH, so
anything there should be runnable directly from the command line. Virtual environments also have their own `\Scripts`
directories that things can be installed to and are effectively on the PATH when the venv is properly activated.

### I get a `SyntaxError` when trying to run `pip`,`python`, or another command.

When you type `python` in the terminal you enter into the python REPL ([read-evaluate-print
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When you type `python` in the terminal you enter into the python REPL ([read-evaluate-print
When you type `python` or `py` in the terminal you enter into the python REPL ([read-evaluate-print

loop](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)), also known as the Python console. This lets
you run `Python` code line by line without having to create a file. You also may have opened a Python REPL by clicking
on the Python App in your Start Menu.

General commands like `pip` and `python` should be run in your computer's normal terminal, that is, directly in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
General commands like `pip` and `python` should be run in your computer's normal terminal, that is, directly in
General commands like `py`, `pip` and `python` should be run in your computer's normal terminal, that is, directly in

something like Command Prompt or PowerShell. _Not_ in the Python REPL, which usually has `>>>` at the start of each
line. To exit the Python REPL, type `exit()` and press enter. You should then be able to run your commands normally,
unless it was the Python App, in which case it will close completely and you'll need to open a normal terminal
separately.

### I tried to uninstall Python by deleting the folder, now it doesn't work and I can't reinstall it!

You should never uninstall your Python installation by deleting the folder it is in. If you have, you may need to edit
Registry Keys to fully remove the installation. Doing this is dangerous and beyond the scope of this guide.

To uninstall Python properly, run the installer for the version you want to uninstall and select "uninstall". If you no
longer have the installer, you can [re-download it from python.org](https://www.python.org/downloads/windows/) first.
Loading