diff options
author | 2022-10-21 15:52:53 +0100 | |
---|---|---|
committer | 2022-10-21 15:52:53 +0100 | |
commit | f072093d8b4bce5cd0f7104509f29ff292f46597 (patch) | |
tree | 3bdce42f4514269c27077a108175c4d3f5dd19ae | |
parent | Wrap lines and make some more changes (diff) |
Made some changes after proof-reading
4 files changed, 94 insertions, 84 deletions
diff --git a/pydis_site/apps/content/resources/guides/python-guides/common-windows-issues.md b/pydis_site/apps/content/resources/guides/python-guides/common-windows-issues.md index 56123556..0bdb11ef 100644 --- a/pydis_site/apps/content/resources/guides/python-guides/common-windows-issues.md +++ b/pydis_site/apps/content/resources/guides/python-guides/common-windows-issues.md @@ -6,7 +6,7 @@ description: This guide describes common issues you may run into when using Pyth ### When I run `python` in the terminal I get no result, or the Windows Store opens! By default Windows has an alias for `python` in the terminal to guide you to -install it from the Windows Store. We would not recommend that you install +install it from the Microsoft Store. I would not recommend that you install Python from the Microsoft Store, see why [here](../microsoft-store.md). 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 @@ -24,12 +24,13 @@ detected, see the below question. If you have not installed Python, you will need to do that. Follow our guide [here](../install-on-windows.md). -If you have installed python and are still getting the error, it is likely that +If you have installed python and are still having the issue, it is likely that you forgot to add to PATH when doing so. See [our guide on adding Python to PATH](../python-on-windows.md#changing-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](../python-on-windows.md#the-py-launcher) for more information. +[this guide](../python-on-windows.md#the-py-launcher) for more information +on that. ### I `pip` installed a package but when running my code get a `ModuleNotFoundError` @@ -37,13 +38,15 @@ You can also use the `py` launcher instead of `python` by just replacing 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 running your - code with to ensure if it actually errors. + possible that this is just an mistake by the editor, so try actually running + your code with to ensure if it actually errors. - If your code runs fine, you could try restarting your editor - if you have + 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 it to ensure it is looking for the - module in the correct Python environment. + 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](./python-on-windows.md#virtual-environments) + for more information. * #### Was the install successful? @@ -54,12 +57,12 @@ You can also use the `py` launcher instead of `python` by just replacing * #### 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, you need to make sure you get the name + in what you installed from PyPI, 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 docs or PyPI page if you are unsure. Examples - of this are: + 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` @@ -71,12 +74,23 @@ You can also use the `py` launcher instead of `python` by just replacing * #### 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 executable, the one that the `pip` you - invoked is referring to. This could be different to the one you are running - the script from, usually because it's using a different python version, or a - virtual environment. + 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. - * ###### I installed the module using pip + ###### 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. + + 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 returned it should be in the form `<PATH_TO_PYTHON>\Lib\site-packages\pip`. @@ -86,47 +100,33 @@ You can also use the `py` launcher instead of `python` by just replacing 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`. See the - relevant "I'm running with ..." sections below for how to resolve this. - - * ###### I installed the module using PyCharm - - PyCharm creates a virtual environment by default, so if you're - installing the module using Pycharm you need to make sure you also run - your code from Pycharm. - - How to fix it depends on how you're running your code - - * ###### I'm running my code with `python` from the terminal. - - If you are using a virtual environment, ensure you have activated your - venv before running `pip install` + different, this is the cause of the `ModuleNotFoundError`. Follow the + relevant "I'm running my code with ..." section below for how to fix + this. - * If you are running your code through your editor, make sure you have - it set up to use the same editor you are `pip install`ing into. - * Use the `py` command to specify the python version you want to pip - install into, e.g. `py -3.9 -m pip install numpy`, or to specify. If - you're still getting an error, the chances are you installed the module - to a different environment to the one you're running python with. This - is very common, and how best to fix it depends on what editor you're - using. + ###### I'm running my code with `python` from the terminal. - * ###### I'm running my code with PyCharm + 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. - PyCharm creates a virtual environment for each project by default, so - you need to make sure that virtual environment is activated before - installing modules. The easiest way to do this is by `pip install`ing - directly from the PyCharm editor terminal, where it will be activated by - default. + 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`. - You can also use PyCharm's GUI to install modules. + 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](../python-on-windows.md#adding-python-to-your-path) + for how to fix this. - * ###### I'm running my code with VSCode + ###### I'm running my code with the button in VSCode To run your code from the same environment you installed Python to, set the 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). + by following + [this guide](https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment). The path should be in the form `PATH_TO_PYTHON\python.exe` Alternatively, you can pip install to the currently activated diff --git a/pydis_site/apps/content/resources/guides/python-guides/install-on-windows.md b/pydis_site/apps/content/resources/guides/python-guides/install-on-windows.md index fcffc0f2..2422e024 100644 --- a/pydis_site/apps/content/resources/guides/python-guides/install-on-windows.md +++ b/pydis_site/apps/content/resources/guides/python-guides/install-on-windows.md @@ -3,10 +3,10 @@ title: Installing Python on Windows description: How to properly install python on windows --- -The recommended way to install Python on windows is directly from the [Python -website's Downloads page](https://www.python.org/downloads/windows/). Installing -Python from the Microsoft Store is not recommended as it can [cause -issues](../microsoft-store.md). +The recommended way to install Python on windows is directly from the +[Python website's Downloads page](https://www.python.org/downloads/windows/). +Installing Python from the Microsoft Store is not recommended as it can +[cause issues](../microsoft-store.md). ## Selecting an Installer There are many different installer options available from the downloads page. @@ -51,7 +51,7 @@ Python installation on your PATH and don't want this one to override it, don't tick this. If you installed Python without adding to PATH and now want to add it, see -(#changing-path)[#changing-path]. +[our guide on adding Python to PATH](../python-on-windows.md#adding-python-to-your-path). Then simply click install, and wait for the install to finish! diff --git a/pydis_site/apps/content/resources/guides/python-guides/microsoft-store.md b/pydis_site/apps/content/resources/guides/python-guides/microsoft-store.md index 4171a3ec..8935163b 100644 --- a/pydis_site/apps/content/resources/guides/python-guides/microsoft-store.md +++ b/pydis_site/apps/content/resources/guides/python-guides/microsoft-store.md @@ -5,13 +5,14 @@ description: This guide explains the issues with using Python installed from the Microsoft provides a Python app on the Microsoft Store as an alternative to using the [Standard Installer](https://www.python.org/downloads/). We would -recommend that you use the standard installer wherever possible. +recommend that you use the standard installer instead wherever possible. Here are some common issues with using Python from the Microsoft Store: * ##### Command line tools won't work - Any command line tools like `black` or `pyinstaller` won't work directly. This - happens because they work by adding an executable to the `Scripts/` directory, + Most command line tools, like `black` or `pyinstaller`, won't work directly + (without specifying the full path, or invoking them as a module if they allow + it). This happens because they work by adding an executable to the `Scripts/` directory, which isn't added to PATH on the Microsoft Store version of Python. * ##### It can cause issues with permissions @@ -23,8 +24,8 @@ Here are some common issues with using Python from the Microsoft Store: `python.exe` isn't added to PATH, but `pip.exe` is. This makes it easy to have an inconsistent `pip` and `python`. The `pip` is under the `C:\Users\username\AppData\Local\Microsoft\WindowsApps` entry, which wouldn't - obviously have anything python related in it. If you add your normal python - install path below this one the `pip` will be overridden by the `WindowsApps` + obviously have anything Python related in it. If you add your normal Python + install path below this one, it's `pip` will be overridden by the `WindowsApps` one but the `python` one wont as it's overwriting the app execution alias. * ##### Confusing App Execution Alias Behaviour diff --git a/pydis_site/apps/content/resources/guides/python-guides/python-on-windows.md b/pydis_site/apps/content/resources/guides/python-guides/python-on-windows.md index 04d1073e..62b006e5 100644 --- a/pydis_site/apps/content/resources/guides/python-guides/python-on-windows.md +++ b/pydis_site/apps/content/resources/guides/python-guides/python-on-windows.md @@ -6,10 +6,10 @@ description: This guide explains how manage your Python installation(s) on Windo ## The py launcher By default, Python installed on Windows using the online Python installer comes with the "py launcher". It is a command line tool you invoke using the `py` -command. +command, and helps you invoke a specific python version. -It provides the very useful `py -0p` command. This lists the python versions you -have installed, the output will look like this: +It provides the `py -0p` command. This lists the python versions you have +installed, the output will look like this: C:\Users\username>py -0p Installed Pythons found by py Launcher for Windows @@ -21,23 +21,23 @@ The versions will be ordered from newest to oldest, and the `*` will indicate which version running `py` will call by default. This depends on the following requirements, and may not be the same version you get from running `python` (if you get any): - - If an exact version is not given, using the latest version can be overridden by - any of the following, (in priority order): - • An active virtual environment - • A shebang line in the script (if present) - • With -2 or -3 flag a matching PY_PYTHON2 or PY_PYTHON3 Environment variable - • A PY_PYTHON Environment variable - • From [defaults] in py.ini in your %LOCALAPPDATA%\py.ini - • From [defaults] in py.ini beside py.exe (use `where py` to locate) - +```text +If an exact version is not given, using the latest version can be overridden by +any of the following, (in priority order): +• An active virtual environment +• A shebang line in the script (if present) +• With -2 or -3 flag a matching PY_PYTHON2 or PY_PYTHON3 Environment variable +• A PY_PYTHON Environment variable +• From [defaults] in py.ini in your %LOCALAPPDATA%\py.ini +• From [defaults] in py.ini beside py.exe (use `where py` to locate) +``` You can override which version is called by specifying the major and minor versions to used. For example, to invoke python 3.7, you could run `py -3.7`. -You can then pass any arguments to `python` on top of that, for example `py -3.7 -myscript.py` to run `myscript.py`, or `py -3.7 -m pip install numpy` to invoke -`pip` to install numpy into that version. +You can then pass any arguments to `python` on top of that, for example +`py -3.7 myscript.py` to run `myscript.py`, or `py -3.7 -m pip install numpy` +to invoke `pip` to install numpy into that version. -You can use `py` instead of `python` and not have any python versions on path at +You can use `py` instead of `python` and not have any python versions on PATH at all. I would recommend having your "main" python version on path so you can invoke it with `python` if you want to, and then use `py` whenever you want a different version. The full documentation of the `py launcher` can be found @@ -46,7 +46,7 @@ different version. The full documentation of the `py launcher` can be found ## Virtual Environments Virtual environments, (aka `venvs`), are a way of letting each of your projects -run in it's own python environment, different projects can have different +run in it's own python environment so different projects can have different versions of the same dependencies. This means that a `pip install` to your main Python will not affect a project where you're using a virtual environment. @@ -66,12 +66,12 @@ the command line. Whenever you type a command name in your terminal, for example `python` or `pip`, the terminal will look it up in the PATH to try and find out what executable that command refers to, so it can be run. -The Python installer does not add `python` to your PATH by default, but even if -you forgot to add it when installing, it is easy to change. - +The Python installer provides an option to add `python` to your PATH, although it +is not checked by default so many people miss it. Don't worry if you forgot to select +this though, you can add it after installing: ### The safe method: through the installer -The easiest and and safest way to add Python to PATH is through the installer. +The easiest and and safest way to add Python to PATH is by rerunning the installer. Here is how to do that: 1. Search for "Apps and features" in the Windows Search Bar. @@ -91,8 +91,8 @@ terminals/editors you have open before they detect the change. ### The advanced method: editing environment variables -If you no longer have the installer, or want more control over variables in -PATH, you can edit the environment variables manually. +If you want more control over the entries in your PATH, you can edit the PATH +environment variables manually. First, search "environment variables" in Windows search and click on "Edit the system environment variables". Then click the "Environment Variables" button @@ -129,3 +129,12 @@ To add a folder, click "Add New", and enter the folder name. You can then move it up above any other Python versions you have installed if necessary. Watch out for `C:\Users\username\AppData\Local\Microsoft\WindowsApps` as if you have Python on the Windows Store it will be installed there. + +#### Verifying your changes + +You can check that `python` was added to PATH correctly by running +`python -c "import sys;print(sys.executable)"` and checking the executable +returned is the one you want. + +You can check that `pip` was added to PATH correctly by running `pip -V` +and checking the executable matches the one returned by `python` |