Python 输入错误:无法输入名称“;功能“;从「;模块.py“;

Python 输入错误:无法输入名称“;功能“;从「;模块.py“;,python,pandas,dataframe,nlp,Python,Pandas,Dataframe,Nlp,在Jupyter笔记本中: 回购: 模块:featurestorage.py 主:preprocessing.ipynb 我在featurestorery.py中有几个函数,我需要导入其中的大部分,其中一些函数,特别是我之前编写的函数,在导入它们时起作用。然而,较新的产品不希望被进口,因为这会给人一种不纯的恐惧感 我检查并在必要时进行了更改,在模块文件中导入了必要的模块,我尝试将特定于某个函数的导入放在函数定义中,但没有修复任何问题,我尝试不单独导入函数,而是在每个函数前面添加“fs.”,以

在Jupyter笔记本中: 回购:

  • 模块:featurestorage.py
  • 主:preprocessing.ipynb
我在featurestorery.py中有几个函数,我需要导入其中的大部分,其中一些函数,特别是我之前编写的函数,在导入它们时起作用。然而,较新的产品不希望被进口,因为这会给人一种不纯的恐惧感

我检查并在必要时进行了更改,在模块文件中导入了必要的模块,我尝试将特定于某个函数的导入放在函数定义中,但没有修复任何问题,我尝试不单独导入函数,而是在每个函数前面添加“fs.”,以引用功能库作为fs

功能和模块:

# # Feature Engineering #

import pandas as pd
import numpy as np
import spacy
import re
import string
from statistics import mean
from collections import Counter 
一些不起作用的功能:

# Phrasal complexity
## reference to Martinez (2018)

def nr_np(series):
    """Calculates nr of noun chunks in the texts."""
    nlp = spacy.load("en_core_web_sm")
    text = nlp(series)
    nps = []
    for nc in text.noun_chunks:
        nps.append(np)
    return len(nps)

def count_np(df, word_count_column, np_count_column):
    """Divides number of words by number of noun phrases."""
    try:
        rel_np = df[word_count_column]/df[np_count_column]
        return rel_np
    except 'ZeroDivisionError':
        return '0'

def count_np_words(df, np_count_column, word_count_column):
    """Divides number of noun phrases by number of words."""
    try:
        rel_np = df[np_count_column]/df[word_count_column]
        return rel_np
    except 'ZeroDivisionError':
        return '0'
我如何在我的主笔记本中实现它们:

# Adding Custom Feature Columns
from featurestorehouse import avg_sentlength, avg_wordlength, nr_deps, count_deps, nr_verbs, count_verbs, nr_words, nr_sents, normalize, normalize_fix, nr_np, count_np, count_np_words, nr_clauses, rel_nr_clauses, rel_nr_words_clauses, unique_words, lexical_variety

## extract relevant column as series
series = df['fulltext'].astype(str)

# Sentence Complexity

## sentence length
df['sentence_cnt'] = series.apply(lambda x: nr_sents(x))

## average sentence length
df['avg_sentlength'] = series.apply(lambda x: avg_sentlength(x))


## POS distributions
### extract relevant column as series
series = df['POS'].astype(str)

### apply function
df['VERB_cnt'] = series.apply(lambda x: nr_verbs(x))
fs.count_verbs(df, 'TAG', 'VERB_cnt')


# Phrasal Complexity

## NP distributions
### extract relevant column as series
series = df['fulltext'].astype(str)

### apply function
df['NP_cnt'] = series.apply(lambda x: nr_np(x))
df['np_rel_cnt'] = count_np(df, 'word_cnt', 'NP_cnt')
df['np_rel_word_cnt'] = count_np_words(df, 'NP_cnt', 'word_cnt')
当我将featurestore.py导入为fs并添加fs时,在“#####apply function”下面的最后一个集合给出了一个错误。到主笔记本中的每个函数(显然,当我分别从featurestorery.py导入每个函数时,它没有达到这一点)

错误:

ImportError                               Traceback (most recent call last)
<ipython-input-18-8ad058b0250d> in <module>
      1 # Adding Custom Feature Columns
----> 2 from featurestorehouse import avg_sentlength, avg_wordlength, nr_deps, count_deps, nr_verbs, count_verbs, nr_words, nr_sents, normalize, normalize_fix, nr_np, count_np, count_np_words, nr_clauses, rel_nr_clauses, rel_nr_words_clauses, unique_words, lexical_variety
      3 
      4 ## extract relevant column as series
      5 series = df['fulltext'].astype(str)

ImportError: cannot import name 'nr_np' from 'featurestorehouse' (C:\Users\tdems\Jupyter\CEFR_AES_dissertation\featurestorehouse.py)
ImportError回溯(最近一次调用)
在里面
1#添加自定义要素列
---->2从功能库导入avg_sentlength、avg_wordlength、nr_deps、count_deps、nr_动词、count_动词、nr_单词、nr_sents、normalize、normalize_fix、nr_np、count_np、count_np_单词、nr_子句、rel_nr_子句、rel_单词_子句、唯一_单词、词汇变化
3.
4##将相关列提取为系列
5系列=df['fulltext'].astype(str)
ImportError:无法从“FeatureStore”导入名称“nr\u np”(C:\Users\tdems\Jupyter\CEFR\u AES\u deposition\featureStore.py)

有人知道这是怎么回事吗?据我所知,没有涉及循环依赖关系。回购协议中还有很多其他笔记本,但到目前为止,这从来都不是问题。

确保您的笔记本在每次更改后始终加载最新版本,而不是缓存版本:

在笔记本顶部-导入之前-输入:

%load\u ext autoreload

在下面的新单元格中:

%autoreload 2


然后重新启动内核,再试一次。

如果它不工作,Jupyter中的一个简单方法是使用

%run Path_To_The_Other_File.ipynb

它将只执行路径引用的完整Jupyter文件,因此如果您定义了一个函数,现在将在主文件中知道它

重新启动并添加代码行已修复该问题,谢谢。现在我也知道下次该做什么了。对不起,我第一次:)