module not found in a custom python package - python-import

I am trying to make a package in Python. I have the following file and directory structure:
.
├── ds
│ ├── __init__.py
│ ├── __main__.py
│ ├── package_a
│ │ ├── __init__.py
│ │ └── package_a_b
│ │ └── __init__.py
│ └── settings.py
├── install.sh
├── LICENSE
├── Manifest.in
├── README.md
└── setup.py
The code is the following:
ds/__init__.py
Empty file
ds/__main__.py
from package_a import name_a
from package_a.package_a_b import name_a_b
from settings import config
def main():
print(name_a)
print(config)
ds/package_a/__init__.py
name_a = 'name_a'
ds/package_a_b/__init__.py
name_a_b = 'name_a_b'
setup.py
import pathlib
from setuptools import setup
HERE = pathlib.Path(__file__).parent
README = (HERE / "README.md").read_text()
setup(
name="ds",
version="2.0.0",
long_description=README,
long_description_content_type="text/markdown",
license="MIT",
classifiers=[
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
],
packages=["ds"],
#packages=find_packages(exclude=("tests",)),
include_package_data=True,
entry_points={
"console_scripts": [
"ds=ds.__main__:main",
]
},
)
In order to install I following these steps
rm -rf dist
pip uninstall ds
python setup.py bdist_wheel
pip install dist/ds-2.0.0-py3-none-any.whl --force-reinstall
ds
the problem is that when I execute ds I got the following message
ModuleNotFoundError: No module named 'settings'
if I comment all about settings then I get this error:
ModuleNotFoundError: No module named 'package_a'
So python is not finding the packages.
How can I solve this?

Your package inside __main__.py is available under the name ds.
from ds.package_a import name_a
from ds.package_a.package_a_b import name_a_b
from ds.settings import config

Related

list all bazel targets in the directory

How can I put all targets from a package into one variable in a higher-level package?
I'm trying to create a simulation framework with the following architecture:
.
├── run_simulation.py
├──data
| ├── folder1
| │ ├── file_1.cvs
| │ ...
| │ └── file_k.csv
| ├── folder2
| │ ├── file_1.cvs
| │ ...
| │ └── file_k.csv
| └── BUILD
└── BUILD
the data/BUILD file consists of:
data_dirs = [
"folder1",
"folder2"
]
[
filegroup(
name = "my_cool_data_" + data_dir,
srcs = glob(["{}/*.csv".format(data_dir)]),
...
) for data_dir in data_dirs
]
and produce a target with experimental data per a folder.
I'd like to run the script run_simulation.py with inputs from folders in the data directory with a command like:
$ bazel run run_simulation_<data target name>
so I have to add to the ./BUILD file
data_targets=[*do smth to get all targets from data/BUILD file*]
[
py_binary(
name = "run_simulation_" + data_target,
...
data = ["//data:{}".format(data_target)],
...
) for data_target in data_targets
]
The problem is how can I put all targets from the data package into a variable in the ./BUILD file? I couldn't find a way to do this in the official documentation.

Bazel. Is there any way of running script under the same directory without `--run_under`?

Summary
I'd like to reduce the number of types when formatting.
Status quo
I am using Bazel to manage C++ project. Below is the simplified structure of the project.
❯ tree
.
├── bin
│ ├── BUILD.bazel
│ └── format.sh
├── README.md
├── src
└── WORKSPACE
Now, I'd like to format all files in src (off course, I have test in my real project) by bin/format.sh.
However, it really bothers me to type the long command below. Do you know how to make it easier?(If it is possible to change the command tobazel run bin:format, that's perfect.)
I think adding some codes in bin/BUILD.bazel would help, but I don't have any idea.
bazel run --run_under="cd $PWD &&" bin:format # format source codes
contents of files
sh_binary(
name = "format",
srcs = ["format.sh"],
)
#!/usr/bin/env sh
buildifier -r .
find . -iname *.h -o -iname *.cc | xargs clang-format -i -style=Google
I think what you are doing is fine. I would just define an alias as in
alias clang-fmt='bazel run --run_under="${PWD}" //bin:format'
You could also not use the --run_under option, and pass the directory to the program:
alias clang-fmt='bazel run //bin:format -- "${PWD}"'
and update the script
find $1 -iname *.h -o -iname *.cc | xargs clang-format -i -style=Google

View changes in custom Rails generator

I'd like to view the affected files and/or changes that will be made prior to running a method in my custom Rails generator. I've looked through the docs for days and am beginning to think its not possible.
module Mygem
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
def copy_theme_files_to_app
directory( source_paths[0] + "/mytemplate", Dir.pwd)
end
end
end
end
In the example above I'm trying to copy the contents of a template directory into the destination app.
├── lib
│ ├── generators
│ │ ├── mygem
│ │ │ ├── templates
│ │ │ │ ├── mytemplate
│ │ │ │ │ ├── app
│ │ │ │ │ │ ├── assets
│ │ │ │ │ │ │ ├── stylesheets
│ │ │ │ │ │ │ │ ├── application.scss
│ │ │ │ │ │ │ │ ├── custom.scss
Here are the contents of the "mytemplate" directory inside of my gem to give a little context. What I'm hoping to see inside of the generators copy_theme_files_to_app method is either an array of new paths to be generated/destroyed or showing the potential conflict between my template's application.scss file and the one in the app.
Is this possible?

waf cross-project dependencies

I have a trivial waf project:
$ root
|-- a
| `-- wscript
|-- b
| `-- wscript
`-- wscript
Root wscript is
def configure(conf):
pass
def build(bld):
bld.recurse('a b')
a wscript is
def build(bld):
bld (rule = 'touch ${TGT}', target = 'a.target' )
b wscript is
def build(bld):
bld (rule = 'cp ${SRC} ${TGT}', source='a.target', target='b.target')
What I'm trying to achieve is have a build system that first touches a.target, then copies it to b.target. I want to have rules for a.target and b.target to stay in their respective wscript files.
When I'm trying to launch it, I get the following error instead:
source not found: 'a.target' in bld(target=['b.target'], idx=1, tg_idx_count=2, meths=['process_rule', 'process_source'], rule='cp ${SRC} ${TGT}', _name='b.target', source='a.target', path=/<skip>/waf_playground/b, posted=True, features=[]) in /<skip>/waf_playground/b
If I put both rules into a single wscript file, everything works like a charm.
Is there a way for a target to depend on a another target defined in other wscript?
When you specify source/target, that is expressed relative to the current wscript file.
$ waf configure build
...
source not found: 'a.target' in bld(source='a.target, ...)
...
$ tree build
build/
├── a
│ └── a.target
...
Knowing that, the fix is to refer to the a.target source file correctly in b/wscript:
def build(bld):
bld (rule = 'cp ${SRC} ${TGT}', source='../a/a.target', target='b.target')
The task now correctly finds the source file:
$ waf build
Waf: Entering directory `.../build'
[1/2] Creating build/a/a.target
[2/2] Compiling build/a/a.target
Waf: Leaving directory `.../build'
'build' finished successfully (0.055s)
$ tree build
build/
├── a
│   └── a.target
├── b
│   └── b.target
...

angular-rails-templates ng-include infinite digest

I'm using angular-rails-templates and trying to use ng-include each time I try to use it, I get an infinite digest.
My folder structure is as follows:
├── app
│ ├── assets
│ │ ├── javascripts
│ │ ├── templates
│ │ | ├── include.html
| ├── views
│ │ ├── layouts
│ │ | ├── application.html.erb
application.html.erb
<div ng-include="'assets/templates/include.html'"></div>
I've tried all sorts of variations, including no single quotes, just include.html, templates/include.html but they all throw me into an infinite digest. Am I missing something simple?

Resources