To install a Python library, use Python’s package installer, pip, in a terminal: run python -m pip install package-name, replacing the name with the library’s package name. For a project, first activate its virtual environment so the library is installed in the right place and does not interfere with other projects.
What a Python library is
A Python library is reusable code that adds capabilities to a program. A library might help work with dates, process images, communicate with a service, or analyze data. Instead of writing every feature yourself, you install a package that makes the library available to your Python code.
People often use “library” and “package” interchangeably, but the terms are not identical in every technical context. A distribution package is what an installer downloads and installs; it may provide one or more importable modules. The name used to install a package can also differ from the name used in an import statement. Check the project’s official documentation for both names.
Installing a library does not usually put its code inside your project folder. Instead, pip places it in the Python environment that is currently active. Your program can import it when it runs with that same environment.
Choose the right Python environment
A Python environment is an installation of Python together with the packages available to it. A computer can have multiple Python versions and environments, so a package may install successfully but remain unavailable to the interpreter running your script. This is one of the most common sources of confusion.
For a project, a virtual environment is usually the clearest choice. It keeps that project’s dependencies separate from packages used by other projects or by the operating system. Create one from the project directory with:
python -m venv .venvcreates a virtual environment in a folder named.venv.- On Windows, activate it with
.venv\Scripts\activate. - On macOS or Linux, activate it with
source .venv/bin/activate. - Then install the package using
python -m pip install package-name.
Activation changes which Python and installer commands your terminal finds first. The prompt may show the environment name, but that visual cue is not guaranteed in every terminal. You can check which interpreter is being used with python --version and, where supported, python -m pip --version.
Install a library step by step
- Open a terminal or command prompt. In a code editor, use its integrated terminal if it is configured for the project’s interpreter.
- Move to the project folder, or create a folder for a new project.
- Create and activate a virtual environment if you want an isolated project setup.
- Find the package’s documented installation name and check that it supports your Python version and operating system.
- Run
python -m pip install package-name. Replace the placeholder with the actual package name; do not type the angle brackets. - Test the installation by importing the library in a short Python session or script.
For example, suppose a project’s documentation says to install sample-package and import it as sample_module. The terminal command would be python -m pip install sample-package; a Python file would then contain import sample_module. These names are illustrative, not a recommendation to install a particular package.
Using python -m pip is generally clearer than typing pip alone because it runs pip through the Python interpreter named by python. If your system uses a different command to launch Python, such as python3 or the Windows py launcher, use that command consistently for both pip and your program.
How pip finds and installs packages
By default, pip looks for packages on the Python Package Index, a public repository of Python distributions. It resolves the requested package’s dependencies, downloads compatible distributions, and installs them into the active environment. A dependency is another package the requested library needs in order to work.
Installation can involve different distribution formats. If a compatible prebuilt wheel is available, pip can install it without compiling the package locally. Otherwise, installation may need build tools or system libraries, depending on the package and platform. That is why the same command can be straightforward for one library and require extra setup for another.
Some packages are not installed from the public package index. A project may document installation from a private repository, a local file, or a source-control checkout. Those methods can require access permissions or extra tools. Follow the project’s official instructions rather than guessing at an alternate source or command.
Check that the installation worked
A successful installer message is useful, but the practical test is whether the intended Python interpreter can import the library. Start Python from the activated environment and try the documented import statement. You can also put that import in a small script and run the script with the same interpreter.
If you see ModuleNotFoundError, Python cannot find a module with the name you attempted to import. Confirm that the package installed in the environment used to run the script, and verify the import name in the library’s documentation. Do not assume the installation name and import name match.
To inspect installed packages in the active environment, run python -m pip list. To see details for a particular installed distribution, use python -m pip show package-name. These commands report what that interpreter’s pip can see; they do not necessarily describe packages installed in another environment.
Record dependencies for a project
A project’s dependencies should be recorded so that another person—or you on another computer—can reproduce its setup. One traditional approach is a requirements file containing package names, often with version constraints. After installing a project’s dependencies, a developer may create a snapshot with python -m pip freeze and save the output to a file named requirements.txt.
A frozen snapshot can include every installed package and exact version in the environment. That may be useful for reproducing a setup, but it can also capture packages unrelated to the project if the environment was not kept focused. For a small project, maintain a deliberate list of direct dependencies and use the format recommended by the project or course. Larger projects may use a dependency manager that handles project metadata, resolution, and lock files.
When installing from a requirements file, the usual command is python -m pip install -r requirements.txt. Run it from the directory containing the file, or provide the correct file path. A file created on one operating system may not be suitable for every other system if some dependencies are platform-specific.
Common installation problems
- Permission denied: The installer may be trying to write to a protected Python location. A virtual environment often avoids the need to change system-wide files. Avoid using administrator privileges as a routine fix; understand which interpreter and destination the command targets.
- Command not found: The terminal may not recognize
pythonorpip. Check how Python is launched on your system and try invoking pip through that interpreter. - Package not found: The package name may be misspelled, may differ from its import name, or may not be available from the configured package source. Check the project’s official installation instructions.
- Version conflict: The library may require a different Python version or may conflict with another dependency. Read the installer’s error message and the package’s compatibility notes; do not randomly upgrade or remove packages in a shared environment.
- Build failure: A package may need a compiler, system library, or other platform-specific prerequisite. Consult its installation guide for your operating system and Python version.
- Works in the terminal, fails in the editor: The editor may be running a different interpreter. Select the project’s virtual environment as the editor’s interpreter, then test the import again.
Security and responsible package use
Installing a package means trusting code from its source to run in your environment. Prefer packages documented by the project or course you are following, check that the package name is correct, and be cautious of commands copied from unfamiliar pages. A lookalike name can refer to a different project.
Use a virtual environment to limit accidental dependency conflicts, but do not treat it as a security barrier: code in an environment can still access files and resources available to your user account. For work involving sensitive data, follow your organization’s software approval and security procedures. Keep dependencies maintained according to the project’s guidance, and avoid installing packages you do not need.
Other ways to install or manage libraries
Not every Python setup should use pip directly. A project may use a dependency manager to declare packages and create reproducible environments. A scientific computing setup may use a different package manager for packages that include non-Python components. A notebook service or managed classroom environment may provide an approved installation method or have packages preinstalled.
There is also a distinction between installing a package for development and installing your own project for use. A developer can install a local project in editable mode so changes to its source are reflected without reinstalling. That is a project-development workflow, not usually necessary when a beginner only needs to add an existing library. Use the method specified by the project’s documentation or your course setup instructions.
Frequently Asked Questions
How do I install a Python library in a terminal?
Activate the environment you intend to use, then run python -m pip install package-name, substituting the package’s documented installation name. Test it by importing the documented module with the same Python interpreter.
Is pip included with Python?
Many standard Python installations include pip, but installation details can vary by operating system and distribution. If the command is unavailable, consult the documentation for your Python installation rather than downloading an installer from an unfamiliar source.
Why can I install a package but not import it?
The package may have been installed into a different environment, or the import name may differ from the installation name. Check the active interpreter, the environment selected in your editor, and the package’s documented import statement.
Should I install libraries globally or in a virtual environment?
For an individual project, a virtual environment is generally easier to manage because it separates that project’s dependencies from other work. Follow system or course instructions when they prescribe a particular setup.
Can I install a Python library inside a notebook?
Some notebook environments allow installation from a notebook cell, but the exact method depends on how the notebook is managed and which interpreter it uses. Use the environment’s official instructions, and make sure the package is installed where the notebook kernel can access it.
In short
For most project work, create and activate a virtual environment, install the documented package with python -m pip install, and test its import using the same interpreter. Keeping environments separate and recording project dependencies makes installations easier to understand, reproduce, and troubleshoot.