在本地运行Python Azure Function的VSCode失败,出现ModuleNotFoundError

时间:2019-11-13 17:39:49

标签: python visual-studio-code azure-functions

我正在运行最近在macOS 10.14.6上升级的VSCode 1.40.0,并且在venv中使用了Python 3.6.5。在升级之前,我的代码可以正常工作(前一周,即我上次对其进行一些处理的时间)。此工作空间是一组Azure函数,具有用于运行它的以下launch.json配置:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to Python Functions",
      "type": "python",
      "request": "attach",
      "port": 9091,
      "preLaunchTask": "func: host start",
      "logToFile": true,
    }
  ]
}

目录结构为:

.
-- env (python env)
-- SharedCode
---- FunctionApp.py
-- TLoginEvents
---- tlogins.py

在VSCode中运行代码时,出现以下ModuleNotFoundError:

[11/13/19 5:17:08 PM] Executed 'Functions.TLoginEvents' (Failed, Id=c6e6b342-4250-4041-8d19-8ce4ae8bb736)
[11/13/19 5:17:08 PM] System.Private.CoreLib: Exception while executing function: Functions.TLoginEvents. System.Private.CoreLib: Result: Failure
[11/13/19 5:17:08 PM] Exception: ModuleNotFoundError: No module named 'SharedCode'
[11/13/19 5:17:08 PM] Stack:   File "/usr/local/Cellar/azure-functions-core-tools/2.7.1846/workers/python/3.6/OSX/X64/azure_functions_worker/dispatcher.py", line 242, in _handle__function_load_request
[11/13/19 5:17:08 PM]     func_request.metadata.entry_point)
[11/13/19 5:17:08 PM]   File "/usr/local/Cellar/azure-functions-core-tools/2.7.1846/workers/python/3.6/OSX/X64/azure_functions_worker/loader.py", line 66, in load_function
[11/13/19 5:17:08 PM]     mod = importlib.import_module(fullmodname)
[11/13/19 5:17:08 PM]   File "/Users/dylankaufman/Documents/Projects/SH/Data Pipeline/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
[11/13/19 5:17:08 PM]     return _bootstrap._gcd_import(name[level:], package, level)
[11/13/19 5:17:08 PM]   File "/Users/dylankaufman/Documents/Projects/SH/Azure Functions/TLoginEvents/tlogins.py", line 1, in <module>
[11/13/19 5:17:08 PM]     from SharedCode import FunctionApp
[11/13/19 5:17:08 PM] .

尽管我以前从未注意到它,但我确实感到非常奇怪,堆栈跟踪似乎正在寻找与我实际运行的项目不同的项目(数据管道)的环境。当前项目(Azure函数)是通过复制原始目录批发创建的,但是我认为env的全部要点是它是本地的...(我对Python和VSCode还是比较陌生,所以如果有人可以解释一下那个,或者让我指点一下,我会很感激的。

也就是说,我认为这不是真正的问题。如果我在“终端”窗口中的同一环境中运行python3(甚至在VSCode中),我也可以运行导入,就像这样(在独立的终端窗口中):

Dylans-iMac:Azure Functions dylankaufman$ source ./env/bin/activate
(env) Dylans-iMac:Azure Functions dylankaufman$ python3
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from SharedCode import FunctionApp
>>> import datetime
>>> app = FunctionApp.FunctionApp('test', datetime.datetime.utcnow())
>>> app.functionName
'test'
>>> 

如果有用,这是vscode settings.json:

{
  "python.pythonPath": "${workspacefolder}/env/bin/python",
  "azureFunctions.projectRuntime": "~2",
  "azureFunctions.projectLanguage": "Python",
  "azureFunctions.templateFilter": "Verified",
  "azureFunctions.deploySubpath": "Azure Functions.zip",
  "azureFunctions.preDeployTask": "func: pack --build-native-deps",
  "files.exclude": {
    "obj": true,
    "bin": true
  },
  "azureFunctions.pythonVenv": "env",
  "debug.internalConsoleOptions": "neverOpen"
}

(请注意,作为解决此问题的一部分,我今天添加了$ {workspacefolder},但无济于事)

感谢您提供的任何帮助/见解!

迪伦

0 个答案:

没有答案