Search code examples
pythonpycharmproject

PyCharm - Add Sources Root to GitHub


I have an old project (OldProject) stored at some location on my Desktop, I've started a new project (NewProject) which uses files from OldProject.

The two projects are in two completely different directories and OldProject can exist entirely on its own without NewProject (meaning I can't refactor it into NewProject as it'll move the directory, which I do not want)

Now to use the scripts from OldProject in NewProject:

  1. Opened NewProject in PyCharm
  2. Opened OldProject and then clicked Attach to attach it to NewProject
  3. Went into project settings and marked OldProject as a sources root and made it a dependency of NewProject
  4. In the same menu, I added it as a content root so that it is now in my Project View

I then carried on with development.

Now, I would like to share NewProject on GitHub but (to my surprise) the initial commit did not include any files from OldProject meaning it's quite useless to anyone else.

Is there a way I can explicitly add OldProject to NewProject without having to copy over all the files and updating the import statements (in both projects) as such?

When using the scripts from OldProject in NewProject the import statements are something like:

import old_project_folder

As opposed to:

import OldProject.old_project_folder

i.e. within the IDE, OldProject and NewProject are shown to be in the same location.

However, I can't run the program in NewProject anywhere else than PyCharm because the import statements aren't correct.

I have also tried programmatically inserting the directory into driver script:

if __name__ == '__main__':
    import sys
    sys.path.insert(1, path_to_old_project)
    run()

But that hasn't worked.

I have tried to add an __init__.py to OldProject to make it a module but I have several directories and subdirectories so I'd like to know if there's a way to achieve the same outcome before going further with it.


Solution

  • (to my surprise) the initial commit did not include any files from OldProject meaning it's quite useless to anyone else.

    That is because OldProject is not in your git repository. You can find the improper solution to what you want here.

    The proper solution to this is to make OldProject a stand-alone package, make it a dependency of NewProject, and install it via pip or setuptools.