Python windows packages directory

Installing PackagesВ¶

This section covers the basics of how to install Python packages .

It’s important to note that the term “package” in this context is being used to describe a bundle of software to be installed (i.e. as a synonym for a distribution ). It does not to refer to the kind of package that you import in your Python source code (i.e. a container of modules). It is common in the Python community to refer to a distribution using the term “package”. Using the term “distribution” is often not preferred, because it can easily be confused with a Linux distribution, or another larger software distribution like Python itself.

Requirements for Installing PackagesВ¶

This section describes the steps to follow before installing other Python packages.

Ensure you can run Python from the command lineВ¶

Before you go any further, make sure you have Python and that the expected version is available from your command line. You can check this by running:

You should get some output like Python 3.6.3 . If you do not have Python, please install the latest 3.x version from python.org or refer to the Installing Python section of the Hitchhiker’s Guide to Python.

If you’re a newcomer and you get an error like this:

It’s because this command and other suggested commands in this tutorial are intended to be run in a shell (also called a terminal or console). See the Python for Beginners getting started tutorial for an introduction to using your operating system’s shell and interacting with Python.

If you’re using an enhanced shell like IPython or the Jupyter notebook, you can run system commands like those in this tutorial by prefacing them with a ! character:

It’s recommended to write rather than plain python in order to ensure that commands are run in the Python installation matching the currently running notebook (which may not be the same Python installation that the python command refers to).

Due to the way most Linux distributions are handling the Python 3 migration, Linux users using the system Python without creating a virtual environment first should replace the python command in this tutorial with python3 and the python -m pip command with python3 -m pip —user . Do not run any of the commands in this tutorial with sudo : if you get a permissions error, come back to the section on creating virtual environments, set one up, and then continue with the tutorial as written.

Ensure you can run pip from the command lineВ¶

Additionally, you’ll need to make sure you have pip available. You can check this by running:

If you installed Python from source, with an installer from python.org, or via Homebrew you should already have pip. If you’re on Linux and installed using your OS package manager, you may have to install pip separately, see Installing pip/setuptools/wheel with Linux Package Managers .

If pip isn’t already installed, then first try to bootstrap it from the standard library:

If that still doesn’t allow you to run python -m pip :

Run python get-pip.py . 2 This will install or upgrade pip. Additionally, it will install setuptools and wheel if they’re not installed already.

Be cautious if you’re using a Python install that’s managed by your operating system or another package manager. get-pip.py does not coordinate with those tools, and may leave your system in an inconsistent state. You can use python get-pip.py —prefix=/usr/local/ to install in /usr/local which is designed for locally-installed software.

Читайте также:  Exploit windows server 2012

Ensure pip, setuptools, and wheel are up to dateВ¶

While pip alone is sufficient to install from pre-built binary archives, up to date copies of the setuptools and wheel projects are useful to ensure you can also install from source archives:

Optionally, create a virtual environmentВ¶

See section below for details, but here’s the basic venv 3 command to use on a typical Linux system:

This will create a new virtual environment in the tutorial_env subdirectory, and configure the current shell to use it as the default python environment.

Creating Virtual EnvironmentsВ¶

Python “Virtual Environments” allow Python packages to be installed in an isolated location for a particular application, rather than being installed globally. If you are looking to safely install global command line tools, see Installing stand alone command line tools .

Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python3.6/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

In all these cases, virtual environments can help you. They have their own installation directories and they don’t share libraries with other virtual environments.

Currently, there are two common tools for creating Python virtual environments:

venv is available by default in Python 3.3 and later, and installs pip and setuptools into created virtual environments in Python 3.4 and later.

virtualenv needs to be installed separately, but supports Python 2.7+ and Python 3.3+, and pip , setuptools and wheel are always installed into created virtual environments by default (regardless of Python version).

Install a Python package into a different directory using pip?

I know the obvious answer is to use virtualenv and virtualenvwrapper, but for various reasons I can’t/don’t want to do that.

So how do I modify the command

to make pip install the package somewhere other than the default site-packages ?

17 Answers 17

You might also want to use —ignore-installed to force all dependencies to be reinstalled using this new prefix. You can use —install-option to multiple times to add any of the options you can use with python setup.py install ( —prefix is probably what you want, but there are a bunch more options you could use).

The —target switch is the thing you’re looking for:

But you still need to add d:\somewhere\other\than\the\default to PYTHONPATH to actually use them from that location.

-t, —target
Install packages into . By default this will not replace existing files/folders in .
Use —upgrade to replace existing packages in with new versions.

Upgrade pip if target switch is not available:

On Linux or OS X:

On Windows (this works around an issue):

Instead of the —target option or the —install-options option, I have found that the following works well (from discussion on a bug regarding this very thing at https://github.com/pypa/pip/issues/446):

(Or set the PYTHONUSERBASE directory in your environment before running the command, using export PYTHONUSERBASE=/path/to/install/to )

This uses the very useful —user option but tells it to make the bin , lib , share and other directories you’d expect under a custom prefix rather than $HOME/.local .

Читайте также:  Загрузочная флешка кали линукс руфус

Then you can add this to your PATH , PYTHONPATH and other variables as you would a normal installation directory.

Note that you may also need to specify the —upgrade and —ignore-installed options if any packages upon which this depends require newer versions to be installed in the PYTHONUSERBASE directory, to override the system-provided versions.

A full example:

..to install the scipy and numpy package most recent versions into a directory which you can then include in your PYTHONPATH like so (using bash and for python 2.6 on CentOS 6 for this example):

Using virtualenv is still a better and neater solution!

How do I install Python packages on Windows?

I’m having a hard time setting up python packages. EasyInstall from SetupTools is supposed to help that, but they don’t have an executable for Python 2.6.

For instance to install Mechanize, I’m just supposed to put the Mechanize folder in C:\Python24\Lib\site-packages according to INSTALL.txt, but runnning the tests does not work. Can someone help shed some light on this? Thanks!

12 Answers 12

The accepted answer is outdated. So first, pip is preferred over easy_install , (Why use pip over easy_install?). Then follow these steps to install pip on Windows, it’s quite easy.

Optionally, you can add the path to your environment so that you can use pip anywhere. It’s somewhere like C:\Python33\Scripts .

Newer versions of Python for Windows come with the pip package manager. (source)

pip is already installed if you’re using Python 2 >=2.7.9 or Python 3 >=3.4

Use that to install packages:

So in your case it’d be:

This is a good tutorial on how to get easy_install on windows. The short answer: add C:\Python26\Scripts (or whatever python you have installed) to your PATH.

You don’t need the executable for setuptools. You can download the source code, unpack it, traverse to the downloaded directory and run python setup.py install in the command prompt

Starting with Python 2.7, pip is included by default. Simply download your desired package via

Packaging in Python is dire. The root cause is that the language ships without a package manager.

Fortunately, there is one package manager for Python, called Pip. Pip is inspired by Ruby’s Gem, but lacks some features. Ironically, Pip itself is complicated to install. Installation on the popular 64-bit Windows demands building and installing two packages from source. This is a big ask for anyone new to programming.

So the right thing to do is to install pip. However if you can’t be bothered, Christoph Gohlke provides binaries for popular Python packages for all Windows platforms http://www.lfd.uci.edu/

In fact, building some Python packages requires a C compiler (eg. mingw32) and library headers for the dependencies. This can be a nightmare on Windows, so remember the name Christoph Gohlke.

I had problems in installing packages on Windows. Found the solution. It works in Windows7+. Mainly anything with Windows Powershell should be able to make it work. This can help you get started with it.

  • Firstly, you’ll need to add python installation to your PATH variable. This should help.
  • You need to download the package in zip format that you are trying to install and unzip it. If it is some odd zip format use 7Zip and it should be extracted.
  • Navigate to the directory extracted with setup.py using Windows Powershell (Use link for it if you have problems)
  • Run the command python setup.py install

That worked for me when nothing else was making any sense. I use Python 2.7 but the documentation suggests that same would work for Python 3.x also.

How do I find the location of my Python site-packages directory?

How do I find the location of my site-packages directory?

Читайте также:  Press any key to boot from usb при установке windows

21 Answers 21

There are two types of site-packages directories, global and per user.

Global site-packages («dist-packages») directories are listed in sys.path when you run:

For a more concise list run getsitepackages from the site module in Python code:

Note: With virtualenvs getsitepackages is not available, sys.path from above will list the virtualenv’s site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:

The per user site-packages directory (PEP 370) is where Python installs your local packages:

If this points to a non-existing directory check the exit status of Python and see python -m site —help for explanations.

Hint: Running pip list —user or pip freeze —user gives you a list of all installed per user site-packages.

Practical Tips

.__path__ lets you identify the location(s) of a specific package: (details)

.__file__ lets you identify the location of a specific module: (difference)

to show Debian-style package information:

(or just first item with site.getsitepackages()[0] )

A solution that:

  • outside of virtualenv — provides the path of global site-packages,
  • insidue a virtualenv — provides the virtualenv’s site-packages

. is this one-liner:

Formatted for readability (rather than use as a one-liner), that looks like the following:

Source: an very old version of «How to Install Django» documentation (though this is useful to more than just Django installation)

It will point you to /usr/lib/pythonX.X/dist-packages

This folder only contains packages your operating system has automatically installed for programs to run.

On ubuntu, the site-packages folder that contains packages installed via setup_tools\easy_install\pip will be in /usr/local/lib/pythonX.X/dist-packages

The second folder is probably the more useful one if the use case is related to installation or reading source code.

If you do not use Ubuntu, you are probably safe copy-pasting the first code box into the terminal.

This is what worked for me:

/.local/lib/python2.7/site-packages ). – Neil Traft Jul 11 ’14 at 18:45

Let’s say you have installed the package ‘django’. import it and type in dir(django). It will show you, all the functions and attributes with that module. Type in the python interpreter —

You can do the same thing if you have installed mercurial.

This is for Snow Leopard. But I think it should work in general as well.

A modern stdlib way is using sysconfig module, available in version 2.7 and 3.2+. Unlike the current accepted answer, this method still works regardless of whether or not you have a virtual environment active.

Note: sysconfig (source) is not to be confused with the distutils.sysconfig submodule (source) mentioned in several other answers here. The latter is an entirely different module and it’s lacking the get_paths function discussed below.

Python currently uses eight paths (docs):

  • stdlib: directory containing the standard Python library files that are not platform-specific.
  • platstdlib: directory containing the standard Python library files that are platform-specific.
  • platlib: directory for site-specific, platform-specific files.
  • purelib: directory for site-specific, non-platform-specific files.
  • include: directory for non-platform-specific header files.
  • platinclude: directory for platform-specific header files.
  • scripts: directory for script files.
  • data: directory for data files.

In most cases, users finding this question would be interested in the ‘purelib’ path (in some cases, you might be interested in ‘platlib’ too). The purelib path is where ordinary Python packages will be installed by tools like pip .

At system level, you’ll see something like this:

With a venv, you’ll get something like this

The function sysconfig.get_paths() returns a dict of all of the relevant installation paths, example on Linux:

A shell script is also available to display these details, which you can invoke by executing sysconfig as a module:

Оцените статью