admin管理员组

文章数量:1023044

I know this has been answered many times but I've been through the answers and it still won't work. I want to install package <some_package> so that I can use it through import in a script I run often.

I tried using pipx, but that obviously installs the package in a virtual environment which is then only helpful if you run the code in that virtual environment which I don't want to do.

So I followed the advice here and did:

$ python3 -m venv $HOME/.venvs/MyEnv
$ $HOME/.venvs/MyEnv/bin/python -m pip install <some_package>
$ source $HOME/.venvs/MyEnv/bin/activate

Then I tried to run my script but I get the error: ModuleNotFoundError: No module named '<some_package>'

Where did I go wrong?

I know this has been answered many times but I've been through the answers and it still won't work. I want to install package <some_package> so that I can use it through import in a script I run often.

I tried using pipx, but that obviously installs the package in a virtual environment which is then only helpful if you run the code in that virtual environment which I don't want to do.

So I followed the advice here and did:

$ python3 -m venv $HOME/.venvs/MyEnv
$ $HOME/.venvs/MyEnv/bin/python -m pip install <some_package>
$ source $HOME/.venvs/MyEnv/bin/activate

Then I tried to run my script but I get the error: ModuleNotFoundError: No module named '<some_package>'

Where did I go wrong?

Share Improve this question asked Nov 20, 2024 at 14:33 Beth LongBeth Long 4135 silver badges25 bronze badges 4
  • 2 After source $HOME/.venvs/MyEnv/bin/activate please do which python and which pip. There you can see which pip and Python your terminal is actually trying to use – SirGamsay Commented Nov 20, 2024 at 14:37
  • How are you running your script? Are you running, e.g., python myscript.py, in which case please report back the information that @SirGamsay suggested. Or, is the script executable and starting with a shebang to define the interpreter to use (and therefore runnable with ./myscript.py) - in which case what is the shebang? – Matt Pitkin Commented Nov 20, 2024 at 14:55
  • Thanks for the insight: if I run with python3 it works, if I specify python3.12 it doesn't – Beth Long Commented Nov 20, 2024 at 15:01
  • What is some_package? Not every Python package has the same name as the distribution package that installs it. (For example, for import yaml, you need pip install pyyaml.) – chepner Commented Nov 20, 2024 at 15:02
Add a comment  | 

2 Answers 2

Reset to default 2

I'm not sure I fully understand your problem. You can install the package in a virtual environment.

$ source venv/bin/activate
$ pip install <some_package>

and run the script as follows from anywhere without having to manually activate the environment.

$ /home/your-virtualenvironment-folder/venv/bin/python home/path-to-your-script/your_script.py

You can also add a line at the beginning of your script:

#!/home/your-virtualenvironment-folder/venv/bin/python

and run the script in the background as follows:

nohup /home/your-virtualenvironment-folder/venv/bin/python nhome/path-to-your-script/your_script.py &

Try activating the virtual environment first and then install the package.

$ python3 -m venv $HOME/.venvs/MyEnv

$ source $HOME/.venvs/MyEnv/bin/activate

$ pip install <some_package>

I know this has been answered many times but I've been through the answers and it still won't work. I want to install package <some_package> so that I can use it through import in a script I run often.

I tried using pipx, but that obviously installs the package in a virtual environment which is then only helpful if you run the code in that virtual environment which I don't want to do.

So I followed the advice here and did:

$ python3 -m venv $HOME/.venvs/MyEnv
$ $HOME/.venvs/MyEnv/bin/python -m pip install <some_package>
$ source $HOME/.venvs/MyEnv/bin/activate

Then I tried to run my script but I get the error: ModuleNotFoundError: No module named '<some_package>'

Where did I go wrong?

I know this has been answered many times but I've been through the answers and it still won't work. I want to install package <some_package> so that I can use it through import in a script I run often.

I tried using pipx, but that obviously installs the package in a virtual environment which is then only helpful if you run the code in that virtual environment which I don't want to do.

So I followed the advice here and did:

$ python3 -m venv $HOME/.venvs/MyEnv
$ $HOME/.venvs/MyEnv/bin/python -m pip install <some_package>
$ source $HOME/.venvs/MyEnv/bin/activate

Then I tried to run my script but I get the error: ModuleNotFoundError: No module named '<some_package>'

Where did I go wrong?

Share Improve this question asked Nov 20, 2024 at 14:33 Beth LongBeth Long 4135 silver badges25 bronze badges 4
  • 2 After source $HOME/.venvs/MyEnv/bin/activate please do which python and which pip. There you can see which pip and Python your terminal is actually trying to use – SirGamsay Commented Nov 20, 2024 at 14:37
  • How are you running your script? Are you running, e.g., python myscript.py, in which case please report back the information that @SirGamsay suggested. Or, is the script executable and starting with a shebang to define the interpreter to use (and therefore runnable with ./myscript.py) - in which case what is the shebang? – Matt Pitkin Commented Nov 20, 2024 at 14:55
  • Thanks for the insight: if I run with python3 it works, if I specify python3.12 it doesn't – Beth Long Commented Nov 20, 2024 at 15:01
  • What is some_package? Not every Python package has the same name as the distribution package that installs it. (For example, for import yaml, you need pip install pyyaml.) – chepner Commented Nov 20, 2024 at 15:02
Add a comment  | 

2 Answers 2

Reset to default 2

I'm not sure I fully understand your problem. You can install the package in a virtual environment.

$ source venv/bin/activate
$ pip install <some_package>

and run the script as follows from anywhere without having to manually activate the environment.

$ /home/your-virtualenvironment-folder/venv/bin/python home/path-to-your-script/your_script.py

You can also add a line at the beginning of your script:

#!/home/your-virtualenvironment-folder/venv/bin/python

and run the script in the background as follows:

nohup /home/your-virtualenvironment-folder/venv/bin/python nhome/path-to-your-script/your_script.py &

Try activating the virtual environment first and then install the package.

$ python3 -m venv $HOME/.venvs/MyEnv

$ source $HOME/.venvs/MyEnv/bin/activate

$ pip install <some_package>

本文标签: macosPython package still unavailable after installing with venvStack Overflow