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

Inconsistent ModuleNotFoundErrors #1456

Closed
ethical-haquer opened this issue Mar 23, 2024 · 11 comments · Fixed by #1493
Closed

Inconsistent ModuleNotFoundErrors #1456

ethical-haquer opened this issue Mar 23, 2024 · 11 comments · Fixed by #1493
Labels

Comments

@ethical-haquer
Copy link
Contributor

ethical-haquer commented Mar 23, 2024

I have two tabs open, both of them Python files.
When I try to run tab one, I get this error:

Traceback (most recent call last):
  File "/home/ethical_haquer/fuzzy.py", line 1, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

When I run tab two I don't get any errors.
The funny thing is... both tab one and tab two contain the same import: import cv2

This has been bugging me for a while, with multiple different imports and projects, but I've been to lazy to report it. 😊

@Moosems
Copy link
Contributor

Moosems commented Mar 23, 2024

Very interesting. Maybe the two call different installed versions of Python?

@rdbende
Copy link
Collaborator

rdbende commented Mar 23, 2024

If porcupine finds a python virtual environment, it will automatically activate it before running the script. Is the other file, by chance in a project that has a python venv?

@Akuli
Copy link
Owner

Akuli commented Mar 23, 2024

You can control venvs by right-clicking them in the directory tree at left. The currently selected one (if any) shows up with a yellow "venv" in front of it.

You can see which venv Porcupine uses based on the blue command that Porcupine displays when a file runs. With a venv in use, the command is two lines (first line activates venv). Without a venv in use, the command is one line.

@Akuli
Copy link
Owner

Akuli commented Mar 23, 2024

Controlling venvs should look like this:

s

I should document all this.

@Akuli Akuli added the docs label Mar 23, 2024
@Moosems
Copy link
Contributor

Moosems commented Mar 23, 2024

So in a way I was kinda right??

@ethical-haquer
Copy link
Contributor Author

Thanks for all of the replies!
So yeah, the first file uses the porcupine-venv, but the second one doesn't.
I'm not sure why they don't both use (or not use) the venv though.
The directory tree doesn't show any venvs - I've only ever used them in Porcupine (that I know of).
Also, what is the proper way to "update" the venv when new modules are installed?
I've tried closing and reopening the app and/or files, but that doesn't work.
TIA.

@ethical-haquer
Copy link
Contributor Author

Another thing, file one is located in my home dir, but file two is in a GitHub repo.

@Akuli
Copy link
Owner

Akuli commented Mar 24, 2024

So yeah, the first file uses the porcupine-venv, but the second one doesn't. I'm not sure why they don't both use (or not use) the venv though.

The porcupine-venv is meant to be used only for Porcupine and its dependencies, not for your projects, so we could adjust Porcupine's auto-detecting logic so that it never uses a venv that is named porcupine-venv.

Currently the auto-detect logic is:

possible_envs = [path for path in project_root.glob("*env*") if is_venv(path)]

Also, what is the proper way to "update" the venv when new modules are installed? I've tried closing and reopening the app and/or files, but that doesn't work.

You don't update the venv, you install the dependencies into that venv. This way, if you want to uninstall the things you installed, just delete the whole venv. When you install a dependency into a venv, everything installs into the venv folder. That's the whole point of venvs: you don't pollute your system's Python with all your projects' dependencies.

I recommend making one venv for each project.

To install something into the venv, you activate the venv (with source name-of-venv/bin/activate) and then pip install as usual.

And again, all of this should be documented, but it isn't...

@Akuli
Copy link
Owner

Akuli commented Mar 29, 2024

I started documenting this. For taking screenshots, I temporarily cleared my Porcupine settings. As you might have guessed, Porcupine found my porcupine-venv folder and used it as the venv for my ~/hello.py file.

Please make a pull request that skips folders named porcupine-venv in the autodetect logic (or maybe any folders whose name contains porcupine).

@ethical-haquer
Copy link
Contributor Author

I started documenting this. For taking screenshots, I temporarily cleared my Porcupine settings. As you might have guessed, Porcupine found my porcupine-venv folder and used it as the venv for my ~/hello.py file.

Please make a pull request that skips folders named porcupine-venv in the autodetect logic (or maybe any folders whose name contains porcupine).

👍 Will do!

@Akuli
Copy link
Owner

Akuli commented Mar 31, 2024

This is now documented in user-doc/projects.md, but let's keep the issue open until the porcupine-venv autodetecting problem is fixed.

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

Successfully merging a pull request may close this issue.

4 participants