英文:
Create a python venv that works on systems with no python installed
问题
如何设置虚拟环境,以便用于调用Python脚本,但不需要系统范围的Python。
我的目标是在一个文件夹中拥有Python脚本以及Python本身,并包含该脚本所需的所有库。
我尝试使用PyCharm来管理我的虚拟环境,但PyCharm创建的虚拟环境需要系统范围的Python才能工作(我相当确定这是由于pyvenv.cfg文件需要引用系统范围的Python导致的)。
最终,我并不在意是否使用PyCharm或其他工具来创建虚拟环境,我只希望这能够正常工作。有什么建议吗?
我不想将其编译为可执行文件。
英文:
How do I setup a virtual environment in such a way that is can be used to call a python script, but doesn't need a system wide python.
My goal is it to have one folder with the python script in it and python itself in that folder as well with all the libs I need for that python script.
I've tried using pycharm to manage my venv, but the venv py charm creates needs a system wide python for the venv python to work. (I'm quite sure this is because of the pyvenv.cfg file which needs reference to a system wide python).
<br>
At the end of the day I don't care if I create the venv with pycharm or some other tool, I just want this to work. Any suggestions?
<br><br>
What I don't want, is to compile it to an executable.
答案1
得分: 1
以下是翻译的部分:
The way to go is NOT through a virtual environment in this case.
在这种情况下,不要使用虚拟环境。
I had to do it in the company I work - because some computers don't have internet.
我在我工作的公司中这样做 - 因为一些电脑没有互联网。
-
create in you internet-connected computer a directory,
into which you will install all the packages.
Let's call itenv
.
$ mkdir -p /path/to/env
-
在您连接互联网的计算机上创建一个目录,
在该目录中您将安装所有的软件包。
我们称之为env
。
$ mkdir -p /path/to/env
-
When installing the required packages, add to
pip install
simply:
-t /path/to/env
(t
for target directory)
so:
pip install -t /path/to/env packagename
-
在安装所需的软件包时,只需在
pip install
后面添加:
-t /path/to/env
(t
代表目标目录)
因此:
pip install -t /path/to/env packagename
-
Transfer your
env
folder to your offline computer (via usb or whatever) -
将您的
env
文件夹传输到您的离线电脑(通过 USB 或其他方式) -
add
/path/to/env
to yourPYTHONPATH
environment variable
In Linux or mac add to~/.bashrc
into its content:
export PYTHONPATH=/path/to/env:$PYTHONPATH
In Windows, add to your:$profile.CurrentUserAllHosts
:
$env:PYTHONPATH=C:/path/to/env;${$env:PYTHONPATH}
(use;
as delimiter!)
PYTHONPATH
determines where your Python looks for packages.
The order matters - from left to right it is searched through. -
将
/path/to/env
添加到您的PYTHONPATH
环境变量中
在 Linux 或 macOS 上,将其添加到~/.bashrc
中的内容中:
export PYTHONPATH=/path/to/env:$PYTHONPATH
在 Windows 中,添加到您的:$profile.CurrentUserAllHosts
:
$env:PYTHONPATH=C:/path/to/env;${$env:PYTHONPATH}
(使用;
作为分隔符!)
PYTHONPATH
决定了 Python 查找软件包的位置。
顺序很重要 - 从左到右进行搜索。 -
source ~/.bashrc
in Linux
. $profile.CurrentUserAllHosts
in Windows
to make the entered changes be executed, thus valid. -
在 Linux 中执行
source ~/.bashrc
在 Windows 中执行. $profile.CurrentUserAllHosts
以执行输入的更改,从而使其生效。
Voila, now your Python will be able to find the packages in your folder!
现在,您的 Python 将能够在您的文件夹中找到软件包!
systemwide or non-systemwide python
系统范围的或非系统范围的 Python
I don't know - I didn't tried myself.
我不知道 - 我自己没有尝试过。
You can start a local python by just giving the
absolute path to the python.exe (windows) or python binary (linux/mac=unix-derived).
In unix-derived systems, you can do:
在类Unix系统中,您可以这样做:
env PYTHONPATH=/path/to/env /path/to/python /path/to/script.py
使 PYTHONPATH 仅在此调用中有效。
In windows, you could do it using a function e.g. in PowerShell
在 Windows 中,您可以使用 PowerShell 中的函数来执行:
myscript () {
local PATH=/path/to/python;$env:PYTHON
local PYTHONPATH=/path/to/env
python /path/to/script.py
}
myscript
然后,在函数外部,变量保持不变。
英文:
The way to go is NOT through a virtual environment in this case.
I had to do it in the company I work - because some computers don't have internet.
- create in you internet-connected computer a directory,
into which you will install all the packages.
Let's call itenv
.
$ mkdir -p /path/to/env
- When installing the required packages, add to
pip install
simply:
-t /path/to/env
(t
for target directory)
so:
pip install -t /path/to/env packagename
- Transfer your
env
folder to your offline computer (via usb or whatever) - add
/path/to/env
to yourPYTHONPATH
environment variable
In Linux or mac add to~/.bashrc
into its content:
export PYTHONPATH=/path/to/env:$PYTHONPATH
In Windows, add to your:$profile.CurrentUserAllHosts
:
$env:PYTHONPATH=C:/path/to/env;${$env:PYTHONPATH}
(use;
as delimiter!)
PYTHONPATH
determines where your Python looks for packages.
The order matters - from left to right it is searched through. source ~/.bashrc
in Linux
. $profile.CurrentUserAllHosts
in Windows
to make the entered changes be executed, thus valid.
Voila, now your Python will be able to find the packages in your folder!
systemwide or non-systemwide python
I don't know - I didn't tried myself.
You can start a local python by just giving the
absolute path to the python.exe (windows) or python binary (linux/mac=unix-derived).
In unix-derived systems, you can do:
env PYTHONPATH=/path/to/env /path/to/python /path/to/script.py
to make the PYTHONPATH only valid for this call.
In windows, you could do it using a function e.g. in PowerShell
myscript () {
local PATH=/path/to/python;$env:PYTHON
local PYTHONPATH=/path/to/env
python /path/to/script.py
}
myscript
Then, outside the function, the variable stays unchanged.
答案2
得分: 0
答案很简单,虚拟环境不适合这种情况。可移植/嵌入式 Python 是正确的选择。视频链接在这里:https://www.youtube.com/watch?v=z8kgu74jERM
英文:
The answer is simply, virtual environments are the wrong thing for this situation. Portable/ embedded Python is the correct thing. A video to that here https://www.youtube.com/watch?v=z8kgu74jERM
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论