杰瑞科技汇

Windows下Python pip安装配置步骤?

Of course! Here is a comprehensive guide to using pip on Windows, covering everything from installation to common tasks and troubleshooting.

Windows下Python pip安装配置步骤?-图1
(图片来源网络,侵删)

What is pip?

pip is the standard package manager for Python. Think of it like the App Store for Python. It allows you to easily install, upgrade, and manage third-party libraries and packages that your Python projects depend on.

For example, to use a popular library like requests for making web requests, you would simply run pip install requests.


Is pip Already Installed?

Modern Python installations for Windows (version 3.4 and later) come with pip pre-installed. The key is to make sure you can access it from your command line.

How to Check:

  1. Open the Command Prompt:

    Windows下Python pip安装配置步骤?-图2
    (图片来源网络,侵删)
    • Press the Windows Key.
    • Type cmd or Command Prompt.
    • Click on the application to open it.
  2. Check the Python Version: This command will show you which version of Python is installed. If it shows a version like Python 3.10.8, you have Python 3.

    python --version

    Note: If you get an error like 'python' is not recognized..., you may need to add Python to your PATH or use py instead.

  3. Check the Pip Version: This command checks if pip is installed and shows its version.

    pip --version

    If this works, you're ready to go! If you get an error like 'pip' is not recognized..., don't worry. The solutions are below.

    Windows下Python pip安装配置步骤?-图3
    (图片来源网络,侵删)

How to Install pip

If pip is missing, the easiest way to install it is by using the ensurepip module, which is included with Python.

  1. Open Command Prompt as an Administrator (this avoids permission issues).

    Right-click on the "Command Prompt" in the Start Menu and select "Run as administrator".

  2. Run the Installation Command: This command will install pip and its dependencies.

    python -m ensurepip --upgrade

    After it runs, you can verify the installation by running pip --version again.


Essential pip Commands

Here are the most common pip commands you'll use every day.

a) Install a Package

This is the most frequent command. It downloads and installs the latest version of a package from the Python Package Index (PyPI).

# Install a single package
pip install requests
# Install a specific version of a package
pip install numpy==1.23.5
# Install a package from a local file (e.g., a .whl or .tar.gz)
pip install C:\path\to\your\package.whl

b) Upgrade a Package

If a new version of a package is available, you can upgrade it.

pip install --upgrade requests

Note: --upgrade can be shortened to -U.

c) Uninstall a Package

To remove a package that you no longer need.

pip uninstall requests

pip will ask for confirmation before removing the package.

d) List Installed Packages

See all the packages currently installed in your Python environment.

pip list

e) Show Package Details

Get detailed information about a specific package, including its location, version, and dependencies.

pip show requests

f) Export/Import Requirements

This is crucial for sharing projects. You can create a file that lists all the packages and their exact versions needed for your project.

  • Generate a requirements.txt file:

    pip freeze > requirements.txt

    This command creates a file named requirements.txt in your current directory with a list like this:

    requests==2.28.1
    numpy==1.23.5
  • Install packages from a requirements.txt file: This is how you set up a project on a new machine or share it with a teammate.

    pip install -r requirements.txt

    Note: -r stands for "requirements file".


Managing Python Environments (Best Practice)

It's a bad practice to install all your packages globally. Instead, you should create isolated environments for each project. This prevents version conflicts.

Using venv (Built-in)

  1. Navigate to your project folder in Command Prompt.

    cd C:\Users\YourUser\Documents\my_project
  2. Create a virtual environment. We'll name it venv.

    python -m venv venv

    This creates a new folder named venv containing a separate Python interpreter and pip.

  3. Activate the environment. This command modifies your command prompt to show that the environment is active and makes its pip and python commands available.

    venv\Scripts\activate

    Your command prompt will now change to show (venv) at the beginning.

  4. Work in the environment. Now, any pip install command will install packages only into this venv folder, not globally.

    (venv) pip install pandas matplotlib
  5. Deactivate the environment. When you're done working, simply type:

    deactivate

    Your command prompt will return to normal.


Troubleshooting

Problem 1: 'python' is not recognized as an internal or external command...

This means the Windows PATH environment variable does not include the path to your Python executable.

Solution (Easy Way): Use the py launcher, which is installed with Python. It's more reliable as it automatically finds your Python installation.

# Instead of 'pip install ...'
py -m pip install requests
# Instead of 'python --version'
py --version

Using py -m pip is the recommended practice on Windows to avoid PATH issues.

Solution (Hard Way - Edit PATH):

  1. Press the Windows Key, type "Environment Variables", and select "Edit the system environment variables".
  2. In the System Properties window, click the "Environment Variables..." button.
  3. In the "System variables" section (bottom half), find the Path variable and click "Edit...".
  4. Click "New" and add the path to your Python Scripts folder. It will look something like this: C:\Users\YourUser\AppData\Local\Programs\Python\Python310\ And another entry for the Scripts folder: C:\Users\YourUser\AppData\Local\Programs\Python\Python310\Scripts
  5. Click OK on all windows and restart your Command Prompt.

Problem 2: pip is very slow

This is usually because it's trying to download from the default PyPI index server, which can sometimes be slow or have connection issues.

Solution: Use a Mirror You can configure pip to use a faster, local mirror. For example, in China, users often use the Tsinghua mirror.

To make this change permanent, run this command:

py -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

To set it as the default for all future pip commands:

py -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

You can replace the URL with other mirrors like https://pypi.douban.com/simple/ (豆瓣) or https://mirrors.aliyun.com/pypi/simple/ (阿里云).

分享:
扫描分享到社交APP
上一篇
下一篇