简体   繁体   English

pipenv:如何强制 virtualenv 目录?

[英]pipenv : how to force virtualenv directory?

Actually, pipenv will install the virtualenv with a path like this :实际上,pipenv 将使用如下路径安装 virtualenv:

$WORKON_HOME/<base_dir>-<hash>

Is it possible to have exactly the path I want, that is without the base_dir and the hash , for exemple :是否可以准确地获得我想要的路径,即没有base_dirhash ,例如:

/home/user/myapp_venv

Apart from using a custom location, you can also install the virtualenv in your project's directory.除了使用自定义位置之外,您还可以在项目目录中安装 virtualenv。 Just add the following line in your .bashrc/.zshrc file:只需在 .bashrc/.zshrc 文件中添加以下行:

export PIPENV_VENV_IN_PROJECT=1

Just wanted to let others know that there is another approach available too.只是想让其他人知道还有另一种方法可用。

Should you keep the virtualenv inside or outside the project's directory is an opinionated question afterall.毕竟,您应该将 virtualenv 保留在项目目录的内部还是外部是一个固执的问题。

There is an undocumented feature of pipenv, it could locate virtualenv path from VIRTUAL_ENV environment variable, but you need to create virtualenv manually: pipenv 有一个公开的特性,它可以从VIRTUAL_ENV环境变量中找到 virtualenv 路径,但您需要手动创建 virtualenv:

virtualenv /home/user/myapp_venv
VIRTUAL_ENV=/home/user/myapp_venv pipenv install

There's an undocumented feature in pipenv : if you create a file named .venv in the project root with a path in it , pipenv will use that instead of an autogenerated path. pipenv有一个未pipenv功能:如果您在项目根目录中创建一个名为.venv的文件,其中包含一个路径pipenv将使用该文件而不是自动生成的路径。

This, however, is more fit for cases when you already have an established set of environments that you wish to reuse .但是,这更适合于您已经拥有一组希望重用的既定环境的情况 Otherwise, placing environments in arbitrary places is prone to create a mess eventually.否则,将环境放置在任意位置最终很容易造成混乱。 pipenv relieves you from this task specifically to keep them all in one predictable place and eliminate accidental collisions from human error. pipenv您从这项任务中pipenv出来,专门将它们保存在一个可预测的地方,并消除人为错误造成的意外碰撞。

Creating a directory .venv in the project then try to initiate with pipenv install <some package> did not work for me at the first in my mac, it was always creating virtual environment somewhere else.在项目中创建一个目录 .venv 然后尝试使用pipenv install <some package>启动在我的 mac 中对我不起作用,它总是在其他地方创建虚拟环境。 Though later I figure it out the reason.虽然后来我弄清楚了原因。 The hack is, first cd to the project folder cd /Home/.../MyProject then create a empty Pipfile touch Pipfile the create a directory mkdir .venv should work. hack 是,首先cd到项目文件夹cd /Home/.../MyProject然后创建一个空的 Pipfile touch Pipfile创建一个目录mkdir .venv应该可以工作。 Now you can run pipenv install and .venv folder will be used.现在您可以运行pipenv install并且 .venv 文件夹将被使用。

Now the elaboration is given form the source code .现在从源代码给出详细说明。 When the pipenv try to create create a virtual env it looks to the directory dot_venv = os.path.join(self.project_directory, ".venv") (taken from source code) and thats how the self.project_directory looks like:当 pipenv 尝试创建一个虚拟环境时,它会查看目录dot_venv = os.path.join(self.project_directory, ".venv") (取自源代码),这就是self.project_directory样子:

@property
    def project_directory(self):
        # type: () -> str
        return os.path.abspath(os.path.join(self.pipfile_location, os.pardir))

So that function looks for the pipfile location to create the path it will create the virtual environment.因此,该函数查找 pipfile 位置以创建它将创建虚拟环境的路径。 So if you create the empty Pipfile before hand it is confirmed that it will find that file and then find the .venv directory and create the path all together.因此,如果您Pipfile创建空的Pipfile ,则确认它会找到该文件,然后找到 .venv 目录并一起创建路径。

In windows, Pycharm user by在 Windows 中,Pycharm 用户通过

  1. make .venv fold in project使 .venv 在项目中折叠

  2. Settings->Project->Project interpreter->pipenv Environment设置->项目->项目解释器->pipenv环境

it works for me这个对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM