23
loading...
This website collects cookies to deliver better user experience
pip install flybypy
cd ~/dotfiles
mkdir ipython
touch ipython/.pyflyby
stow ipython
Seriously don't sleep on the stow.
pyflyby
is configured simply by putting all of your import statements that you want to automatically import into your ~/.pyflyby
file. You can import pandas
, from pandas import DataFrame
, or even import pandas as pd
, and all of these will work pretty much as expected.# comments start with a #
# import your favorite libraries
import visidata as vd
import fsspec
import difflib
import s3fs
import seaborn as sns
import plotly
# also supports from imports
from rich.layout import Layout
from rich.live import Live
# duplicates are allowed
import plotly
import plotly
# duplicate names from different libraries are not allowed
import copy
from numpy import copy
Add all the things you would like to be imported automatically, just as you would import them. I went kinda crazy and added over 200 to mine based on packages that I use.
from pandas import DataFrame, Series
Even imports with a comma will be treated separately.
flybypy
from the docs is to run the following magic command. This works well, but I wan even less typing, I want pyflyby automatically installed and importing things without me even thinking about it.%load_ext pyflyby
from IPython import get_ipython
import subprocess
ipython = get_ipython()
try:
ipython.magic("load_ext pyflyby")
except ModuleNotFoundError:
print("installing pyflyby")
subprocess.Popen(
["pip", "install", "pyflyby"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
).wait()
ipython.magic("load_ext pyflyby")
Note: if installation fails you will still make it into ipython, there will just be a traceback to the failed command as you enter.
Check out this article for a bit more in depth ipython configuration
df = pd.read_csv("https://waylonwalker.com/cars.csv")
[PYFLYBY] import pandas as pd
?
, ??
, or help
and pyflyby will import it for you.Popen?
this article has more ways to get help in ipython.
Pop
it will complete to Popen
without even adding Popen
to your local namespace. If you ask for something inside of a module i.e. requests.<tab>
, then it will import requests.# does not populate the namespace
Pop<tab>
# !!does populate the local namespace
requests.<tab>
ModuleNotFoundError
when it tries to import and it will not import or try to install for you. You will have to change environments or install that module.❯ pd?
[PYFLYBY] import pandas as pd
[PYFLYBY] Error attempting to 'import pandas as pd': ModuleNotFoundError: No module named 'pandas'
[PYFLYBY] Traceback (most recent call last):
[PYFLYBY] File "/home/u_walkews/.local/lib/python3.8/site-packages/pyflyby/_autoimp.py", line 1610, in _try_import
[PYFLYBY] exec_(stmt, scratch_namespace)
[PYFLYBY] File "<string>", line 1, in <module>
[PYFLYBY] ModuleNotFoundError: No module named 'pandas'
Object `pd` not found.
❯ df = pd.read_csv("https://waylonwalker.com/cars.csv")
<ipython-input-3-69b040434562>:1 in <module>
NameError: name 'pd' is not defined
function! s:PyPreSave()
Black
endfunction
function! s:PyPostSave()
execute "silent !tidy-imports --black --quiet --replace-star-imports --action REPLACE " . bufname("%")
execute "e"
endfunction
:command! PyPreSave :call s:PyPreSave()
:command! PyPostSave :call s:PyPostSave()
augroup waylonwalker
autocmd!
autocmd bufwritepre *.py execute 'PyPreSave'
autocmd bufwritepost *.py execute 'PyPostSave'
autocmd bufwritepost .tmux.conf execute ':!tmux source-file %'
autocmd bufwritepost .tmux.local.conf execute ':!tmux source-file %'
autocmd bufwritepost *.vim execute ':source %'
augroup end
~/.pyflyby
will get automatically imported if it is used within the file/console. If you are working in a file, and stop using a module, it will automatically get removed.pyflyby
does not sort imports into paragraphs or by category. When it needs to add new imports. It will find the last paragraph of imports in your file, add the new one, and sort that paragraph alphebetically.from collections import Counter
import requests
from plugins.custom_seo import post_render
# <-- pyflyby will put the import here
function! s:PyPostSave()
execute "silent !tidy-imports --black --quiet --replace-star-imports --action REPLACE " . bufname("%")
execute "silent !isort " . bufname("%")
execute "e"
endfunction
def get():
"""
Get all the posts from waylonwalker.com.
Yes theres an rss feed, you should be subscribed if your not already.
Oh, and we don't need no stinkin error handing because it's always live
"""
r = requests.get("https://waylonwalker.com/rss")
return r.content
import requests
def get():
"""
Get all the posts from waylonwalker.com.
Yes theres an rss feed, you should be subscribed if your not already.
Oh, and we don't need no stinkin error handing because it's always live
"""
r = requests.get("https://waylonwalker.com/rss")
return r.content
__all__
list of all the unused things that are imported according to pep8. Pyflyby will remove any unused modules unless they are in the __all__
list.# snippet from kedro.extras.datasets.pandas
__all__ = [
"CSVDataSet",
"ExcelDataSet",
"FeatherDataSet",
"GBQTableDataSet",
"ExcelDataSet",
"AppendableExcelDataSet",
"HDFDataSet",
"JSONDataSet",
"ParquetDataSet",
"SQLQueryDataSet",
"SQLTableDataSet",
]
py help pd
py help pd.DataFrame
py pd.read_csv 'https://waylonwalker.com/cars.csv'