How do I import function from .pyx file in python?

question:

I'm trying to run Hadoopy, which has a file _main.pyx, and import _main is failing with module not found in __init__.py.

I'm trying to run this on OS X w/ standard python 2.7.


solution:

Add this code before you try to import _main:

import pyximport
pyximport.install()

Note that pyximport is part of Cython, so you'll have to install that if it isn't already.



报错了:2025-09-19 22:26:22.104 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.104 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.264 Warning: to view this Streamlit app on a browser, run it with the following command: streamlit run Z:\PythonProject_9_19_GBDT\CZD_9_19_RFR.py [ARGUMENTS] 2025-09-19 22:26:22.264 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.264 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.264 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.264 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.264 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.264 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.264 Session state does not function when running a script without `streamlit run` 2025-09-19 22:26:22.264 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.264 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-19 22:26:22.264 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. Traceback (most recent call last): File "Z:\PythonProject_9_19_GBDT\.venv\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc return self._engine.get_loc(casted_key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "pandas/_libs/index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas/_libs/hashtable_class_helper.pxi", line 7096, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'time' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "Z:\PythonProject_9_19_GBDT\CZD_9_19_RFR.py", line 78, in <module> processed_df = preprocess_data(df) ^^^^^^^^^^^^^^^^^^^ File "Z:\PythonProject_9_19_GBDT\CZD_9_19_RFR.py", line 27, in preprocess_data df['time'] = pd.to_datetime(df['time']) ~~^^^^^^^^ File "Z:\PythonProject_9_19_GBDT\.venv\Lib\site-packages\pandas\core\frame.py", line 4107, in __getitem__ indexer = self.columns.get_loc(key) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "Z:\PythonProject_9_19_GBDT\.venv\Lib\site-packages\pandas\core\indexes\base.py", line 3819, in get_loc raise KeyError(key) from err KeyError: 'time'
09-20
出现以下错误,请给出修改后完整的代码NameError Traceback (most recent call last) Cell In[23], line 181 178 print("未能获取到任何股票数据") 180 if __name__ == "__main__": --> 181 main() Cell In[23], line 142, in main() 139 financial_data = get_financial_data(stock_code, stock_name) 141 # 合并数据 --> 142 merged_data = merge_data(price_data, financial_data) 144 if merged_data is not None: 145 all_data.append(merged_data) Cell In[23], line 103, in merge_data(price_data, financial_data) 99 merged_data = price_data.copy() 101 if financial_data is not None and not financial_data.empty: 102 # 找到最近的财务报告日期 --> 103 merged_data['nearest_report_date'] = merged_data['date'].apply( 104 lambda x: find_nearest_report_date(x, financial_data['report_date']) 105 ) 107 # 合并行情和财务数据 108 financial_data = financial_data.rename(columns={'report_date': 'nearest_report_date'}) File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/series.py:4935, in Series.apply(self, func, convert_dtype, args, by_row, **kwargs) 4800 def apply( 4801 self, 4802 func: AggFuncType, (...) 4807 **kwargs, 4808 ) -> DataFrame | Series: 4809 """ 4810 Invoke function on values of Series. 4811 (...) 4926 dtype: float64 4927 """ 4928 return SeriesApply( 4929 self, 4930 func, 4931 convert_dtype=convert_dtype, 4932 by_row=by_row, 4933 args=args, 4934 kwargs=kwargs, -> 4935 ).apply() File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/apply.py:1422, in SeriesApply.apply(self) 1419 return self.apply_compat() 1421 # self.func is Callable -> 1422 return self.apply_standard() File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/apply.py:1502, in SeriesApply.apply_standard(self) 1496 # row-wise access 1497 # apply doesn't have a `na_action` keyword and for backward compat reasons 1498 # we need to give `na_action="ignore"` for categorical data. 1499 # TODO: remove the `na_action="ignore"` when that default has been changed in 1500 # Categorical (GH51645). 1501 action = "ignore" if isinstance(obj.dtype, CategoricalDtype) else None -> 1502 mapped = obj._map_values( 1503 mapper=curried, na_action=action, convert=self.convert_dtype 1504 ) 1506 if len(mapped) and isinstance(mapped[0], ABCSeries): 1507 # GH#43986 Need to do list(mapped) in order to get treated as nested 1508 # See also GH#25959 regarding EA support 1509 return obj._constructor_expanddim(list(mapped), index=obj.index) File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/base.py:923, in IndexOpsMixin._map_values(self, mapper, na_action, convert) 920 arr = self._values 922 if isinstance(arr, ExtensionArray): --> 923 return arr.map(mapper, na_action=na_action) 925 return algorithms.map_array(arr, mapper, na_action=na_action, convert=convert) File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/arrays/_mixins.py:81, in ravel_compat.<locals>.method(self, *args, **kwargs) 78 @wraps(meth) 79 def method(self, *args, **kwargs): 80 if self.ndim == 1: ---> 81 return meth(self, *args, **kwargs) 83 flags = self._ndarray.flags 84 flat = self.ravel("K") File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/arrays/datetimelike.py:763, in DatetimeLikeArrayMixin.map(self, mapper, na_action) 759 @ravel_compat 760 def map(self, mapper, na_action=None): 761 from pandas import Index --> 763 result = map_array(self, mapper, na_action=na_action) 764 result = Index(result) 766 if isinstance(result, ABCMultiIndex): File /home/ubuntu/miniconda3/lib/python3.13/site-packages/pandas/core/algorithms.py:1743, in map_array(arr, mapper, na_action, convert) 1741 values = arr.astype(object, copy=False) 1742 if na_action is None: -> 1743 return lib.map_infer(values, mapper, convert=convert) 1744 else: 1745 return lib.map_infer_mask( 1746 values, mapper, mask=isna(values).view(np.uint8), convert=convert 1747 ) File pandas/_libs/lib.pyx:2999, in pandas._libs.lib.map_infer() Cell In[23], line 104, in merge_data.<locals>.<lambda>(x) 99 merged_data = price_data.copy() 101 if financial_data is not None and not financial_data.empty: 102 # 找到最近的财务报告日期 103 merged_data['nearest_report_date'] = merged_data['date'].apply( --> 104 lambda x: find_nearest_report_date(x, financial_data['report_date']) 105 ) 107 # 合并行情和财务数据 108 financial_data = financial_data.rename(columns={'report_date': 'nearest_report_date'}) Cell In[23], line 122, in find_nearest_report_date(target_date, report_dates) 120 return None 121 valid_dates = report_dates[report_dates <= target_date] --> 122 return valid_dates.max() if not valid_empty_dates else report_dates.min() NameError: name 'valid_empty_dates' is not defined
09-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值