Hacker News new | ask | show | jobs
by mrwebmaster 2272 days ago
Thanks! I'm learning OOP and data analysis. For OOP I use Pycharm and for data analysis I use Jupyter Notebooks (in Lab or VS Code). Sometimes I write some reusable code in .py modules and call them from my notebooks. Anyway, I didn't fully undertand your proposal to use importlib.reload but will try to research about it. For exploratory data analysis, I guess notebooks are better that IDEs.
1 comments

Hi, I'm happy to try to help. At its most basic, don't copy paste your code into the notebook! And don't import your code like this `from mymodule import myfunction`. Instead import it like this:

  import mymodule

  mymodule.myfunction()
That allows you to do this to reload your module and pick up the updates to myfunction that you've made:

  from importlib import reload
  reload(mymodule)
However, how do you ensure that python can find your code so that `import mymodule` even works? Don't mess about with PYTHONPATH and sys.path. What you really want to do is house your work in its own python package. So, the milestones you want to get to are (not implying you don't already do these things!):

- Always use a virtualenv when working with python

- Create proper package structure for your python project. This means your directory structure will look like this

  myproject/myproject/__init__.py
  myproject/myproject/mymodule.py
  myproject/setup.py
- Google for how to create a minimal setup.py. Just put what you need in there, it's not much.

- Now, with your virtualenv activated, so that `which pip` resolves to `myvirtualenv/bin/pip`, do this:

  cd myproject
  pip install -e .
- That pip command will execute your setup.py and "install" your library into the virtualenv. But it will install it in such a way that you can edit the code and the edits will be picked up by the installed version (it uses symlinks).

- Now install jupyter in that same virtualenv and start your notebook. You should now be able to do `from myproject import mymodule` and `reload(mymodule)`. And your project is now a real python library so you can create subdirectories, etc e.g. `from myproject/plots import create_boxplot`.