如何复制virtualenv

问题描述 投票:0回答:10

我有一个现有的 virtualenv,其中有很多包,但有旧版本的 Django。

我想做的是复制这个环境,这样我就有了另一个具有完全相同的包的环境但是有较新版本的Django。我该怎么做?

python django virtualenv
10个回答
244
投票

最简单的方法是使用 pip 生成需求文件。需求文件基本上是一个文件,其中包含您想要安装的所有 python 包(或者在 pip 生成的文件的情况下已经安装)的列表,以及它们的版本。

要生成需求文件,请进入原始 virtualenv,然后运行:

pip freeze > requirements.txt

这将为您生成 requirements.txt 文件。如果您在您最喜欢的文本编辑器中打开该文件,您会看到类似以下内容:

Django==1.3
Fabric==1.0.1
etc...

现在,将显示

Django==x.x
的行编辑为
Django==1.3
(或者您想要在新 virtualenv 中安装的任何版本)。

最后,激活你的new virtualenv,然后运行:

pip install -r requirements.txt

pip 将自动下载并安装您指定的任何版本的 requirements.txt 文件中列出的所有 python 模块!


39
投票

另一种选择是使用

virtualenv-clone
包:

用于克隆不可重定位 virtualenv 的脚本。


20
投票

最简单的选择是使用

virtualenv-clone
包。

要将

venv1
复制到
venv2
,请按照以下步骤操作:

  1. virtualenv-clone
    或虚拟虚拟环境
    venv1
    中安装
    venv_dummy
    。创建
    venv_dummy

    python -m virtualenv venv_dummy
    source venv_dummy/bin/activate
    
  2. 安装

    virtualenv-clone

    (venv_dummy): pip install virtualenv-clone
    
  3. 要将

    venv1
    复制到
    venv2

    (venv_dummy): virtualenv-clone venv1/ venv2/
    

18
投票

virtualenvwrapper
提供了一个命令来复制virtualenv

cpvirtualenv ENVNAME [TARGETENVNAME]

12
投票

如果您正在使用Anaconda,您可以运行:

conda create --name myclone --clone myenv

这会将

myenv
复制到新创建的名为
myclone
的环境。


2
投票

这是我用于克隆 python 虚拟环境的命令。

packs=`source-path/bin/pip freeze` && python3 -m venv <env-name> && target-path/bin/pip install $packs

上述命令中使用的约定:

  • source-path = 您要克隆的环境路径,例如
    /home/john/envs/oldenv
  • env-name = 克隆环境的名称,例如
    myenv
    ,它也可以是一条路径,例如
    /home/john/envs/myenv
  • target-path = 新克隆环境的路径,例如
    /home/john/envs/<env-name>

使用这个的优点或者为什么我更喜欢这个

  1. 无需生成requirements.txt文件。
  2. 克隆过程中不会激活/停用任何环境。
  3. 要执行的单个命令(一次运行3个命令)。

在某些情况下,您可能希望在克隆环境时排除全局包,您可以将

source-path/bin/pip freeze
替换为
source-path/bin/pip freeze --local
,更多关于
--local
这里


1
投票

如果您使用 pip“venv”。我复制粘贴了保存虚拟环境的文件夹,并手动更改了复制文件夹的 bin 文件夹中的文件。 我不知道它是否有效,但它有效!


0
投票

你能不能简单地:

  • 将现有的虚拟环境目录复制到新的目录
  • 更新到新的 Django?

0
投票

pip 可以,但在没有互联网的计算机上这是一个问题。

我为此编写了一个小代码,它对我有用。我写在这里是因为也许对其他人有用。

(注:我在Windows上测试过)

  1. 复制项目文件夹
  2. 将项目文件夹粘贴到另一个目录
  3. 更改此代码中的地址部分并运行代码:
import os

# The new address of our script folder
script_folder = r'D:\Python proqrams\pdf_to_excel\venv\Scripts'

# the old address of our venv folder
old_path = rb'C:\Users\AVG-dell\Desktop\pdf_to_excel\venv'

# the new address of our venv folder
new_path = rb"D:\Python proqrams\pdf_to_excel\venv"


def find_replace( folder ):

    names = os.listdir( folder )

    for name in names:
        current_path = os.path.join( folder, name )

        if os.path.isdir( current_path ):
            find_replace( current_path )

        elif os.path.isfile( current_path ) :

            try:
                with open( current_path ,'rb' ) as f:
                    data = f.read()

                if old_path in data:
                    print( current_path )

                    data2 = data.replace( old_path , new_path )

                    with open( current_path , 'wb' ) as f:
                        f.write(data2)


            except:
                pass



find_replace( script_folder )

print('completed')

0
投票

随机导入

defguess_the_number(): print("欢迎来猜数字!")

# Set the range for the random number
lower_limit = 1
upper_limit = 100
secret_number = random.randint(lower_limit, upper_limit)

# Set the initial number of attempts
attempts = 0

print(f"Guess the number between {lower_limit} and {upper_limit}")

while True:
    # Get user input
    guess = input("Enter your guess: ")
    
    try:
        # Convert the input to an integer
        guess = int(guess)
    except ValueError:
        print("Please enter a valid number.")
        continue
    
    # Increment the number of attempts
    attempts += 1
    
    # Check if the guess is correct
    if guess == secret_number:
        print(f"Congratulations! You guessed the number in {attempts} attempts.")
        break
    elif guess < secret_number:
        print("Too low! Try again.")
    else:
        print("Too high! Try again.")

if name == "main": 猜数字()

© www.soinside.com 2019 - 2024. All rights reserved.