How to inculde one group of optional-dependencies in another? - python

If I have 2 groups of project.optional-dependencies in my pyproject.toml, is there a way to specify that installing one group installs the dependencies of the other?
E.g.
[project.optional-dependencies]
test = [
"pytest",
"pytest-asyncio",
"pytest-cov",
]
dev = [
"flake8",
"flake8-import-order",
"black",
]
How can I specify that installing myproj[dev] also installs [test].

Not sure from which pip version this is possible, on 22.2.2 it is, on 20.0.2 it isn't.
[project]
name = "my-pkg"
[project.optional-dependencies]
test = [
"pytest",
"pytest-asyncio",
"pytest-cov",
]
dev = [
"flake8",
"flake8-import-order",
"black",
"my-pkg[test]"
]
source => https://hynek.me/articles/python-recursive-optional-dependencies/

Related

how to automatically generate mypy stubs using pyproject.toml

i have a package and in it i use pyproject.toml
and for proper typing i need stubs generated, although
its kinda annoying to generate them manually every time,
so, is there a way to do it automatically using it ?
i just want it to run stubgen and thats it, just so
mypy sees the stubs and its annoying seeing linters
throw warnings and you keep having to # type: ignore
heres what i have as of now, i rarely do this so its probably
not that good :
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"
[project]
name = "<...>"
authors = [
{name = "<...>", email = "<...>"},
]
description = "<...>"
readme = "README"
requires-python = ">=3.10"
keywords = ["<...>"]
license = {text = "GNU General Public License v3 or later (GPLv3+)"}
classifiers = [
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Programming Language :: Python :: 3",
]
dependencies = [
"<...>",
]
dynamic = ["version"]
[tool.setuptools]
include-package-data = true
[tool.setuptools.package-data]
<...> = ["*.pyi"]
[tool.pyright]
pythonVersion = "3.10"
exclude = [
"venv",
"**/node_modules",
"**/__pycache__",
".git"
]
include = ["src", "scripts"]
venv = "venv"
stubPath = "src/stubs"
typeCheckingMode = "strict"
useLibraryCodeForTypes = true
reportMissingTypeStubs = true
[tool.mypy]
exclude = [
"^venv/.*",
"^node_modules/.*",
"^__pycache__/.*",
]
thanks for the answers in advance
just make a shellscript and add it to pyproject.toml as a script
:+1:

Create a Python entry point manually

In setup.py I can put something like this:
import setuptools
setuptools.setup(
name = "my_project",
version = "1.2.3.4",
packages = [ "my_project" ],
entry_points = {
"console_scripts": [
"my_project = my_project.__main__:main" ] } )
To create an entry point/executable, my_project, that I can call from the console.
Is it possible to create these entry points manually, in a normal Python script, outside of setup.py?
(I'm interested in creating these the same way setup.py does, so not using system-specific hashbang scripts, etc.)

bazel py_proto_library is not defined

BUILD:
cc_proto_library(
name = "yd_fieldoptions_cc",
deps = [":yd_fieldoptions"],
)
py_proto_library(
name = "yd_fieldoptions_py",
deps = [":yd_fieldoptions"],
)
proto_library(
name = "yd_fieldoptions",
srcs = ["yd_fieldoptions.proto"],
deps = [
"#com_google_protobuf//:descriptor_proto",
],
)
Error
bazel build -s //field_options:yd_fieldoptions_py
BUILD:11:1: name 'py_proto_library' is not defined (did you mean 'cc_proto_library'?)
version:
Build label: 0.14.0- (#non-git)
protobuf verson: 3.5.0
You might be thinking of this rule: https://github.com/google/protobuf/blob/master/protobuf.bzl
In order to use it you have to load the bzl file in the BUILD file: https://docs.bazel.build/versions/master/skylark/concepts.html#loading-an-extension
The implementation of py_proto_library has some hacks related to it.
Some of the toolchain/library references are only valid inside the Protobuf repository. In order to use the rule py_proto_library, you have to manually bind those references in your own repository.
I have a very rough example that demonstrates how to bind some (but definitely not all) of those references in order to make py_proto_library work in your repository.
You can checkout the example here.
This is a very rough implementation, though it works, I don't have any idea whether this will work with a more complex scenario.
You have been warned.
However, if you really really want to make things work.
You can invoke the Protobuf compiler directly, then export the generate Python file to a py_library.
This is guaranteed to work, though this requires more code.
# This generates the Protobuf Python code using the protoc compiler
genrule(
name = "yd_fieldoptions_compiled_python",
srcs = ["yd_fieldoptions.proto"],
outs = ["yd_fieldoptions_pb2.py"],
cmd = "$(location #com_google_protobuf//:protoc) -I=proto --python_out=$(#D) $<",
tools = ["#com_google_protobuf//:protoc"],
)
# Setup a py_library target to be used by your code.
py_library(
name = "yd_fieldoptions_py",
srcs = [":yd_fieldoptions_compiled_python"],
deps = [
"#protobuf_python",
"#pypi_six//:six",
],
)
Also, you have to include the following info in your WORKSPACE file.
Those are used to download the necessary dependencies, you might have to change those URL as well as versions for Protobuf-Python according to your need.
new_http_archive(
name = "pypi_six",
url = "https://pypi.python.org/packages/16/d8/bc6316cf98419719bd59c91742194c111b6f2e85abac88e496adefaf7afe/six-1.11.0.tar.gz",
build_file_content = """
py_library(
name = "six",
srcs = ["six.py"],
visibility = ["//visibility:public"],
)
""",
strip_prefix = "six-1.11.0",
)
new_http_archive(
name = "protobuf_python",
url = "https://pypi.python.org/packages/14/03/ff5279abda7b46e9538bfb1411d42831b7e65c460d73831ed2445649bc02/protobuf-3.5.1.tar.gz",
build_file_content = """
py_library(
name = "protobuf_python",
srcs = glob(["google/protobuf/**/*.py"]),
visibility = ["//visibility:public"],
imports = [
"#pypi_six//:six",
],
)
""",
strip_prefix = "protobuf-3.5.1",
)
BTW, the code included above does not have gRPC plugin included.
If you are looking for a gRPC enabled Protobuf library, you have to include the gRPC repo, then include necessary the plugin in the corresponding rule.

Bazel build package not found

I'm trying to run Tensorflow code downloaded from github tensorflow/models/adversarial_text, but running into a bazel build error. The error looks quite straightforward. But as I haven't used bazel very much before, I'd appreciate any ideas/suggestions about it. The error is below:
ERROR: /home/dasgupta/adversarial_text/BUILD:60:1: no such package 'adversarial_text/data': BUILD file not found on package path and referenced by '//:inputs'.
Inside adversarial_text/BUILD:(line 60 - that gives above error) is the following rule:
py_library(
name = "inputs",
srcs = ["inputs.py"],
deps = [
# tensorflow dep,
"//adversarial_text/data:data_utils",
],
}
But I see that there is a directory called "adversarial_text/data" and inside adversarial_text/data/BUILD there's this rule too:
py_library(
name = "data_utils",
srcs = ["data_utils.py"],
deps = [
# tensorflow dep,
],
)
I tried adding
visibility = ["//adversarial_text:__pkg__"],
right after the deps rule for data_utils, but that didn't solve the problem.
Any ideas what I might be missing here, or what I might need to set/change (environment vars?) to get this to work.
My config: bash on Ubuntu 16.04, Tensorflow 1.2, bazel 0.5 and python 2.7
The visibility has to be //:__pkg__ since adversarial_text is the root of your workspace. And you should try building //:inputs.
So to summarize, this is what I did to make it work, after cloning the project.
1 Create "WORKSPACE" file in adversarial_text/
touch WORKSPACE
2 Edit deps in adversarial_text/BUILD
py_library(
name = "inputs",
srcs = ["inputs.py"],
deps = [
# tensorflow dep,
"//data:data_utils",
],
)
py_test(
name = "graphs_test",
size = "large",
srcs = ["graphs_test.py"],
deps = [
":graphs",
# tensorflow dep,
"//data:data_utils",
],
)
3 add visibility for data_utils in adversarial_text/data/BUILD
py_library(
name = "data_utils",
srcs = ["data_utils.py"],
deps = [
# tensorflow dep,
],
visibility = ["//:__pkg__"],
)
This should be fixed now, running the code no longer requires bazel as of https://github.com/tensorflow/models/pull/3414

How to pass commands to gcc through cx_Freeze

How to pass commands to gcc through cx_Freeze 'disutils.core.setup()' arguments?
Specifically, i want my .exe file to use relative paths in traceback messages, rather than the path where i build .exe file
Here is my setup.py file:
setup(
name="test",
packages=['test'],
package_data={'': ['*.py', '*.txt', '*.sample', '*.mo', 'README.rst']},
options={"build_exe": {
"icon": r"test\resources\test.ico",
"compressed": True,
"create_shared_zip": True,
"copy_dependent_files": True,
"include_files": [
('test/i18n/', 'i18n/'),
('test/resources/', 'resources/'),
('test/client.conf.sample', 'client.conf.sample'),
],
"excludes": [
'urllib.sys',
'urllib._sre',
'urllib.array',
'urllib._locale',
'urllib.datetime',
'urllib._functools',
]
}
},
executables=Executable(script=script),)
You need to add one additional option to the ones you already have:
replace_paths = [("*", "")]
That will replace all paths with relative paths. You can also do more interesting things like:
replace_paths = [
("/path/to/python/lib", "<Python>"),
("/path/to/my/script", "<Script>")
]
In essence, the first element of the tuple is the part of the path which is to be replaced with the value in the second element of the tuple. A value of * in the search value results in all paths being replaced with the replacement value.

Categories

Resources