【问题标题】:Trying to import a Python module from another repo (within VSCode)尝试从另一个仓库(在 VSCode 中)导入 Python 模块
【发布时间】:2023-04-04 14:13:01
【问题描述】:

目前有两个 repos 克隆到 VSCode。当我打开 VSCode 时,我的目录结构如下所示:

- Repo1
  - Base 1
    - Scripts
      -Code.py
- Repo2
  - testcode.py

我正在尝试执行的这个 python 文件具有以下导入语句:

from repo2.testcode import testmodule

但是,python 没有将其识别为模块...并且正在返回以下错误消息:

 ModuleNotFoundError: No Module named 'Repo2'.

我做了一些研究并意识到我需要做任何一个:我需要制作一个包裹吗?还是需要在模块路径中添加import语句?

提前致谢。

【问题讨论】:

    标签: python python-3.x visual-studio-code


    【解决方案1】:

    在import语句之前使用下面的代码

    import os,sys,inspect
    current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    parent_dir = os.path.dirname(current_dir)
    sys.path.insert(0, parent_dir)
    

    它将带你到父目录并从那里导入模块

    【讨论】:

      【解决方案2】:

      问题:

      ModuleNotFoundError: No Module named
      

      这意味着python解释器在PYTHONPATH下找不到这个包或者包被损坏了。

      您可以通过此代码获取PYTHONPATH

      import sys
      from pprint import pprint
      
      pprint(sys.path)
      

      可以查看是否能找到Repo2文件夹的父文件夹。

      解决方案:

      VSCode 提供了修改 PYTHONPATH 的方法,但只能在调试模式下工作:

      "env": {"PYTHONPATH":"${workspaceFolder}"}, //${workspaceFolder} means the path of the folder opened in VS Code. Modify it to the path of the parent folder of Repo2.
      

      或者在python文件中手动修改路径:

      sys.path.append("parent folder path of the Repo2 folder")
      

      它应该放在 import 语句之前。

      【讨论】:

        【解决方案3】:

        如果你决定使用包,你可以在 setup.py 中指定另一个本地包的路径。

        你的整体结构是:

        |-- Repo1/
        |   |-- setup.py
        |   |-- Repo1/
        │   |    |-- __init__.py
        |   |    |--Code.py
        |   |-- tests/
        |   |    |--test_Code.py
        |
        |-- Repo2/
        |   |-- setup.py
        |   |-- Repo2/
        │   |    |-- __init__.py
        |   |    |--testcode.py
        
        

        repo1/setup.py.

        from setuptools import setup, find_packages
        import os
        
        # dynamically determine the path to Repo2
        local_name = "Repo2"
        local_path = os.getcwd().split(os.sep)
        local_path = os.sep.join(local_path[0:local_path.index(local_name)])
        local_path = os.path.join(local_path, local_name)
        
        
        setup(
            name="Repo1",
            version="1.0.0",
            description="First Repo",
            python_requires=">=3.5.0",
            packages = find_packages(),
            install_requires=[
                'SomePyPIPackage',
                f"{local_name} @ file://localhost/{local_path}#egg={local_name}"
            ]
        )
        

        Repo2 中的 setup.py 文件类似,但没有 install_requires localhost 部分。

        然后,当您在 Repo1 的顶级目录中时,您可以运行(不要忘记末尾的点):

        # would have to reinstall if you make changes to Repo2
        pip install .
        
        # or install in editable mode
        pip install -e .
        

        那么在 Repo1 中你应该可以使用:

        from repo2.testcode import testmodule
        

        【讨论】:

          猜你喜欢
          • 2017-10-23
          • 2013-10-03
          • 1970-01-01
          • 2020-05-18
          • 1970-01-01
          • 2010-11-11
          • 1970-01-01
          • 2014-04-30
          • 2022-01-08
          相关资源
          最近更新 更多