从Git子模块导入Pycharm [英] Pycharm imports from Git Submodule

查看:259
本文介绍了从Git子模块导入Pycharm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Pycharm中有一个python项目,其中有一个嵌套的Git子模块.这是文件夹结构:

I have a python project in Pycharm, wherein there is a nested Git submodule. Here is the folder structure:

my-git-repo
    git-submodule-repo
        package1
           foo.py
           bar.py
    package2
        baz.py
    .gitmodules

git-submodule-repo中的导入结构如下:

foo.py:
from package1.bar import some_func

但是,Pycharm无法识别这一点,希望我拥有以下内容:

However, Pycharm doesn't recognize this and wants me to instead have the following:

foo.py:
from git-submodule-repo.package1.bar import some_func

这是有问题的,因为我不想更改git-submodule-repo中的所有导入(这似乎不是一种好习惯,并且无法缩放),并且git-submodule-repo中带有破折号,这是无效的导入的Python语法(我无法重命名存储库).

This is problematic because I don't want to have to change all the imports in git-submodule-repo (doesn't seem like good practice and isn't scaleable) and git-submodule-repo has dashes in it which isn't valid Python syntax for an import (I can't rename the repo).

我还需要一种从代码中的git-submodule-repo导入的方法.像这样:

I also need a way to import from git-submodule-repo in my code. Something like this:

baz.py:
from git-submodule-repo.package1.bar import some_func

但是当然没有破折号使它成为无效的语法.这是.gitmodules的内容,以防它有用:

But of course without the dashes making it invalid syntax. Here is the content of .gitmodules in case its useful:

[submodule "git-submodule-repo"]
    path = git-submodule-repo
    url = https://github.com/SomeAccount/git-submodule-repo.git

任何帮助将不胜感激!

推荐答案

灵魂1
通过在Preferences -> Project -> Project Structure中将子模块文件夹标记为 Sources ,使用本机PyCharm支持.
这种方法的缺点是,除非您使用

Soultion 1
Use native PyCharm support by marking you submodule folders as Sources in Preferences -> Project -> Project Structure.
The drawback of this approach is that it will not allow you to run your code without PyCharm (e.g. from terminal on a remote server) unless you use PyCharm remote interpreter option (works in PyCharm Professional only).

解决方案2
查看
解决方案,该解决方案是@Kevin提出的有关从git子模块常规导入python文件的方法.您可以根据项目的根目录创建指向lib的软链接.您的命令将是:
$ ln -s git-submodule-repo.package1 package1
然后您就可以使用from package1.bar import some_funcfoo.py导入它.
这种方法的缺点是它不是跨平台的,例如.您将无法在Windows中运行它.

Solution 2
Check out the solution proposed by @Kevin about general import of python files from git submodule. You may create soft link to the lib on interest in the root of the project. In your case the command will be:
$ ln -s git-submodule-repo.package1 package1
Then you will be able to import it with from package1.bar import some_func from foo.py.
The drawback of this approach is that it is not cross-platform e.g. you will not be able to run it from windows in case you need it.

这篇关于从Git子模块导入Pycharm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆