diff --git a/quantitative_data/config.py b/quantitative_data/config.py index a5cbf68..745f6e8 100644 --- a/quantitative_data/config.py +++ b/quantitative_data/config.py @@ -15,14 +15,24 @@ from pathlib import Path from urllib.parse import quote_plus # 加载 .env 文件中的环境变量 +_LOADED = False try: from dotenv import load_dotenv - env_path = Path(__file__).parent / ".env" + # 使用 resolve() 获取绝对路径,避免 os.chdir() 改变工作目录后 __file__ 变为相对路径 + env_path = Path(__file__).resolve().parent / ".env" + # 如果 resolve 后的路径不存在,尝试从当前文件所在目录查找 + if not env_path.exists(): + # 备用方案:也检查 Notebook 的当前工作目录 + cwd_path = Path.cwd() / ".env" + if cwd_path.exists(): + env_path = cwd_path + if env_path.exists(): load_dotenv(dotenv_path=env_path, override=True) print(f"✓ 已加载环境变量文件: {env_path}") + _LOADED = True else: - print(f"⚠ 未找到 .env 文件: {env_path}") + print(f"⚠ 未找到 .env 文件。检查路径: {Path(__file__).resolve().parent / '.env'}, 当前目录: {Path.cwd() / '.env'}") except ImportError: print("⚠ python-dotenv 未安装,无法自动加载 .env 文件。请手动设置环境变量或执行: pip install python-dotenv") diff --git a/quantitative_data/importer.py b/quantitative_data/importer.py index a6aab08..923ab55 100644 --- a/quantitative_data/importer.py +++ b/quantitative_data/importer.py @@ -820,7 +820,9 @@ def init_database(): # 执行 DDL conn = get_pg_connection() try: - with open("schema.sql", "r", encoding="utf-8") as f: + import os as _os + schema_path = _os.path.join(_os.path.dirname(_os.path.abspath(__file__)), "schema.sql") + with open(schema_path, "r", encoding="utf-8") as f: ddl_sql = f.read() # 按分号分割,逐条执行 (忽略被注释掉的分区表DDL) diff --git a/quantitative_data/数据批量导入.ipynb b/quantitative_data/数据批量导入.ipynb index 3f204d2..73a6788 100644 --- a/quantitative_data/数据批量导入.ipynb +++ b/quantitative_data/数据批量导入.ipynb @@ -40,9 +40,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "工作目录: t:\\jupyter\\notebook\\quantitative_data\n", + "Python 版本: 3.10.2 (heads/master:d9999f5, Dec 16 2022, 16:20:32) [MSC v.1929 64 bit (AMD64)]\n" + ] + } + ], "source": [ "import sys\n", "import os\n", @@ -53,12 +62,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pandas 1.5.0\n", + "python-dotenv 1.2.2\n", + "SQLAlchemy 1.3.24\n", + "tushare 1.2.89\n", + "vnpy-tushare 1.2.85.1\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n", + "[notice] A new release of pip available: 22.2.2 -> 26.2\n", + "[notice] To update, run: python.exe -m pip install --upgrade pip\n" + ] + } + ], "source": [ "# 检查依赖包\n", - "!pip list | findstr -i \"tushare pandas psycopg2 sqlalchemy\"" + "!pip list | findstr -i \"tushare pandas psycopg2-binary sqlalchemy python-dotenv\"" ] }, { @@ -68,14 +98,22 @@ "outputs": [], "source": [ "# 如果需要安装依赖,取消注释下面这行\n", - "# !pip install -r requirements.txt" + "#!pip install -r requirements.txt" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "模块导入成功!\n" + ] + } + ], "source": [ "# 导入核心模块\n", "from importer import (\n", @@ -105,9 +143,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "✗ 连接失败: connection to server at \"192.168.27.11\", port 5438 failed: fe_sendauth: no password supplied\n", + "\n", + "请检查:\n", + " 1. Docker 容器是否已启动: docker ps | findstr postgres\n", + " 2. 环境变量 (.env) 中的连接参数是否正确\n", + " 3. 防火墙是否开放 5438 端口\n" + ] + } + ], "source": [ "# 测试数据库连接\n", "try:\n", @@ -625,7 +676,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -639,7 +690,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.10.2" } }, "nbformat": 4,