Closed rmcgibbo closed 4 years ago
Thank you for contributing both the idea and a solution! And thanks for checking out pyp in the first place! This is definitely a use case pyp should support.
I'm going to need a little time to think about this, because I wouldn't want to add this feature and then change how it works in backwards incompatible ways. Some of the things that I'm thinking of:
One of pyp's goals is to feel very much like writing Python, and it would be cool if this extended to configuration files. For instance, if configuration just looked like:
import numpy as np
from scipy.linalg import eigvals
from pandas import *
I imagine users might want to be able to define custom functions too, which ideally they could just add to this file and it would work. Aside from import *
, we could make this work by just copying and pasting the code from the configuration file, but I think we could maybe use the configuration file as a source of undefined names, and then just define the ones we need on demand (this might need to be recursive).
And thanks for adding shebangs to --script
! Maybe that should just be the default and doesn't need to be configurable?
Yeah, I think it definitely makes sense to take this slow and try to get it right first.
The kind of config format you suggested would be really neat! And especially if it can be statically analyzed so that those imports aren't executed unless they're necessary (as in the current design). I agree the from pandas import *
could be tricky.
As for the shebangs, we use a kind of strange custom shebangs in my organization to manage environments, so I would like the opportunity to customize the shebang, but I can leave that on my fork -- it's kind of an esoteric/rare thing.
I'm a big fan of your idea for the format of the config file, @hauntsaninja. I'm going to take a stab at trying to implement it.
Sure! You'll probably need to pull in the latest commit. It fixes a bug in find_names
that is relevant.
Closing this, since I just cut a release with #13 merged. Thanks again!
Hi,
I think this tool is really really cool. And the code is really clean and straightforward. Impressive work. This PR adds a little feature in order to help use
pyp
more efficiently in an environment where there are a bunch of libraries outside the standard library that are widely used. For example, in my workplace we usenumpy
a lot, and everyone importsnumpy
asnp
, so I want to be able to usenp.<function>
in my scripts and get pyp's awesome auto-imports.So, I've added support for a config file, loaded via the environment variable
PYPCONFIG
, and then the config file can add to the subimport list, add module aliases (like np -> numpy), and customize the shebang used in--explain
mode.Let me know what you think!