beancount / beanprice

Daily price quotes fetching library for plain-text accounting
GNU General Public License v2.0
66 stars 37 forks source link

Question: How to create a python package for a price source? #18

Closed blais closed 3 years ago

blais commented 6 years ago

Original report by vonpupp (Bitbucket: vonpupp, GitHub: vonpupp).


Hi,

I have created a custom price source that seems to work fine, however I would like to be able to create a package for it so it can be easily distributed and maintained without messing with beancount source code.

Currently I have the following package skeleton (I used cookicutter)

.
├── beancount_cryptocompare
│   ├── cryptocompareusd.py
│   └── __pycache__
│       ├── cryptocompareusd.cpython-36.pyc
│       └── __init__.cpython-36.pyc
├── beancount_cryptocompare.egg-info
│   ├── dependency_links.txt
│   ├── not-zip-safe
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── ...
├── setup.cfg
├── setup.py

When I run a python shell and import beancount_cryptocompare.cryptocompareusd it doesn't complain, however when I use the namespace on the price attribute it doesn't work, example:

(.env3) > $ bean-price -e USD:cryptocompareusd/ETH --no-cache                                                                                                                     
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/beancount/prices/find_prices.py", line 147, in import_source
    __import__(default_name)
ModuleNotFoundError: No module named 'beancount.prices.sources.cryptocompareusd'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/beancount/prices/find_prices.py", line 151, in import_source
    __import__(module_name)
  File "/home/av/repos/crypto/beancount_cryptocompare/beancount_cryptocompare/cryptocompareusd.py", line 10, in <module>
    import cryptocompare
ModuleNotFoundError: No module named 'cryptocompare'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/bean-price", line 2, in <module>
    from beancount.prices.price import main; main()
  File "/usr/lib/python3.6/site-packages/beancount/prices/price.py", line 310, in main
    args, jobs, entries = process_args()
  File "/usr/lib/python3.6/site-packages/beancount/prices/price.py", line 281, in process_args
    psource_map = find_prices.parse_source_map(source_str)
  File "/usr/lib/python3.6/site-packages/beancount/prices/find_prices.py", line 99, in parse_source_map
    for source_str in source_strs.split(','))
  File "/usr/lib/python3.6/site-packages/beancount/prices/find_prices.py", line 99, in <genexpr>
    for source_str in source_strs.split(','))
  File "/usr/lib/python3.6/site-packages/beancount/prices/find_prices.py", line 124, in parse_single_source
    module = import_source(short_module_name)
  File "/usr/lib/python3.6/site-packages/beancount/prices/find_prices.py", line 155, in import_source
    module_name, exc))
ImportError: Could not find price source module "cryptocompareusd": No module named 'cryptocompare'

If I try with the full namespace, it doesn't work either, example:

av@sa4 ~/repos/crypto/beancount_cryptocompare                                                                                                                           [20:54:15] 
(.env3) > $ bean-price -e USD:beancount_cryptocompare.cryptocompareusd/ETH --no-cache                                                                                             
usage: bean-price [-h] [-e] [-v] [-d DATE] [-i] [-u] [-c] [-a] [-s] [-n]
                  [--cache CACHE_FILENAME] [--no-cache] [--clear-cache]
                  sources [sources ...]

From what I understood the find_prices works as some sort of plugin importer or similar, is that right?

IMHO it would be very desirable not to mess with PYTHONPATH nor beancount source. Would that be possible?

Could you please help me?

Thank you very much.

blais commented 6 years ago

Original comment by Justus Pendleton (Bitbucket: hoostus, GitHub: hoostus).


Using the full namespace should work. (It works for me.) What happens when you turn up the verbosity (-v -v)?

blais commented 4 years ago

This isn't a Beancount issue, nor a P0.