I'm relatively new to Python and thought that I had a basic understanding of pip install
, but I'm stumped with the following:
$ pip install ".[full]"
I've only seen pip install ".[full]"
on a couple of occasions, and I'm not sure what this command actually does. The pattern I've seen is that I clone a git repo, I change directories to that project root, and run pip install ".[full]"
in my activated virtual environment of choice (I'm using conda). I'm guessing it looks for something in the project, but the terminal output doesn't say exactly what file/directory it is reading. Is this reading the ./build directory, or maybe the ./src directory, or some config file?
setup.py
#!/usr/bin/env python
import os
import subprocess
import sys
# Required third-party imports, must be specified in pyproject.toml.
import packaging.version
import setuptools
def process_options():
"""
Determine all runtime options, returning a dictionary of the results. The
keys are:
'rootdir': str
The root directory of the setup. Almost certainly the directory
that this setup.py file is contained in.
'release': bool
Is this a release build (True) or a local development build (False)
"""
options = {}
options['rootdir'] = os.path.dirname(os.path.abspath(__file__))
options = _determine_version(options)
return options
def _determine_version(options):
"""
Adds the 'short_version', 'version' and 'release' options.
Read from the VERSION file to discover the version. This should be a
single line file containing valid Python package public identifier (see PEP
440), for example
4.5.2rc2
5.0.0
5.1.1a1
We do that here rather than in setup.cfg so we can apply the local
versioning number as well.
"""
version_filename = os.path.join(options['rootdir'], 'VERSION')
with open(version_filename, "r") as version_file:
version_string = version_file.read().strip()
version = packaging.version.parse(version_string)
if isinstance(version, packaging.version.LegacyVersion):
raise ValueError("invalid version: " + version_string)
options['short_version'] = str(version.public)
options['release'] = not version.is_devrelease
if not options['release']:
# Put the version string into canonical form, if it wasn't already.
version_string = str(version)
version_string += "+"
try:
git_out = subprocess.run(
('git', 'rev-parse', '--verify', '--short=7', 'HEAD'),
check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
)
git_hash = git_out.stdout.decode(sys.stdout.encoding).strip()
version_string += git_hash or "nogit"
# CalledProcessError is for if the git command fails for internal
# reasons (e.g. we're not in a git repository), OSError is for if
# something goes wrong when trying to run git (e.g. it's not installed,
# or a permission error).
except (subprocess.CalledProcessError, OSError):
version_string += "nogit"
options['version'] = version_string
return options
def create_version_py_file(options):
"""
Generate and write out the file version.py, which is used to produce the
'__version__' information for the module. This function will overwrite an
existing file at that location.
"""
filename = os.path.join(
options['rootdir'], 'src', 'qutip_tensorflow', 'version.py',
)
content = "\n".join([
"# This file is automatically generated during package setup.",
f"short_version = '{options['short_version']}'",
f"version = '{options['version']}'",
f"release = {options['release']}",
])
with open(filename, 'w') as file:
print(content, file=file)
if __name__ == "__main__":
options = process_options()
create_version_py_file(options)
# Most of the kwargs to setup are defined in setup.cfg; the only ones we
# keep here are ones that we have done some compile-time processing on.
setuptools.setup(
version=options['version'],
)
setup.cfg
"""Extensions to the 'distutils' for large or complex distributions"""
from fnmatch import fnmatchcase
import functools
import os
import re
import _distutils_hack.override # noqa: F401
import distutils.core
from distutils.errors import DistutilsOptionError
from distutils.util import convert_path
from ._deprecation_warning import SetuptoolsDeprecationWarning
import setuptools.version
from setuptools.extension import Extension
from setuptools.dist import Distribution
from setuptools.depends import Require
from . import monkey
__all__ = [
'setup',
'Distribution',
'Command',
'Extension',
'Require',
'SetuptoolsDeprecationWarning',
'find_packages',
'find_namespace_packages',
]
__version__ = setuptools.version.__version__
bootstrap_install_from = None
class PackageFinder:
"""
Generate a list of all Python packages found within a directory
"""
@classmethod
def find(cls, where='.', exclude=(), include=('*',)):
"""Return a list all Python packages found within directory 'where'
'where' is the root directory which will be searched for packages. It
should be supplied as a "cross-platform" (i.e. URL-style) path; it will
be converted to the appropriate local path syntax.
'exclude' is a sequence of package names to exclude; '*' can be used
as a wildcard in the names, such that 'foo.*' will exclude all
subpackages of 'foo' (but not 'foo' itself).
'include' is a sequence of package names to include. If it's
specified, only the named packages will be included. If it's not
specified, all found packages will be included. 'include' can contain
shell style wildcard patterns just like 'exclude'.
"""
return list(
cls._find_packages_iter(
convert_path(where),
cls._build_filter('ez_setup', '*__pycache__', *exclude),
cls._build_filter(*include),
)
)
@classmethod
def _find_packages_iter(cls, where, exclude, include):
"""
All the packages found in 'where' that pass the 'include' filter, but
not the 'exclude' filter.
"""
for root, dirs, files in os.walk(where, followlinks=True):
# Copy dirs to iterate over it, then empty dirs.
all_dirs = dirs[:]
dirs[:] = []
for dir in all_dirs:
full_path = os.path.join(root, dir)
rel_path = os.path.relpath(full_path, where)
package = rel_path.replace(os.path.sep, '.')
# Skip directory trees that are not valid packages
if '.' in dir or not cls._looks_like_package(full_path):
continue
# Should this package be included?
if include(package) and not exclude(package):
yield package
# Keep searching subdirectories, as there may be more packages
# down there, even if the parent was excluded.
dirs.append(dir)
@staticmethod
def _looks_like_package(path):
"""Does a directory look like a package?"""
return os.path.isfile(os.path.join(path, '__init__.py'))
@staticmethod
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches at least one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
class PEP420PackageFinder(PackageFinder):
@staticmethod
def _looks_like_package(path):
return True
find_packages = PackageFinder.find
find_namespace_packages = PEP420PackageFinder.find
def _install_setup_requires(attrs):
# Note: do not use `setuptools.Distribution` directly, as
# our PEP 517 backend patch `distutils.core.Distribution`.
class MinimalDistribution(distutils.core.Distribution):
"""
A minimal version of a distribution for supporting the
fetch_build_eggs interface.
"""
def __init__(self, attrs):
_incl = 'dependency_links', 'setup_requires'
filtered = {k: attrs[k] for k in set(_incl) & set(attrs)}
distutils.core.Distribution.__init__(self, filtered)
def finalize_options(self):
"""
Disable finalize_options to avoid building the working set.
Ref #2158.
"""
dist = MinimalDistribution(attrs)
# Honor setup.cfg's options.
dist.parse_config_files(ignore_option_errors=True)
if dist.setup_requires:
dist.fetch_build_eggs(dist.setup_requires)
def setup(**attrs):
# Make sure we have any requirements needed to interpret 'attrs'.
_install_setup_requires(attrs)
return distutils.core.setup(**attrs)
setup.__doc__ = distutils.core.setup.__doc__
_Command = monkey.get_unpatched(distutils.core.Command)
class Command(_Command):
__doc__ = _Command.__doc__
command_consumes_arguments = False
def __init__(self, dist, **kw):
"""
Construct the command for dist, updating
vars(self) with any keyword parameters.
"""
_Command.__init__(self, dist)
vars(self).update(kw)
def _ensure_stringlike(self, option, what, default=None):
val = getattr(self, option)
if val is None:
setattr(self, option, default)
return default
elif not isinstance(val, str):
raise DistutilsOptionError(
"'%s' must be a %s (got `%s`)" % (option, what, val)
)
return val
def ensure_string_list(self, option):
r"""Ensure that 'option' is a list of strings. If 'option' is
currently a string, we split it either on /,\s*/ or /\s+/, so
"foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
["foo", "bar", "baz"].
"""
val = getattr(self, option)
if val is None:
return
elif isinstance(val, str):
setattr(self, option, re.split(r',\s*|\s+', val))
else:
if isinstance(val, list):
ok = all(isinstance(v, str) for v in val)
else:
ok = False
if not ok:
raise DistutilsOptionError(
"'%s' must be a list of strings (got %r)" % (option, val)
)
def reinitialize_command(self, command, reinit_subcommands=0, **kw):
cmd = _Command.reinitialize_command(self, command, reinit_subcommands)
vars(cmd).update(kw)
return cmd
def _find_all_simple(path):
"""
Find all files under 'path'
"""
results = (
os.path.join(base, file)
for base, dirs, files in os.walk(path, followlinks=True)
for file in files
)
return filter(os.path.isfile, results)
def findall(dir=os.curdir):
"""
Find all files under 'dir' and return the list of full filenames.
Unless dir is '.', return full filenames with dir prepended.
"""
files = _find_all_simple(dir)
if dir == os.curdir:
make_rel = functools.partial(os.path.relpath, start=dir)
files = map(make_rel, files)
return list(files)
class sic(str):
"""Treat this string as-is (https://en.wikipedia.org/wiki/Sic)"""
# Apply monkey patches
monkey.patch_all()