0

I have multiple Python projects (which are also Git repositories) in a "GitHub" folder like so:

GitHub/
├── Project_1/
│   ├── main.py
│   └── ...
└── Project_2/
    ├── main.py
    └── ...

Now what I want is to import Project_1 inside of a .py file of Project_2 (for example main.py).

I searched the net of course and found these related questions:

However, they all describe how I would manage to import GitHub, which is not what I want, since I get a weird self-import of Project_2 then.

Is there any way of achieving what I want, while still keeping the possibility of modifying all the code "on the fly" (without having to install it every time again or so)?

I am using PyCharm by the way - in case there is an internal workaround.

Please also tell me if anything I am doing is bad practice, and why - so I can learn from it.

Thanks in advance!

1 Answers1

0

If the project packages are properly, well, packaged, i.e. that you can in general pip install them, then you can also use pip install -e (-e for --editable) to have pip just link them to your current environment instead of copying files over.

That way the packages remain editable in their directories and are usable just like any other package.

Example

An example of an easily workable tree of projects might be something like the below.

- my_projects/ (née GitHub)
  - project1/
    - setup.py (or pyproject.toml)
    - package1/
      - __init__.py
      - mod1.py
      - mod2.py
  - project2/
    - setup.py (or pyproject.toml)
    - package2/
      - __init__.py
      - mod3.py
      - mod4.py
  - project3/
    - secretproject/
      - __init__.py
      - __main__.py  # to allow running `python -m secretproject`
      - mod5.py

This assumes project1 and project2 are projects containing packages you'd want to use in project3, which doesn't necessarily need to be reusable.

If project1 and project2 have been properly configured for package installation (see the link above), you could then, in project3's virtualenv, do pip install -e ~/my_projects/project1 -e ~/my_projects/project2 and package1 and package2 are available for secretproject to use.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Is it "well packaged" when I have all the .py files directly on top (see directory tree)? I get this error when trying your idea: "Can't get a consistent path to setup script from installation directory". I like the approach though! – N. Jonas Figge Nov 12 '21 at 11:33
  • Not quite... See my edit! – AKX Nov 12 '21 at 11:45
  • Okay, now this is not my directory tree anymore ("packages are in extra folders")... If I change it to your example, I by now know a bunch of ways to achieve my goal... I was hoping there exists yet another possibility, where the .py files can be imported across Projects – N. Jonas Figge Nov 12 '21 at 12:47
  • There are no non-standard ways to do something like that. You could fiddle with `sys.path`, but I **very strongly** advise against it, since e.g. IDE support will break. – AKX Nov 12 '21 at 13:01
  • Yes, I understand - I now changed my directory tree and I'm gonna accept this answer – N. Jonas Figge Nov 12 '21 at 13:04