git-sim icon indicating copy to clipboard operation
git-sim copied to clipboard

ModuleNotFoundError when executing source code locally, if "git_sim." prefix isn't removed

Open initialcommit-io opened this issue 2 years ago • 1 comments

@paketb0te I think maybe you can help me with this since I haven't had too much time to research it.

Basically, for local testing of Git-Sim I like to directly run the __main__.py file with python like this:

python __main__.py log

But the problem is since it's not running as a package I end up getting errors like:

ModuleNotFoundError: No module named 'git_sim.git_sim_add'; 'git_sim' is not a package

I can get around this by removing the package prefix git_sim. in all the imports, but its very annoying because I need to remember to stash those changes before building the code for release, and I need to make sure not to commit that by accident.

Do you have any thoughts on how I can test locally without having to mess with the imports like that?

initialcommit-io avatar Jan 25 '23 10:01 initialcommit-io

Did you try installing with https://pypa.github.io/pipx instead of pip? What operating system are you using?

cclauss avatar Jan 25 '23 16:01 cclauss

@initialcommit-io yeah I think the whole imports / packages / modules stuff is one of the more confusing parts of python, I find myself trying to make sense of it very often :sweat_smile:

I tried running the file directly and see the same behavior you described.

I think I found the issue in our case though: We have a file git_sim.py (which for python is a module) inside the directory git_sim (which for python is a package:

git_sim
├── git_sim_add.py
├── git_sim_base_command.py
├── git_sim_branch.py
├── git_sim_cherrypick.py
├── git_sim_commit.py
├── git_sim_log.py
├── git_sim_merge.py
├── git_sim.py          <-- here
├── git_sim_rebase.py
├── git_sim_reset.py
├── git_sim_restore.py
├── git_sim_revert.py
├── git_sim_stash.py
├── git_sim_status.py
├── git_sim_tag.py
├── __init__.py
├── logo.png
└── __main__.py

So what I think happens, is that python gets confused that there is a module and a package with the same name.

For me it seems to work in either case after moving the contents of git_sim.py into __main__.py and deleting it, see my branch fix_ModuleNotFoundError.

paketb0te avatar Jan 25 '23 19:01 paketb0te

We could of course also just rename the file git_sim.py, git_sim_foo.py worked for me (import git_sim.git_sim_foo as gs in the __main__.py file) :laughing:

paketb0te avatar Jan 25 '23 19:01 paketb0te

I do prefer to keep GitSim class in a separate module than __main__.py.

Hmm, so I renamed git_sim.py to git_sim_scene.py, and changed the import in __main__.py to import git_sim.git_sim_scene as gs.

But when I run python __main__.py status or something, I still get:

line 1, in <module>
    import git_sim.git_sim_scene as gs
ModuleNotFoundError: No module named 'git_sim.git_sim_scene'

Does this work for you?

initialcommit-io avatar Jan 25 '23 21:01 initialcommit-io

Yeah, that works for me :thinking: (updated my branch accordingly)

paketb0te avatar Jan 25 '23 21:01 paketb0te

Hi guys, To avoid messing the imports, I'd suggest to install from source as development mode to try locally. To test this, please uninstall any versions of git-sim you may have installed,

  • Clone the repository
  • cd path/to/repo
  • python setup.py develop

This will install all the necessary dependencies listed in the package and install in developer mode. Meaning, it will always use the source to serve the commands. You can edit the source and it will be reflected immediately. I have opened a PR to update the CONTRIBUTING.md with these steps. If things work, we can close this issue.

abhijitnathwani avatar Jan 26 '23 10:01 abhijitnathwani

This was fixed with the help of @abhijitnathwani and @cclauss. See linked PR for details.

Thank you both!

initialcommit-io avatar Jan 26 '23 17:01 initialcommit-io