-1

I get the ModuleNotFoundError: no module named 'mysql' error when I run a Python script.

I am a novice to Python programming. The mysql-connector was installed via environment (python3 -m venv env). When env is active (source env/bin/activate) 'pip list' shows a (short) list including mysql-connector and the script runs without error. When env is not active, 'pip list' does not contain mysql-connector and the script fails with the above error. What am I missing? What do I have to do so that the script can run without activating env first?

2
  • 1
    Why would you think you could use a module outside the virtual environment if you installed inside the virtual environment? Commented Jan 21 at 21:43
  • This is the correct behavior. It's what you want. You should only install packages in a virtual environment to keep your system and other python environments clean. You should run within your environment. Commented Jan 21 at 23:32

2 Answers 2

1

You must have installed the mysql package while the venv was active, therefore it is only available inside of the venv.

The solution is simple -- install the package again, while the venv is not active, so that it is available everywhere.

Sign up to request clarification or add additional context in comments.

Comments

0

When I tried pip install mysql-connector I got a 'externally managed environment' error, and that was the reason I created a virtual environment as stated in the error message.

However, another Google search led to this: sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /usr/lib/python3.11/EXTERNALLY-MANAGED.old. This got rid of the externally managed error, and I was able to install mysql-connector without venv. Idk if this is a 'dirty' solution or not, but it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.