Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
initialcommit-io opened this issue Jan 25, 2023 · 7 comments · Fixed by #27

Comments

@initialcommit-io
Copy link
Contributor

@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?

@cclauss
Copy link
Contributor

cclauss commented Jan 25, 2023

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

@paketb0te
Copy link
Contributor

@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 😅

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
Copy link
Contributor

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) 😆

@initialcommit-io
Copy link
Contributor Author

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?

@paketb0te
Copy link
Contributor

paketb0te commented Jan 25, 2023

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

@abhijitnathwani
Copy link
Contributor

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.

@initialcommit-io initialcommit-io linked a pull request Jan 26, 2023 that will close this issue
@initialcommit-io
Copy link
Contributor Author

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

Thank you both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants