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 Answers
Reset to default 2I'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 dowhich python
andwhich 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 specifypython3.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, forimport yaml
, you needpip install pyyaml
.) – chepner Commented Nov 20, 2024 at 15:02
2 Answers
Reset to default 2I'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
版权声明:本文标题:macos - Python package still unavailable after installing with venv - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745550050a2155588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
source $HOME/.venvs/MyEnv/bin/activate
please dowhich python
andwhich pip
. There you can see which pip and Python your terminal is actually trying to use – SirGamsay Commented Nov 20, 2024 at 14:37python 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:55python3
it works, if I specifypython3.12
it doesn't – Beth Long Commented Nov 20, 2024 at 15:01some_package
? Not every Python package has the same name as the distribution package that installs it. (For example, forimport yaml
, you needpip install pyyaml
.) – chepner Commented Nov 20, 2024 at 15:02