Want to save and run a Python script in a Jupyter laptop (Anaconda)

Another newbie for python. I am currently using a Jupypter laptop as part of anaconda.

To continue my projects with the iPython Notebook,

I need to run some of my python scripts (tp.py file) on a laptop.

from tp import wordtoplural  

Since this makes life easier, and does not define the entire function in the laptop itself.

How can I do this, importerrors are currently happening in my code.

ImportError: cannot import name wordtoplural

  • IPython and python script (. Py) laptops are in the same folder.
  • An empty __init.py__file has been added to this directory .
+4
source share
2 answers

, ipython , python script. , __init__.py , python script, .

, , python script , autoreload, , python:

%load_ext autoreload
%autoreload 2

, .

, IPython (% magics):

from __future__ import absolute_import

: , , , , : , ! , ( ), , ( !). , , , .

, super() , , , .

+5

, , .

Jupyter Notebook python script.

ipython, [. ipynb file]

,

.py .

%%writefile textproc.py

def plural(word):
    if word.endswith('y'):
        return word[:-1] + 'ies'
    elif word[-1] in 'sx' or word[-2:] in ['sh', 'ch']:
        return word + 'es'
    elif word.endswith('an'):
        return word[:-2] + 'en'
    else:
        return word + 's'

, .py .

from textproc import plural
plural('wish')

, .py , ( .. ..)

+2

Source: https://habr.com/ru/post/1611305/


All Articles