English 中文(简体)
如何重复虚拟
原标题:How to duplicate virtualenv

我有一套现成的包裹,但有一个旧版本的詹戈。

我想做的是duplicate<>/strong>这一环境,因此,我有另一个环境,其组合恰好相同:<>but/em>,新版本的Django。 我如何能够这样做?

最佳回答

最容易的方式是利用管道生成要求书。 要求档案基本上包含一份清单,列出你希望安装的所有假装(或已经安装在纸浆生成的档案中),以及它们重新使用的版本。

a. 编制要求文件,进入你原来的虚拟机构,运行:

pip freeze > requirements.txt

这将为你生成requirements.txt文档。 如果你以你的优良文本编辑宣布该档案,你就认为:

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

现在,用以下文字表示:<代码>Django=x.x,表示Django==1.3<>/code>(或您希望安装的新虚拟版本)。

Lastly, activate your new virtualenv, and run:

pip install -r requirements.txt

And pip will automatically download and install all the python modules listed in your requirements.txt file, at whatever versions you specified!

问题回答

Another option is to use virtualenv-clone package:

A script for cloning a non-relocatable virtualenv.

方便选择使用virtualenv-clone。 一揽子计划。

复制<代码>venv1venv2:

  1. Install virtualenv-clone, 或venv1 或圆形虚拟环境venv_dummy。 创建<代码>venv_dummy:

    python -m virtualenv venv_dummy
    source venv_dummy/bin/activate
    
  2. To install virtualenv-clone:

    (venv_dummy): pip install virtualenv-clone
    
  3. 复制venv1venv2:

    (venv_dummy): virtualenv-clone venv1/ venv2/
    

If you are using Anaconda you can just run:

conda create --name myclone --clone myenv

This will copy myenv to the newly created environment called myclone.

这里,我去指挥克隆的假环境。

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

Conventions used in above command:

  • source-path = path to env that you want to clone e.g. /home/john/envs/oldenv.
  • env-name = name of the cloned env e.g. myenv, it can be a path as well e.g. /home/john/envs/myenv
  • target-path = path to new cloned env e.g. /home/john/envs/<env-name>

采用这一方法的优点,或为何更愿意这样做

  1. No need to generate a requirements.txt file.
  2. No environment is activated/deactivated during cloning process.
  3. single command to be executed(3 commands ran at once).

在某些情况下,你可能希望将全球包裹从克隆技术中删除,而你可以取代<条码>源-path/bin/pipzen>/code>,代之以<条码>>-当地here

In case you use pip "venv". I copy pasted the folder holding the virtual environment and manually changed the files in the bin folder of the copied folder. I don t know if its efficient,but it works!

Can you not simply:

  • Copy the existing virtual env directory to a new one
  • Update to the new Django?

pip works, but it s a problem on a computer without internet.

I wrote a small code for this, it worked for me. I m writing it here because maybe it will be useful for someone else.

(注:我用Windows 测试)

  1. copy project folder
  2. paste project folder to another directory
  3. change the address parts in this code and run the code:
import os

# The new address of our script folder
script_folder = r D:Python proqramspdf_to_excelvenvScripts 

# the old address of our venv folder
old_path = rb C:UsersAVG-dellDesktoppdf_to_excelvenv 

# the new address of our venv folder
new_path = rb"D:Python proqramspdf_to_excelvenv"


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 )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...