标签归档:virtualenv

bash:mkvirtualenv:找不到命令

问题:bash:mkvirtualenv:找不到命令

按照Doug Hellman的virtualenvwrapper帖子中的说明进行操作,我仍然无法启动测试环境。

[mpenning@tsunami ~]$ mkvirtualenv test
-bash: mkvirtualenv: command not found
[mpenning@tsunami ~]$

请注意,我使用WORKON_HOME的不在我的$HOME。我尝试/usr/local/bin/virtualenvwrapper.shvirtualenvwrapper安装文档中所示进行查找,但是它不存在。

如果这很重要,我正在运行CentOS 6和python 2.6.6。


# File: ~/.bash_profile
# ...

export WORKON_HOME="/opt/virtual_env/"
source "/opt/virtual_env/bin/virtualenvwrapper_bashrc"

After following the instructions on Doug Hellman’s virtualenvwrapper post, I still could not fire up a test environment.

[mpenning@tsunami ~]$ mkvirtualenv test
-bash: mkvirtualenv: command not found
[mpenning@tsunami ~]$

It should be noted that I’m using WORKON_HOME that is not in my $HOME. I tried looking for /usr/local/bin/virtualenvwrapper.sh as shown in the virtualenvwrapper installation docs, but it does not exist.

I’m running CentOS 6 and python 2.6.6, if this matters.


# File: ~/.bash_profile
# ...

export WORKON_HOME="/opt/virtual_env/"
source "/opt/virtual_env/bin/virtualenvwrapper_bashrc"

回答 0

解决方案1

由于某种原因,请virtualenvwrapper.sh安装在中/usr/bin/virtualenvwrapper.sh,而不是在下/usr/local/bin

.bash_profile作品中的以下内容…

source "/usr/bin/virtualenvwrapper.sh"
export WORKON_HOME="/opt/virtual_env/"

我的安装看起来不错,无需采购 virtualenvwrapper_bashrc

解决方案2

或者,如下所述,您可以利用virtualenvwrapper.sh外壳中已经存在的机会,PATH然后发出一个source `which virtualenvwrapper.sh`

Solution 1:

For some reason, virtualenvwrapper.sh installed in /usr/bin/virtualenvwrapper.sh, instead of under /usr/local/bin.

The following in my .bash_profile works…

source "/usr/bin/virtualenvwrapper.sh"
export WORKON_HOME="/opt/virtual_env/"

My install seems to work fine without sourcing virtualenvwrapper_bashrc

Solution 2:

Alternatively as mentioned below, you could leverage the chance that virtualenvwrapper.sh is already in your shell’s PATH and just issue a source `which virtualenvwrapper.sh`


回答 1

尝试:

source `which virtualenvwrapper.sh`

反引号是命令替换-它们将程序打印出的所有内容放入表达式中。在这种情况下,“哪个”检查$ PATH以找到virtualenvwrapper.sh并输出路径。然后,shell通过“源”读取脚本。

如果您希望每次重新启动外壳程序时都会发生这种情况,最好先从“哪个”命令中获取输出,然后将“源代码”行放入外壳程序中,如下所示:

echo "source /path/to/virtualenvwrapper.sh" >> ~/.profile

^这可能因您的外壳而略有不同。另外,请注意不要使用单个>,因为这会截断〜/ .profile:-o

Try:

source `which virtualenvwrapper.sh`

The backticks are command substitution – they take whatever the program prints out and put it in the expression. In this case “which” checks the $PATH to find virtualenvwrapper.sh and outputs the path to it. The script is then read by the shell via ‘source’.

If you want this to happen every time you restart your shell, it’s probably better to grab the output from the “which” command first, and then put the “source” line in your shell, something like this:

echo "source /path/to/virtualenvwrapper.sh" >> ~/.profile

^ This may differ slightly based on your shell. Also, be careful not to use the a single > as this will truncate your ~/.profile :-o


回答 2

我在OS X 10.9.1和python 2.7.5上遇到了相同的问题。没有问题WORKON_HOME的我,但我确实有手动添加source "/usr/local/bin/virtualenvwrapper.sh"~/.bash_profile(或~/.bashrc后我跑UNIX)pip install virtualenvwrapper

I had the same issue on OS X 10.9.1 with python 2.7.5. No issues with WORKON_HOME for me, but I did have to manually add source "/usr/local/bin/virtualenvwrapper.sh" to ~/.bash_profile (or ~/.bashrc in unix) after I ran pip install virtualenvwrapper


回答 3

执行此命令的先决条件-

  1. PIP(递归缩写,P IP nstalls P ackages)是用于安装和管理Python编写的软件包,软件包管理系统。在Python软件包索引(PyPI)中可以找到许多软件包。

    须藤apt-get install python-pip

  2. 安装虚拟环境。用于创建虚拟环境,安装彼此隔离的多个项目的软件包和依赖项。

    sudo pip安装virtualenv

  3. 安装虚拟环境包装器关于虚拟环境包装

    sudo pip安装virtualenvwrapper

安装必备组件后,您需要使虚拟环境包装器生效以创建虚拟环境。以下是步骤-

  1. 在路径变量中设置虚拟环境目录 export WORKON_HOME=(directory you need to save envs)

  2. source /usr/local/bin/virtualenvwrapper.sh -p $WORKON_HOME

如@Mike所提到的,来源`哪个virtualenvwrapper.sh`或which virtualenvwrapper.sh可用于定位virtualenvwrapper.sh文件。

最好在〜/ .bashrc中放置两行,以免每次打开新的Shell时都执行上述命令。这就是使用mkvirtualenv创建环境所需的全部

注意事项-

  • 在Ubuntu下,您可能需要以root用户身份安装virtualenv和virtualenvwrapper。只需在上面的命令前加上sudo前缀即可。
  • 根据用于安装virtualenv的过程,virtualenvwrapper.sh的路径可能会有所不同。通过运行$ find / usr -name virtualenvwrapper.sh查找合适的路径。相应地调整.bash_profile或.bashrc脚本中的行。

Prerequisites to execute this command –

  1. pip (recursive acronym of Pip Installs Packages) is a package management system used to install and manage software packages written in Python. Many packages can be found in the Python Package Index (PyPI).

    sudo apt-get install python-pip

  2. Install Virtual Environment. Used to create virtual environment, to install packages and dependencies of multiple projects isolated from each other.

    sudo pip install virtualenv

  3. Install virtual environment wrapper About virtual env wrapper

    sudo pip install virtualenvwrapper

After Installing prerequisites you need to bring virtual environment wrapper into action to create virtual environment. Following are the steps –

  1. set virtual environment directory in path variable- export WORKON_HOME=(directory you need to save envs)

  2. source /usr/local/bin/virtualenvwrapper.sh -p $WORKON_HOME

As mentioned by @Mike, source `which virtualenvwrapper.sh` or which virtualenvwrapper.sh can used to locate virtualenvwrapper.sh file.

It’s best to put above two lines in ~/.bashrc to avoid executing the above commands every time you open new shell. That’s all you need to create environment using mkvirtualenv

Points to keep in mind –

  • Under Ubuntu, you may need install virtualenv and virtualenvwrapper as root. Simply prefix the command above with sudo.
  • Depending on the process used to install virtualenv, the path to virtualenvwrapper.sh may vary. Find the appropriate path by running $ find /usr -name virtualenvwrapper.sh. Adjust the line in your .bash_profile or .bashrc script accordingly.

回答 4

使用此过程在ubuntu中创建虚拟环境

第1步

安装点子

   sudo apt-get install python-pip

第2步

安装virtualenv

   sudo pip install virtualenv

第三步

创建一个目录来存储您的virtualenvs(我使用〜/ .virtualenvs)

   mkdir ~/.virtualenvs

或使用此命令在env中安装特定版本的python

virtualenv -p /usr/bin/python3.6 venv

第4步

   sudo pip install virtualenvwrapper

第5步

   sudo nano ~/.bashrc

步骤6

在bashrc文件的末尾添加这两行代码

  export WORKON_HOME=~/.virtualenvs
  source /usr/local/bin/virtualenvwrapper.sh

步骤7

打开新终端(推荐)

步骤8

创建一个新的virtualenv

  mkvirtualenv myawesomeproject

步骤9

要在virtualenvs之间加载或切换,请使用workon命令:

  workon myawesomeproject

步骤10

要退出新的virtualenv,请使用

 deactivate

并确保使用pip vs pip3

或按照以下步骤使用python3安装虚拟环境

安装环境

python3 -m venv my-project-env

并使用以下命令激活您的虚拟环境:

source my-project-env/bin/activate

Use this procedure to create virtual env in ubuntu

step 1

Install pip

   sudo apt-get install python-pip

step 2

Install virtualenv

   sudo pip install virtualenv

step 3

Create a dir to store your virtualenvs (I use ~/.virtualenvs)

   mkdir ~/.virtualenvs

or use this command to install specific version of python in env

virtualenv -p /usr/bin/python3.6 venv

step 4

   sudo pip install virtualenvwrapper

step 5

   sudo nano ~/.bashrc

step 6

Add this two line code at the end of the bashrc file

  export WORKON_HOME=~/.virtualenvs
  source /usr/local/bin/virtualenvwrapper.sh

step 7

Open new terminal (recommended)

step 8

Create a new virtualenv

  mkvirtualenv myawesomeproject

step 9

To load or switch between virtualenvs, use the workon command:

  workon myawesomeproject

step 10

To exit your new virtualenv, use

 deactivate

and make sure using pip vs pip3

OR follow the steps below to install virtual environment using python3

Install env

python3 -m venv my-project-env

and activate your virtual environment using the following command:

source my-project-env/bin/activate

回答 5

由于我刚经历了一次阻力,所以我将尝试写两个小时前希望得到的答案。这适用于不只是想要复制粘贴解决方案的人

第一:您是否想知道为什么复制和粘贴路径对某些人有用,而对其他人却无效?**主要原因是,解决方案不同是因为Python版本2.x或3.x不同。实际上,存在与python 2或3一起工作的virtualenv和virtualenvwrapper的不同版本。如果您使用的是python 2,请像这样安装:

sudo pip install virutalenv
sudo pip install virtualenvwrapper

如果您打算使用python 3,请安装相关的python 3版本

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper

您已经成功安装了适用于python版本的软件包,并且已经全部安装好吗?好吧,尝试一下。输入workon到你的终端。您的终端将无法找到命令(workon是virtualenvwrapper的命令)。当然不会。Workon是可执行文件,只有在您加载/提供文件后才能使用virtualenvwrapper.sh。但是正式安装指南已经涵盖了这一点,对吧?在文档中说,只需打开.bash_profile并插入以下内容:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

尤其是,该命令source /usr/local/bin/virtualenvwrapper.sh似乎很有帮助,因为该命令似乎可以加载/提供所需的文件virtualenvwrapper.sh,该文件包含您要使用的所有命令,如like workonmkvirtualenv但是,是的。按照官方安装指南进行操作时,您很可能会从初始帖子中收到错误消息:mkvirtualenv: command not found。仍然找不到命令,您仍然感到沮丧。那么,这里出了什么问题?问题在于,如果您正在寻找它,则不是virtualenvwrapper.sh。简短提醒…您在这里看:

source /usr/local/bin/virtualenvwrapper.sh

但是,找到所需文件的方法非常简单。只需输入

which virtualenvwrapper

到您的终端。这将在您的PATH中搜索该文件,因为该文件很可能位于系统PATH所包含的某个文件夹中。

如果您的系统非常陌生,则所需的文件将隐藏在PATH文件夹之外。在这种情况下,您可以virtalenvwrapper.sh使用shell命令找到路径find / -name virtualenvwrapper.sh

您的结果可能看起来像这样:/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh 恭喜。You have found your missing file!。现在,您要做的就是更改.bash_profile中的一个命令。只是改变:

source "/usr/local/bin/virtualenvwrapper.sh"

至:

"/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh"

恭喜你 Virtualenvwrapper现在可以在您的系统上运行。但是您可以做一件事来增强您的解决方案。如果virtualenvwrapper.sh使用命令找到了文件,则which virtualenvwrapper.sh知道该文件位于PATH文件夹中。因此,如果只写文件名,则文件系统将假定该文件位于PATH文件夹内。因此,您不必写出完整的路径。只需输入:

source "virtualenvwrapper.sh"

而已。您不再沮丧。您已经解决了问题。希望。

Since I just went though a drag, I’ll try to write the answer I’d have wished for two hours ago. This is for people who don’t just want the copy&paste solution

First: Do you wonder why copying and pasting paths works for some people while it doesn’t work for others?** The main reason, solutions differ are different python versions, 2.x or 3.x. There are actually distinct versions of virtualenv and virtualenvwrapper that work with either python 2 or 3. If you are on python 2 install like so:

sudo pip install virutalenv
sudo pip install virtualenvwrapper

If you are planning to use python 3 install the related python 3 versions

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper

You’ve successfully installed the packages for your python version and are all set, right? Well, try it. Type workon into your terminal. Your terminal will not be able to find the command (workon is a command of virtualenvwrapper). Of course it won’t. Workon is an executable that will only be available to you once you load/source the file virtualenvwrapper.sh. But the official installation guide has you covered on this one, right?. Just open your .bash_profile and insert the following, it says in the documentation:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

Especially the command source /usr/local/bin/virtualenvwrapper.sh seems helpful since the command seems to load/source the desired file virtualenvwrapper.sh that contains all the commands you want to work with like workon and mkvirtualenv. But yeah, no. When following the official installation guide, you are very likely to receive the error from the initial post: mkvirtualenv: command not found. Still no command is being found and you are still frustrated. So whats the problem here? The problem is that virtualenvwrapper.sh is not were you are looking for it right now. Short reminder … you are looking here:

source /usr/local/bin/virtualenvwrapper.sh

But there is a pretty straight forward way to finding the desired file. Just type

which virtualenvwrapper

to your terminal. This will search your PATH for the file, since it is very likely to be in some folder that is included in the PATH of your system.

If your system is very exotic, the desired file will hide outside of a PATH folder. In that case you can find the path to virtalenvwrapper.sh with the shell command find / -name virtualenvwrapper.sh

Your result may look something like this: /Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh Congratulations. You have found your missing file!. Now all you have to do is changing one command in your .bash_profile. Just change:

source "/usr/local/bin/virtualenvwrapper.sh"

to:

"/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh"

Congratulations. Virtualenvwrapper does now work on your system. But you can do one more thing to enhance your solution. If you’ve found the file virtualenvwrapper.sh with the command which virtualenvwrapper.sh you know that it is inside of a folder of the PATH. So if you just write the filename, your file system will assume the file is inside of a PATH folder. So you you don’t have to write out the full path. Just type:

source "virtualenvwrapper.sh"

Thats it. You are no longer frustrated. You have solved your problem. Hopefully.


回答 6

为了virtualenvwrapper在Ubuntu 18.04.3上成功安装,您需要执行以下操作:

  1. 安装 virtualenv

    sudo apt install virtualenv
  2. 安装 virtualenvwrapper

    sudo pip install virtualenv
    sudo pip install virtualenvwrapper
  3. 将以下内容添加到.bashrc文件末尾

    export WORKON_HOME=~/virtualenvs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
    source ~/.local/bin/virtualenvwrapper.sh
  4. 执行.bashrc文件

    source ~/.bashrc
  5. 创建您的virtualenv

    mkvirtualenv your_virtualenv

In order to successfully install the virtualenvwrapper on Ubuntu 18.04.3 you need to do the following:

  1. Install virtualenv

    sudo apt install virtualenv
    
  2. Install virtualenvwrapper

    sudo pip install virtualenv
    sudo pip install virtualenvwrapper
    
  3. Add the following to the end of the .bashrc file

    export WORKON_HOME=~/virtualenvs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
    source ~/.local/bin/virtualenvwrapper.sh
    
  4. Execute the .bashrc file

    source ~/.bashrc
    
  5. Create your virtualenv

    mkvirtualenv your_virtualenv
    

回答 7

在Windows 7和Git Bash上,这可以帮助我:

  1. 创建一个〜/ .bashrc文件(在用户主文件夹下)
  2. 添加行导出WORKON_HOME = $ HOME / .virtualenvs(如果此文件夹不存在,则必须创建此文件夹)
  3. 添加行源“ C:\ Program Files(x86)\ Python36-32 \ Scripts \ virtualenvwrapper.sh”(更改virtualenvwrapper.sh的路径)

现在重新启动 git bash和mkvirtualenv命令将可以正常工作。

On Windows 7 and Git Bash this helps me:

  1. Create a ~/.bashrc file (under your user home folder)
  2. Add line export WORKON_HOME=$HOME/.virtualenvs (you must create this folder if it doesn’t exist)
  3. Add line source “C:\Program Files (x86)\Python36-32\Scripts\virtualenvwrapper.sh” (change path for your virtualenvwrapper.sh)

Restart your git bash and mkvirtualenv command now will work nicely.


回答 8

在Windows 10和Windows的Python36上使用Git Bash,我在稍微不同的位置找到了virtualenvwrapper.sh,运行此命令解决了该问题

source virtualenvwrapper.sh 
/c/users/[myUserName]/AppData/Local/Programs/Python36/Scripts

Using Git Bash on Windows 10 and Python36 for Windows I found the virtualenvwrapper.sh in a slightly different place and running this resolved the issue

source virtualenvwrapper.sh 
/c/users/[myUserName]/AppData/Local/Programs/Python36/Scripts

回答 9

通过在〜/ .bash_profile(或unix中的〜/ .bashrc)文件中添加以下两行,解决了我在python 2.7.6的Ubuntu 14.04 OS中的问题。

源“ /usr/local/bin/virtualenvwrapper.sh”

导出WORKON_HOME =“ / opt / virtual_env /”

然后将这两行都执行到终端上。

Solved my issue in Ubuntu 14.04 OS with python 2.7.6, by adding below two lines into ~/.bash_profile (or ~/.bashrc in unix) files.

source “/usr/local/bin/virtualenvwrapper.sh”

export WORKON_HOME=”/opt/virtual_env/”

And then executing both these lines onto the terminal.


回答 10

在Windows 10上,要创建虚拟环境,我将“ pip mkvirtualenv myproject” 替换为“ mkvirtualenv myproject”,并且效果很好。

On Windows 10, to create the virtual environment, I replace “pip mkvirtualenv myproject” by “mkvirtualenv myproject” and that works well.


同时安装Anacondas 2.7和3.5可以吗?

问题:同时安装Anacondas 2.7和3.5可以吗?

我目前正在将Anaconda与Python 2.7一起使用,但是我将需要使用Python 3.5。可以同时安装它们吗?我应该期待一些问题吗?
我使用的是64位Win8。

I am using currently Anaconda with Python 2.7, but I will need to use Python 3.5. Is it ok to have them installed both in the same time? Should I expect some problems?
I am on a 64-bit Win8.


回答 0

我的理解是,您无需再次安装Anaconda即可开始使用其他版本的python。相反,conda 可以单独管理python 2和3环境

My understanding is you don’t need to install Anaconda again to start using a different version of python. Instead, conda has the ability to separately manage python 2 and 3 environments.


回答 1

我会同时使用这两种方法,具体取决于我所帮助的部门(有些人喜欢2.7,有些人则喜欢3.5)。无论如何,我使用Anaconda,默认安装为3.5。我将环境用于其他版本的python,软件包等。因此,例如,当我想开始使用python 2.7时,我运行了:

 conda create -n Python27 python=2.7

这将创建一个名为Python27的新环境并安装Python版本2.7。您可以在该行中添加参数以默认情况下安装其他软件包,也可以只是从头开始。该环境将自动激活,只需在命令行中键入deactivate(windows)或source deactivate(linux,osx)即可停用。要在以后激活,请键入activate Python27(windows)或source activate Python27(linux,osx)。如果您选择采用那条路线,我建议您阅读Anaconda中的管理环境文档。

更新资料

conda4.6版开始,您现在可以使用conda activateconda deactivate。采用source现在已被弃用,最终将被删除。

I use both depending on who in my department I am helping (Some people prefer 2.7, others 3.5). Anyway, I use Anaconda and my default installation is 3.5. I use environments for other versions of python, packages, etc.. So for example, when I wanted to start using python 2.7 I ran:

 conda create -n Python27 python=2.7

This creates a new environment named Python27 and installs Python version 2.7. You can add arguments to that line for installing other packages by default or just start from scratch. The environment will automatically activate, to deactivate simply type deactivate (windows) or source deactivate (linux, osx) in the command line. To activate in the future type activate Python27 (windows) or source activate Python27 (linux, osx). I would recommend reading the documentation for Managing Environments in Anaconda, if you choose to take that route.

Update

As of conda version 4.6 you can now use conda activate and conda deactivate. The use of source is now deprecated and will eventually be removed.


回答 2

是的你可以。

您不必都下载两个Anaconda。

只有您需要下载Anaconda版本之一,并且需要激活其他版本的Anaconda python。

如果您拥有Python 3,则可以这样设置Python 2内核;

python2 -m pip install ipykernel

python2 -m ipykernel install --user

如果您有Python 2,

python3 -m pip install ipykernel

python3 -m ipykernel install --user

然后,您将能够看到两个版本的Python!

如果您正在使用Anaconda Spyder,则应在此处交换版本:

在此处输入图片说明

如果您使用的是木星,请在这里检查:

在此处输入图片说明

注意:如果安装后Jupiter或Anaconda已打开,则需要重新启动。然后您将能够看到。

Yes you can.

You don’t have to download both Anaconda.

Only you need to download one of the version of Anaconda and need activate other version of Anaconda python.

If you have Python 3, you can set up a Python 2 kernel like this;

python2 -m pip install ipykernel

python2 -m ipykernel install --user

If you have Python 2,

python3 -m pip install ipykernel

python3 -m ipykernel install --user

Then you will be able to see both version of Python!

If you are using Anaconda Spyder then you should swap version here:

enter image description here

If you are using Jupiter then check here:

enter image description here

Note: If your Jupiter or Anaconda already open after installation you need to restart again. Then you will be able to see.


回答 3

我已经安装了python 2.7.13和3.6.2。首先为python 3安装Anaconda,然后可以使用conda语法获得2.7。我的安装使用了:conda create -n py27 python = 2.7.13 anaconda

I have python 2.7.13 and 3.6.2 both installed. Install Anaconda for python 3 first and then you can use conda syntax to get 2.7. My install used: conda create -n py27 python=2.7.13 anaconda


回答 4

是的,可以同时安装两个版本。如今,实际上已经很期待了。2.7中编写了很多东西,但是3.5正在成为规范。我建议您尽快将所有python更新到3.5。

Yes, It should be alright to have both versions installed. It’s actually pretty much expected nowadays. A lot of stuff is written in 2.7, but 3.5 is becoming the norm. I would recommend updating all your python to 3.5 ASAP, though.


回答 5

Anaconda是根据您的要求制造的。它也是环境经理。它分离出环境。之所以这样做,是因为较新/不稳定的宿主语言版本不支持稳定和旧版软件包。因此,需要一种可以在同一台计算机上分离和管理这些版本的软件,而无需重新安装或卸载单个主机编程语言/环境。

您可以在Anaconda文档中找到环境的创建/删除。

希望这会有所帮助。

Anaconda is made for the purpose you are asking. It is also an environment manager. It separates out environments. It was made because stable and legacy packages were not supported with newer/unstable versions of host languages; therefore a software was required that could separate and manage these versions on the same machine without the need to reinstall or uninstall individual host programming languages/environments.

You can find creation/deletion of environments in the Anaconda documentation.

Hope this helped.


如何在虚拟环境中运行Spyder?

问题:如何在虚拟环境中运行Spyder?

我一直在使用随Anaconda发行版安装的Spyder,后者使用Python 2.7作为默认值。当前,我需要使用Python 3.4设置开发虚拟环境。

经过网上研究后,最重要的两个建议是:

  1. 首先建立虚拟环境并指出改变Spyder的偏好,例如在这里 ;
  2. 在虚拟环境本身中安装所有Spyder依赖项,例如PyQt4,例如在这里

两项建议都很繁琐,看起来也不是明智的开发选择。

有没有一种解决方案可以在激活所需的虚拟环境后自动使用所需的Python版本运行Spyder?

I have been using Spyder installed with with Anaconda distribution which uses Python 2.7 as default. Currently I need to set up a development virtual environment with Python 3.4.

Top two suggestions after research online are:

  1. to set up virtual environment first and to point change the preferences of Spyder , e.g here;
  2. to install all Spyder dependencies, like PyQt4, in the virtual environment itself, e. g. here ;

Both recommendations are cumbersome and do not look like smart options for development.

Is there a solution that would allow to run Spyder with required Python version automatically after activating the required virtual environment?


回答 0

这是在2020年实现的快速方法。使用Anaconda Navigator:

  1. 打开Anaconda Navigator
  2. 根据需要创建新环境。我将此环境命名为“测试”。单击它将其激活。

在此处输入图片说明

  1. 转到“主页”,然后在Spyder框下单击“安装”。

在此处输入图片说明

  1. 点击“启动/运行”

设置环境时,仍然存在一些小错误(大多数问题可以通过重新启动Navigator来解决),如果发现错误,请将其发布到Anaconda Issues bug-tracker中。谢谢。


如果仍然无法解决问题,导航器仍然有用,单击>环境会将您带到该环境中安装的模块的管理窗口,搜索并选择与spyder相关的模块,然后单击应用将安装它们并他们的依赖性。

在此处输入图片说明

Here is a quick way to do it in 2020. Using the Anaconda Navigator:

  1. Open Anaconda Navigator
  2. Create your new environment as you wish. I named this environment “test”. Click on it to activate it.

enter image description here

  1. Go to “Home” and click on “Install” under the Spyder box.

enter image description here

  1. Click “Launch/Run”

There are still some minor bugs when setting up your environment (most of which are solved by restarting the Navigator), if you find a bug, please post it in Anaconda Issues bug-tracker. Thanks.


If even then this doesn’t work, Navigator could still be useful, clicking on >Enviroments takes you to a management window for the modules installed on such enviroment, searching and selecting the spyder related ones, and then clicking on Apply will install them and their dependencies.

enter image description here


回答 1

有一个选项可以使用所需的Python版本在Anaconda中创建虚拟环境

conda create -n myenv python=3.4

要激活它:

source activate myenv   # (in linux, you can use . as a shortcut for "source")
activate myenv          # (in windows - note that you should be in your c:\anaconda2 directory)

更新。我已经在Ubuntu 18.04上对其进行了测试。现在,您必须使用此命令为新环境另外安装spyder(在使用上述命令激活环境之后):

conda install spyder

(我也用pip测试了安装,但是对于Python 3.4或更早版本,它会因需要手动安装的库依赖关系错误而中断。)

现在,要在Python 3.4中运行Spyder,只需键入:

spyder

Spyder与Python 3.4

读者编辑:

对于一个正常的开立,使用“Python提示符”> > activate myenvspyder那么“巨蟒提示”必须保持开放的,你不能将其用于其他命令和强制关闭将关闭Spyder的)。当然,这要比长时间加载“ Anaconda Navigator”>切换环境>启动Spyder(@adelriosantiago的答案)更快。

There is an option to create virtual environments in Anaconda with required Python version.

conda create -n myenv python=3.4

To activate it :

source activate myenv   # (in linux, you can use . as a shortcut for "source")
activate myenv          # (in windows - note that you should be in your c:\anaconda2 directory)

UPDATE. I have tested it with Ubuntu 18.04. Now you have to install spyder additionally for the new environment with this command (after the activation of the environment with the command above):

conda install spyder

(I have also tested the installation with pip, but for Python 3.4 or older versions, it breaks with the library dependencies error that requires manual installation.)

And now to run Spyder with Python 3.4 just type:

spyder

Spyder with Python 3.4

EDIT from a reader:

For a normal opening, use “Anaconda Prompt” > activate myenv > spyder (then the “Anaconda Prompt” must stay open, you cannot use it for other commands, and a force-close will shut down Spyder). This is of course faster than the long load of “Anaconda Navigator” > switch environment > launch Spyder (@adelriosantiago’s answer).


回答 2

tomaskazemekas的回答的附加内容:您应该通过以下方式在该虚拟环境中安装spyder:

conda install -n myenv spyder

(在Windows上,对于Linux或MacOS,您可以搜索类似的命令)

Additional to tomaskazemekas’s answer: you should install spyder in that virtual environment by:

conda install -n myenv spyder

(on Windows, for Linux or MacOS, you can search for similar commands)


回答 3

对我有用的是:

  1. 从环境中运行间谍程序(激活源后)
  2. 转到工具->首选项-> python解释器,然后从您要链接到spyder en 的env中选择python文件:/ home / you / anaconda3 / envs / your_env / bin / python

在ubuntu 16,spyder3,python3.6上工作。

What worked for me :

  1. run spyder from the environment (after source activate)
  2. go to Tools –> preferences –> python Interpreter and select the python file from the env you want to link to spyder ex : /home/you/anaconda3/envs/your_env/bin/python

Worked on ubuntu 16, spyder3, python3.6.


回答 4

在所有环境中都无需重新安装spyder的情况下,请遵循此处的官方参考。

总结(用conda测试):

  • Spyder应该安装在基本环境中

在系统提示下:

  • 创建一个新的环境。请注意,根据创建方式(conda,virtualenv),环境文件夹将位于系统上的其他位置)

  • 激活环境(例如conda activate [yourEnvName]

  • 在环境中安装间谍内核(例如conda install spyder-kernels

  • 在环境中查找并复制python可执行文件的路径。查找此路径可以使用提示符以下命令来完成python -c "import sys; print(sys.executable)"

  • 停用环境(即返回基地conda deactivate

  • 运行间谍(spyder3

  • 最后,在spyder的“工具”菜单中,转到“首选项”>“ Python解释器”>“使用以下解释器”,然后粘贴环境python可执行文件路径。

  • 重新启动ipython控制台

PS:在间谍中,您应该在底部看到这样的内容在此处输入图片说明

沃伊拉

To do without reinstalling spyder in all environments follow official reference here.

In summary (tested with conda):

  • Spyder should be installed in the base environment

From the system prompt:

  • Create an new environment. Note that depending on how you create it (conda, virtualenv) the environment folder will be located at different place on your system)

  • Activate the environment (e.g., conda activate [yourEnvName])

  • Install spyder-kernels inside the environment (e.g., conda install spyder-kernels)

  • Find and copy the path for the python executable inside the environment. Finding this path can be done using from the prompt this command python -c "import sys; print(sys.executable)"

  • Deactivate the environment (i.e., return to base conda deactivate)

  • run spyder (spyder3)

  • Finally in spyder Tool menu go to Preferences > Python Interpreter > Use the following interpreter and paste the environment python executable path

  • Restart the ipython console

PS: in spyder you should see at the bottom something like thisenter image description here

Voila


回答 5

上面的答案是正确的,但是我spyder在virtualenv中调用仍会使用PATH默认的anaconda env查找spyder的版本。我发现此答案提供了以下解决方法:

source activate my_env            # activate your target env with spyder installed
conda info -e                     # look up the directory of your conda env
find /path/to/my/env -name spyder # search for the spyder executable in your env
/path/to/my/env/then/to/spyder    # run that executable directly

我选择此方法的目的PATH是优先于修改或添加指向可执行文件的链接,PATH因为我认为这样做不太可能破坏其他程序。但是,我确实在中为可执行文件添加了别名~/.bash_aliases

The above answers are correct but I calling spyder within my virtualenv would still use my PATH to look up the version of spyder in my default anaconda env. I found this answer which gave the following workaround:

source activate my_env            # activate your target env with spyder installed
conda info -e                     # look up the directory of your conda env
find /path/to/my/env -name spyder # search for the spyder executable in your env
/path/to/my/env/then/to/spyder    # run that executable directly

I chose this over modifying PATH or adding a link to the executable at a higher priority in PATH since I felt this was less likely to break other programs. However, I did add an alias to the executable in ~/.bash_aliases.


回答 6

我只是在尝试使Spyder在虚拟环境中运行时遇到相同的问题。

解决方案很简单:

激活您的虚拟环境。

然后在您的虚拟环境中pip安装Spyder及其依赖项(PyQt5)。

然后从您的虚拟环境CLI启动Spyder3。

现在对我来说很好。

I just had the same problem trying to get Spyder to run in Virtual Environment.

The solution is simple:

Activate your virtual environment.

Then pip install Spyder and its dependencies (PyQt5) in your virtual environment.

Then launch Spyder3 from your virtual environment CLI.

It works fine for me now.


回答 7

在Windows上:

您可以创建一个快捷方式执行

Anaconda3\pythonw.exe Anaconda3\cwp.py Anaconda3\envs\<your_env> Anaconda3\envs\<your env>\pythonw.exe Anaconda3\envs\<your_env>\Scripts\spyder-script.py

但是,如果您从Anaconda外壳中的venv启动spyder,我相信它会为您创建此快捷方式(只需在Windows上搜索“ Spyder”)。

On Windows:

You can create a shortcut executing

Anaconda3\pythonw.exe Anaconda3\cwp.py Anaconda3\envs\<your_env> Anaconda3\envs\<your env>\pythonw.exe Anaconda3\envs\<your_env>\Scripts\spyder-script.py

However, if you started spyder from your venv inside Anaconda shell, it creates this shortcut for you automatically in the Windows menu. The steps:

  1. install spyder in your venv using the methods mentioned in the other answers here.

  2. (in anaconda:) activate testenv

  3. Look up the windows menu “recently added” or just search for “spyder” in the windows menu, find spyder (testenv) and

  • [add that to taskbar] and / or

  • [look up the file source location] and copy that to your desktop, e.g. from C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit), where the spyder links for any of my environments can be found.

Now you can directly start spyder from a shortcut without the need to open anaconda prompt.


回答 8

我遵循上述建议之一,并且确实有效。总而言之,使用上述建议在Ubuntu上下载Anaconda时,可以帮助您“创建”环境。在我的情况下,下载Spyder的默认值为:(base)smith @ ubuntu〜$。创建环境(即fenics并使用$ conda激活它)后,立即激活(fenics)smith @ ubuntu〜$。然后从该提示启动Spyder,即$ spyder然后您的系统打开Spyder IDE,您可以在其上编写fenics代码。记住,每次打开终端时,系统都会打开默认提示。您必须激活您的包装所在的环境,并迅速更改它,即(fenics)。我希望这将有所帮助。谢谢您以及所有提供帮助的人。这个社区很棒。

I follow one of the advice above and indeed it works. In summary while you download Anaconda on Ubuntu using the advice given above can help you to ‘create’ environments. The default when you download Spyder in my case is: (base) smith@ubuntu ~$. After you create the environment, i.e. fenics and activate it with $ conda activate fenics the prompt change to (fenics) smith@ubuntu ~$. Then you launch Spyder from this prompt, i.e $ spyder and your system open the Spyder IDE, and you can write fenics code on it. Remember every time you open a terminal your system open the default prompt. You have to activate your environment where your package is and the prompt change to it i.e. (fenics). I Hope this will help. Thank you as well as all the people who help. This community is great.


Ruby相当于virtualenv吗?

问题:Ruby相当于virtualenv吗?

是否有类似于Python实用程序virtualenv的东西?

基本上,它允许您将Python软件包安装到沙盒环境中,因此easy_install django它不会进入系统范围的site-packages目录中,而是会进入virtualenv创建的目录中。

例如:

$ virtualenv test
New python executable in test/bin/python
Installing setuptools...cd .........done.
$ cd test/
$ source bin/activate
(test)$ easy_install tvnamer
Searching for tvnamer
Best match: tvnamer 0.5.1
Processing tvnamer-0.5.1-py2.5.egg
Adding tvnamer 0.5.1 to easy-install.pth file
Installing tvnamer script to /Users/dbr/test/bin

Using /Library/Python/2.5/site-packages/tvnamer-0.5.1-py2.5.egg
Processing dependencies for tvnamer
Finished processing dependencies for tvnamer
(test)$ which tvnamer 
/Users/dbr/test/bin/tvnamer

RubyGems是否有类似的东西?

Is there something similar to the Python utility virtualenv?

Basically it allows you to install Python packages into a sandboxed environment, so easy_install django doesn’t go in your system-wide site-packages directory, it would go in the virtualenv-created directory.

For example:

$ virtualenv test
New python executable in test/bin/python
Installing setuptools...cd .........done.
$ cd test/
$ source bin/activate
(test)$ easy_install tvnamer
Searching for tvnamer
Best match: tvnamer 0.5.1
Processing tvnamer-0.5.1-py2.5.egg
Adding tvnamer 0.5.1 to easy-install.pth file
Installing tvnamer script to /Users/dbr/test/bin

Using /Library/Python/2.5/site-packages/tvnamer-0.5.1-py2.5.egg
Processing dependencies for tvnamer
Finished processing dependencies for tvnamer
(test)$ which tvnamer 
/Users/dbr/test/bin/tvnamer

Is there something like this for RubyGems?


回答 0

RVM的工作方式与virtualenv的工作方式更为接近,因为它可以让您沙盒化不同的红宝石版本及其宝石等。

RVM works closer to how virtualenv works since it lets you sandbox different ruby versions and their gems, etc.


回答 1

沙箱,RVM和rbenv都不管理应用程序的gem依赖项的版本。该工具是bundler

  • 使用Gemfile作为应用程序的依赖项声明
  • 用于bundle install将这些依赖项的显式版本安装到隔离的位置
  • 用于bundle exec运行您的应用程序

Neither sandbox, RVM, nor rbenv manage the versions of your app’s gem dependencies. The tool for that is bundler.

  • use a Gemfile as your application’s dependency declaration
  • use bundle install to install explicit versions of these dependencies into an isolated location
  • use bundle exec to run your application

回答 2

似乎没有人提到rbenv

No one seems to have mentioned rbenv.


回答 3

我想您会喜欢沙盒

I think you’ll like sandbox.


回答 4

我将提到使用Bundler进行此操作的方式(我将其与RVM一起使用-RVM用于管理红宝石和一组默认的全局宝石,Bundler则用于处理项目特定的宝石)

bundler install --binstubs --path vendor

在项目的根目录中运行此命令将安装Gemfile中列出的gems,将libs放入中./vendor./bin并且所有requires 中的所有可执行文件(如果使用bundle console或Bundler要求)将引用这些exes和libs。

为我工作。

I’ll mention the way I do this with Bundler (which I use with RVM – RVM to manage the rubies and a default set of global gems, Bundler to handle project specific gems)

bundler install --binstubs --path vendor

Running this command in the root of a project will install the gems listed from your Gemfile, put the libs in ./vendor, and any executables in ./bin and all requires (if you use bundle console or the Bundler requires) will reference these exes and libs.

Works for me.


回答 5

如果仅需要以非root用户身份安装gem,请尝试设置GEM_HOME环境变量。然后运行gem

例如:

$ export GEM_HOME=$HOME/local/gems
$ gem install rhc

If you only need to install gems as non-root, try setting the GEM_HOME environment variable. Then just run gem.

For example:

$ export GEM_HOME=$HOME/local/gems
$ gem install rhc

回答 6

我推荐direnv。它是外壳的环境切换器。

在每个提示之前,它会检查当前目录和父目录中是否存在“ .envrc”文件。如果文件存在(并已授权),则将其加载到bash子shell中,然后所有导出的变量都由direnv捕获,然后使当前shell可用。

这是如何在 ruby-install中使用direnv

+红宝石安装

将此添加到 ~/.direnvrc

use_ruby() {
  local ruby_root=$HOME/.rubies/$1
  load_prefix "$ruby_root"
  layout_ruby
}

安装ruby-install(brew install ruby-install)并安装一堆Ruby 。

ruby-install ruby 1.9.3
ruby-install ruby 2.0.0
ruby-install ruby 2.2.0

为了方便起见,然后进行几个符号链接:

ln -s .rubies/1.9 ruby-1.9.3-p*
ln -s .rubies/2.0 ruby-2.0.0
ln -s .rubies/2.2 ruby-2.2.0

最后在任何项目中.envrc

use ruby 2.0

这会将所有gem放置在项目.direnv/ruby目录下(使打开gem更加容易)。捆绑程序将放入包装二进制文件 .direnv/bin(不再bundle exec!)。

+ rbenv

也可以通过use rbenv在任何.envrc文件中添加命令来使用rbenv 。这将激活rbenv,从而将红宝石包装器放入PATH中。

请注意,不必在.bashrc或.zshrc中安装rbenv即可起作用。

+ RVM

这是我在ruby项目上使用的最复杂的.envrc:

rvm use 1.8.7
layout ruby
PATH_add .direnv/bundler-bin

rvm用于为您选择正确的红宝石版本

布局命令会自动设置一些常用的环境变量。目前仅存在红宝石布局。它的作用是设置GEM_HOME环境变量,并将它放在bin目录中。因为它取决于Ruby版本,所以请确保在“ rvm”之后调用它。由于每个ruby布局目录都有其自己的GEM_HOME,因此您无需使用rvm的gemset。

PATH_add前置并扩展给定的相对路径。在这种情况下,我使用它来将捆绑程序binstub与我自己的bin脚本隔离,bundle install --binstubs .direnv/bundler-bin

如果您想了解这些命令的确切功能,现在:cat direnv stdlib| 减

I recommend direnv. It is an environment switcher for the shell.

Before each prompt it checks for the existence of an “.envrc” file in the current and parent directories. If the file exists (and authorized), it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available the current shell.

Here is how to use direnv with ruby-install

+ ruby-install

Add this to the ~/.direnvrc

use_ruby() {
  local ruby_root=$HOME/.rubies/$1
  load_prefix "$ruby_root"
  layout_ruby
}

Install ruby-install (brew install ruby-install) and install a bunch of rubies.

ruby-install ruby 1.9.3
ruby-install ruby 2.0.0
ruby-install ruby 2.2.0

And then make a couple of symlinks for convenience:

ln -s .rubies/1.9 ruby-1.9.3-p*
ln -s .rubies/2.0 ruby-2.0.0
ln -s .rubies/2.2 ruby-2.2.0

And finally in any project’s .envrc:

use ruby 2.0

This will put all gems under the project’s .direnv/ruby directory (makes opening gems easier). bundler will put wrapper binaries in .direnv/bin (no more bundle exec!).

+ rbenv

It’s also possible to use rbenv by adding the use rbenv command in any .envrc file. This will activate rbenv which in turn will put the ruby wrappers in the PATH.

Note that it’s not necessary to install rbenv in the .bashrc or .zshrc for this to work.

+ RVM

Here is the most complicated .envrc that I use on ruby projects:

rvm use 1.8.7
layout ruby
PATH_add .direnv/bundler-bin

rvm is used to select the right ruby version for you

layout commands automatically set some of the usual environment variables. For now only the ruby layout exists. What it does is set the GEM_HOME environment variable and it’s bin directory to your path. Because it depends on the ruby version, make sure to call it after “rvm”. Since each ruby layout directories have their own GEM_HOME, you don’t need to use rvm’s gemsets.

PATH_add prepends and expands the given relative path. In that case, I use this to segregate the bundler binstubs from my own bin scripts with bundle install --binstubs .direnv/bundler-bin

If you want to find out what those commands exactly do, for now: cat direnv stdlib | less


回答 7

Mineshaft是一个我已经从事了一段时间的项目,并将继续进行开发工作。

它提供了创建类似于virtualenv工作原理的虚拟环境的能力,并且还可以全局安装Ruby。

Mineshaft is a project that I’ve been working on for some time and am continuing development work on.

It offers the ability to both create virtual environments akin to how virtualenv works and can also install Ruby globally as well.


PyLint“无法导入”错误-如何设置PYTHONPATH?

问题:PyLint“无法导入”错误-如何设置PYTHONPATH?

我正在Windows的Wing IDE中运行PyLint。我的项目中有一个子目录(程序包),在程序包中,我从顶层(即)导入模块。

__init__.py
myapp.py
one.py
subdir\
    __init__.py
    two.py

two.py我的内部import one,这在运行时效果很好,因为顶层目录(从中myapp.py运行)位于Python路径中。但是,当我在two.py上运行PyLint时,出现了一个错误:

F0401: Unable to import 'one'

我该如何解决?

I’m running PyLint from inside Wing IDE on Windows. I have a sub-directory (package) in my project and inside the package I import a module from the top level, ie.

__init__.py
myapp.py
one.py
subdir\
    __init__.py
    two.py

Inside two.py I have import one and this works fine at runtime, because the top-level directory (from which myapp.py is run) is in the Python path. However, when I run PyLint on two.py it gives me an error:

F0401: Unable to import 'one'

How do I fix this?


回答 0

我知道有两个选择。

一,更改PYTHONPATH环境变量,使其包含模块上方的目录。

或者,编辑~/.pylintrc以包括模块上方的目录,如下所示:

[MASTER]
init-hook='import sys; sys.path.append("/path/to/root")'

(或者在其他版本的pylint中,init-hook要求您将[General]更改为[MASTER])

这两个选项都应该起作用。

希望有帮助。

There are two options I’m aware of.

One, change the PYTHONPATH environment variable to include the directory above your module.

Alternatively, edit ~/.pylintrc to include the directory above your module, like this:

[MASTER]
init-hook='import sys; sys.path.append("/path/to/root")'

(Or in other version of pylint, the init-hook requires you to change [General] to [MASTER])

Both of these options ought to work.

Hope that helps.


回答 1

更改路径的解决方案init-hook很好,但是我不喜欢必须在此处添加绝对路径的事实,因此,我无法在项目开发人员之间共享此pylintrc文件。这种使用相对路径指向pylintrc文件的解决方案对我来说效果更好:

[MASTER]
init-hook="from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

请注意,pylint.config.PYLINTRC该值也存在,并且具有与相同的值find_pylintrc()

The solution to alter path in init-hook is good, but I dislike the fact that I had to add absolute path there, as result I can not share this pylintrc file among the developers of the project. This solution using relative path to pylintrc file works better for me:

[MASTER]
init-hook="from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

Note that pylint.config.PYLINTRC also exists and has the same value as find_pylintrc().


回答 2

1)sys.path是一个列表。

2)问题有时是sys.path不是您的virtualenv.path,并且您想在virtualenv中使用pylint

3)像这样说,使用init-hook(注意’和’pylint的解析是严格的)

[Master]
init-hook='sys.path = ["/path/myapps/bin/", "/path/to/myapps/lib/python3.3/site-packages/", ... many paths here])'

要么

[Master]
init-hook='sys.path = list(); sys.path.append("/path/to/foo")'

..和

pylint --rcfile /path/to/pylintrc /path/to/module.py

1) sys.path is a list.

2) The problem is sometimes the sys.path is not your virtualenv.path and you want to use pylint in your virtualenv

3) So like said, use init-hook (pay attention in ‘ and ” the parse of pylint is strict)

[Master]
init-hook='sys.path = ["/path/myapps/bin/", "/path/to/myapps/lib/python3.3/site-packages/", ... many paths here])'

or

[Master]
init-hook='sys.path = list(); sys.path.append("/path/to/foo")'

.. and

pylint --rcfile /path/to/pylintrc /path/to/module.py

回答 3

可以通过在venv下配置pylint路径来解决该问题:$ cat .vscode / settings.json

{
    "python.pythonPath": "venv/bin/python",
    "python.linting.pylintPath": "venv/bin/pylint"
}

The problem can be solved by configuring pylint path under venv: $ cat .vscode/settings.json

{
    "python.pythonPath": "venv/bin/python",
    "python.linting.pylintPath": "venv/bin/pylint"
}

回答 4

您是否__init__.py在两个目录中都有一个空文件来让python知道dirs是模块?

当您不在文件夹中运行时(例如,虽然不是我使用过的pylint),其基本轮廓是:

topdir\
  __init__.py
  functions_etc.py
  subdir\
    __init__.py
    other_functions.py

这是怎样的Python解释器知道不参考当前目录中的模块,因此,如果pylint的是从自身的绝对路径运行它就能访问functions_etc.pytopdir.functions_etctopdir.subdir.other_functions,只要topdir是对的PYTHONPATH

更新:如果问题不在于__init__.py文件,也许只是尝试将模块复制或移动到c:\Python26\Lib\site-packages-这是放置其他软件包的常见位置,并且肯定会在pythonpath上。如果您知道如何执行Windows符号链接或等效链接(我不知道!),则可以这样做。这里还有更多选项:http://docs.python.org/install/index.html,包括在开发代码的用户级目录后附加sys.path的选项,但实际上,我通常只是象征性地链接我将本地开发目录复制到站点软件包-将其复制具有相同的效果。

Do you have an empty __init__.py file in both directories to let python know that the dirs are modules?

The basic outline when you are not running from within the folder (ie maybe from pylint’s, though I haven’t used that) is:

topdir\
  __init__.py
  functions_etc.py
  subdir\
    __init__.py
    other_functions.py

This is how the python interpreter is aware of the module without reference to the current directory, so if pylint is running from its own absolute path it will be able to access functions_etc.py as topdir.functions_etc or topdir.subdir.other_functions, provided topdir is on the PYTHONPATH.

UPDATE: If the problem is not the __init__.py file, maybe just try copying or moving your module to c:\Python26\Lib\site-packages — that is a common place to put additional packages, and will definitely be on your pythonpath. If you know how to do Windows symbolic links or the equivalent (I don’t!), you could do that instead. There are many more options here: http://docs.python.org/install/index.html, including the option of appending sys.path with the user-level directory of your development code, but in practice I usually just symbolically link my local development dir to site-packages – copying it over has the same effect.


回答 5

我在此页面上找到的关于此问题的一般答案请不要打开,网站显示错误

创建.pylintrc并添加

[MASTER]
init-hook="from pylint.config import find_pylintrc;
import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

general answer for this question I found on this page PLEASE NOT OPEN, SITE IS BUGED

create .pylintrc and add

[MASTER]
init-hook="from pylint.config import find_pylintrc;
import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

回答 6

我不知道它如何与WingIDE一起使用,但是对于将PyLint与Geany一起使用,我将外部命令设置为:

PYTHONPATH=${PYTHONPATH}:$(dirname %d) pylint --output-format=parseable --reports=n "%f"

其中%f是文件名,%d是路径。可能对某人有用:)

I don’t know how it works with WingIDE, but for using PyLint with Geany, I set my external command to:

PYTHONPATH=${PYTHONPATH}:$(dirname %d) pylint --output-format=parseable --reports=n "%f"

where %f is the filename, and %d is the path. Might be useful for someone :)


回答 7

我必须更新系统PYTHONPATH变量以添加我的App Engine路径。就我而言,我只需要编辑~/.bashrc文件并添加以下行:

export PYTHONPATH=$PYTHONPATH:/path/to/google_appengine_folder

实际上,我尝试设置第init-hook一个,但这不能在我的代码库中始终解决此问题(不确定原因)。一旦将其添加到系统路径中(一般来说可能是一个好主意),我的问题就消失了。

I had to update the system PYTHONPATH variable to add my App Engine path. In my case I just had to edit my ~/.bashrc file and add the following line:

export PYTHONPATH=$PYTHONPATH:/path/to/google_appengine_folder

In fact, I tried setting the init-hook first but this did not resolve the issue consistently across my code base (not sure why). Once I added it to the system path (probably a good idea in general) my issues went away.


回答 8

我刚刚发现的一种解决方法是实际上只对整个软件包运行PyLint,而不是对单个文件运行。不知何故,它设法找到了导入的模块。

One workaround that I only just discovered is to actually just run PyLint for the entire package, rather than a single file. Somehow, it manages to find imported module then.


回答 9

尝试

if __name__ == '__main__':
    from [whatever the name of your package is] import one
else:
    import one

请注意,在Python 3中,else子句中该部分的语法为

from .. import one

再三考虑,这可能无法解决您的特定问题。我误解了这个问题,并认为two.py已作为主要模块运行,但事实并非如此。考虑到Python 2.6(不absolute_import从导入__future__)和Python 3.x处理导入的方式的差异,我认为对于Python 2.6无需这样做。

不过,如果最终您确实切换到Python 3,并计划将模块既用作包模块又用作包内的独立脚本,那么最好保留以下内容:

if __name__ == '__main__':
    from [whatever the name of your package is] import one   # assuming the package is in the current working directory or a subdirectory of PYTHONPATH
else:
    from .. import one

心里。

编辑:现在为您的实际问题提供可能的解决方案。从包含one模块的目录中运行PyLint (也许通过命令行),或者在运行PyLint时将以下代码放在某个位置:

import os

olddir = os.getcwd()
os.chdir([path_of_directory_containing_module_one])
import one
os.chdir(olddir)

基本上,作为摆弄PYTHONPATH的替代方法,只需确保当前工作目录是one.py执行导入时包含的目录。

(查看Brian的答案,您可能会将先前的代码分配给init_hook,但是如果您打算这样做,则可以简单地对其进行附加sys.path操作,这比我的解决方案要优雅得多。)

Try

if __name__ == '__main__':
    from [whatever the name of your package is] import one
else:
    import one

Note that in Python 3, the syntax for the part in the else clause would be

from .. import one

On second thought, this probably won’t fix your specific problem. I misunderstood the question and thought that two.py was being run as the main module, but that is not the case. And considering the differences in the way Python 2.6 (without importing absolute_import from __future__) and Python 3.x handle imports, you wouldn’t need to do this for Python 2.6 anyway, I don’t think.

Still, if you do eventually switch to Python 3 and plan on using a module as both a package module and as a standalone script inside the package, it may be a good idea to keep something like

if __name__ == '__main__':
    from [whatever the name of your package is] import one   # assuming the package is in the current working directory or a subdirectory of PYTHONPATH
else:
    from .. import one

in mind.

EDIT: And now for a possible solution to your actual problem. Either run PyLint from the directory containing your one module (via the command line, perhaps), or put the following code somewhere when running PyLint:

import os

olddir = os.getcwd()
os.chdir([path_of_directory_containing_module_one])
import one
os.chdir(olddir)

Basically, as an alternative to fiddling with PYTHONPATH, just make sure the current working directory is the directory containing one.py when you do the import.

(Looking at Brian’s answer, you could probably assign the previous code to init_hook, but if you’re going to do that then you could simply do the appending to sys.path that he does, which is slightly more elegant than my solution.)


回答 10

关键是在sys.path不考虑env变量的情况下将项目目录添加到其中。

对于使用VSCode的人,如果您的项目有基本目录,这是一种单线解决方案:

[MASTER]
init-hook='base_dir="my_spider"; import sys,os,re; _re=re.search(r".+\/" + base_dir, os.getcwd()); project_dir = _re.group() if _re else os.path.join(os.getcwd(), base_dir); sys.path.append(project_dir)'

让我解释一下:

  • re.search(r".+\/" + base_dir, os.getcwd()).group():根据编辑文件查找基本目录

  • os.path.join(os.getcwd(), base_dir):添加cwdsys.path满足命令行环境


仅供参考,这是我的.pylintrc:

https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f

The key is to add your project directory to sys.path without considering about the env variable.

For someone who use VSCode, here’s a one-line solution for you if there’s a base directory of your project:

[MASTER]
init-hook='base_dir="my_spider"; import sys,os,re; _re=re.search(r".+\/" + base_dir, os.getcwd()); project_dir = _re.group() if _re else os.path.join(os.getcwd(), base_dir); sys.path.append(project_dir)'

Let me explain it a little bit:

  • re.search(r".+\/" + base_dir, os.getcwd()).group(): find base directory according to the editing file

  • os.path.join(os.getcwd(), base_dir): add cwd to sys.path to meet command line environment


FYI, here’s my .pylintrc:

https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f


回答 11

我遇到了同样的问题,并通过在virtualenv中安装pylint,然后将.pylintrc文件添加到我的项目目录中并在文件中添加以下内容来修复了该问题:

[Master]
init-hook='sys.path = list(); sys.path.append("./Lib/site-packages/")'

I had this same issue and fixed it by installing pylint in my virtualenv and then adding a .pylintrc file to my project directory with the following in the file:

[Master]
init-hook='sys.path = list(); sys.path.append("./Lib/site-packages/")'

回答 12

也许通过在PYTHONPATH内手动附加目录?

sys.path.append(dirname)

Maybe by manually appending the dir inside the PYTHONPATH?

sys.path.append(dirname)

回答 13

我遇到了同样的问题,因为我找不到答案,所以我希望这可以帮助任何有类似问题的人。

我用飞配epylint。基本上我所做的就是添加一个dired-mode-hook,以检查dired目录是否为python软件包目录。如果是,我将其添加到PYTHONPATH。就我而言,如果目录包含名为“ setup.py”的文件,则我认为该目录是python软件包。

;;;;;;;;;;;;;;;;;
;; PYTHON PATH ;;
;;;;;;;;;;;;;;;;;

(defun python-expand-path ()
  "Append a directory to the PYTHONPATH."
  (interactive
   (let ((string (read-directory-name 
          "Python package directory: " 
          nil 
          'my-history)))
     (setenv "PYTHONPATH" (concat (expand-file-name string)
                  (getenv ":PYTHONPATH"))))))

(defun pythonpath-dired-mode-hook ()
  (let ((setup_py (concat default-directory "setup.py"))
    (directory (expand-file-name default-directory)))
    ;;   (if (file-exists-p setup_py)
    (if (is-python-package-directory directory)
    (let ((pythonpath (concat (getenv "PYTHONPATH") ":" 
                  (expand-file-name directory))))
      (setenv "PYTHONPATH" pythonpath)
      (message (concat "PYTHONPATH=" (getenv "PYTHONPATH")))))))

(defun is-python-package-directory (directory)
  (let ((setup_py (concat directory "setup.py")))
    (file-exists-p setup_py)))

(add-hook 'dired-mode-hook 'pythonpath-dired-mode-hook)

希望这可以帮助。

I had the same problem and since i could not find a answer I hope this can help anyone with a similar problem.

I use flymake with epylint. Basically what i did was add a dired-mode-hook that check if the dired directory is a python package directory. If it is I add it to the PYTHONPATH. In my case I consider a directory to be a python package if it contains a file named “setup.py”.

;;;;;;;;;;;;;;;;;
;; PYTHON PATH ;;
;;;;;;;;;;;;;;;;;

(defun python-expand-path ()
  "Append a directory to the PYTHONPATH."
  (interactive
   (let ((string (read-directory-name 
          "Python package directory: " 
          nil 
          'my-history)))
     (setenv "PYTHONPATH" (concat (expand-file-name string)
                  (getenv ":PYTHONPATH"))))))

(defun pythonpath-dired-mode-hook ()
  (let ((setup_py (concat default-directory "setup.py"))
    (directory (expand-file-name default-directory)))
    ;;   (if (file-exists-p setup_py)
    (if (is-python-package-directory directory)
    (let ((pythonpath (concat (getenv "PYTHONPATH") ":" 
                  (expand-file-name directory))))
      (setenv "PYTHONPATH" pythonpath)
      (message (concat "PYTHONPATH=" (getenv "PYTHONPATH")))))))

(defun is-python-package-directory (directory)
  (let ((setup_py (concat directory "setup.py")))
    (file-exists-p setup_py)))

(add-hook 'dired-mode-hook 'pythonpath-dired-mode-hook)

Hope this helps.


回答 14

如果有人在寻找一种方法来将Pylint作为PyCharm中的外部工具运行并使其与他们的虚拟环境一起工作(为什么我要问这个问题),那么我将通过以下方式解决它:

  1. 在PyCharm>首选项>工具>外部工具中,添加或编辑pylint的项目。
  2. 在“编辑工具”对话框的“工具设置”中,将“程序”设置为使用python解释器目录中的pylint: $PyInterpreterDirectory$/pylint
  3. 在“参数”字段中设置其他参数,例如: --rcfile=$ProjectFileDir$/pylintrc -r n $FileDir$
  4. 将工作目录设置为 $FileDir$

现在,使用pylint作为外部工具,可以在使用公共配置文件选择的任何目录上运行pylint,并使用为项目配置的任何解释器(大概是您的virtualenv解释器)。

In case anybody is looking for a way to run pylint as an external tool in PyCharm and have it work with their virtual environments (why I came to this question), here’s how I solved it:

  1. In PyCharm > Preferences > Tools > External Tools, Add or Edit an item for pylint.
  2. In the Tool Settings of the Edit Tool dialog, set Program to use pylint from the python interpreter directory: $PyInterpreterDirectory$/pylint
  3. Set your other parameters in the Parameters field, like: --rcfile=$ProjectFileDir$/pylintrc -r n $FileDir$
  4. Set your working directory to $FileDir$

Now using pylint as an external tool will run pylint on whatever directory you have selected using a common config file and use whatever interpreter is configured for your project (which presumably is your virtualenv interpreter).


回答 15

这是一个老问题,但是没有可接受的答案,所以我建议这样做:将two.py中的import语句更改为:

from .. import one

在我当前的环境(Python 3.6,使用pylint 2.3.1的VSCode)中,这清除了标记的语句。

This is an old question but has no accepted answer, so I’ll suggest this: change the import statement in two.py to read:

from .. import one

In my current environment (Python 3.6, VSCode using pylint 2.3.1) this clears the flagged statement.


回答 16

我找到了一个不错的答案。编辑您的pylintrc并在master中添加以下内容

init-hook="import imp, os; from pylint.config import find_pylintrc; imp.load_source('import_hook', os.path.join(os.path.dirname(find_pylintrc()), 'import_hook.py'))"

I found a nice answer. Edit your pylintrc and add the following in master

init-hook="import imp, os; from pylint.config import find_pylintrc; imp.load_source('import_hook', os.path.join(os.path.dirname(find_pylintrc()), 'import_hook.py'))"

回答 17

安装Python时,可以设置路径。如果已经定义了路径,那么您可以在VS Code中进行操作,按Ctrl + Shift + P并键入Python:选择“解释器”并选择Python的更新版本。请点击此链接以获取更多信息,https://code.visualstudio.com/docs/python/environments

When you install Python, you can set up the path. If path is already defined then what you can do is within VS Code, hit Ctrl+Shift+P and type Python: Select Interpreter and select updated version of Python. Follow this link for more information, https://code.visualstudio.com/docs/python/environments


回答 18

如果使用vscode,请确保您的软件包目录不在_pychache__目录中。

if you using vscode,make sure your package directory is out of the _pychache__ directory.


回答 19

如果您在Linux中使用Cython,则解决了删除module.cpython-XXm-X-linux-gnu.so项目目标目录中的文件的问题。

If you are using Cython in Linux, I resolved removing module.cpython-XXm-X-linux-gnu.so files in my project target directory.


回答 20

只需在.vscode / settings.json文件中添加此代码

,“ python.linting.pylintPath”:“ venv / bin / pylint”

这将通知pylint的位置(这是python的错误检查器)

just add this code in .vscode/settings.json file

,”python.linting.pylintPath”: “venv/bin/pylint”

This will notify the location of pylint(which is an error checker for python)


回答 21

您好,我能够从其他目录导入软件包。我只是做了以下事情:注意:我正在使用VScode

创建Python软件包的步骤使用Python软件包确实非常简单。您需要做的只是:

创建一个目录,并为其指定软件包的名称。将类放进去。在目录中创建一个初始化 .py文件

例如:您有一个名为Framework的文件夹,其中保存了所有自定义类,您的工作是在名为Framework的文件夹内创建一个init .py文件。

在导入时,您需要以这种方式导入->

从Framework导入库

因此E0401错误消失了,Framework是您刚在其中创建init .py 的文件夹,而base是您要自定义的模块,您需要将其导入并在该模块上工作,希望它能有所帮助!!!

Hello i was able to import the packages from different directory. I just did the following: Note: I am using VScode

Steps to Create a Python Package Working with Python packages is really simple. All you need to do is:

Create a directory and give it your package’s name. Put your classes in it. Create a init.py file in the directory

For example: you have a folder called Framework where you are keeping all the custom classes there and your job is to just create a init.py file inside the folder named Framework.

And while importing you need to import in this fashion—>

from Framework import base

so the E0401 error disappears Framework is the folder where you just created init.py and base is your custom module which you are required to import into and work upon Hope it helps!!!!


重命名virtualenv文件夹而不破坏它

问题:重命名virtualenv文件夹而不破坏它

我已经创建了文件夹并在其中初始化了virtualenv实例。

$ mkdir myproject
$ cd myproject
$ virtualenv env

当我运行时(env)$ pip freeze,它将按原样显示已安装的软件包。

现在我想重命名myproject/project/

$ mv myproject/ project/

但是,当我跑步时

$ . env/bin/activate
(env)$ pip freeze

提示未安装pip。如何在不破坏环境的情况下重命名项目文件夹?

I’ve created folder and initialized a virtualenv instance in it.

$ mkdir myproject
$ cd myproject
$ virtualenv env

When I run (env)$ pip freeze, it shows the installed packages as it should.

Now I want to rename myproject/ to project/.

$ mv myproject/ project/

However, now when I run

$ . env/bin/activate
(env)$ pip freeze

it says pip is not installed. How do I rename the project folder without breaking the environment?


回答 0

您需要调整安装以使用相对路径。virtualenv为此提供了--relocatable选项。从文档

通常,环境绑定到特定路径。这意味着您无法移动环境或将其复制到另一台计算机。您可以使用以下命令修复环境以使其可重定位:

$ virtualenv-可重定位的ENV

注意: ENV是虚拟环境的名称,您必须从ENV目录外部运行它。

这将使setuptools创建的某些文件或分发文件的文件使用相对路径,并将所有脚本更改为使用activate_this.py而不是使用Python解释器的位置来选择环境。

注意:在将任何软件包安装到环境中之后,必须运行此命令。如果使环境可重定位,然后安装新软件包,则必须再次运行virtualenv –relocatable。

You need to adjust your install to use relative paths. virtualenv provides for this with the --relocatable option. From the docs:

Normally environments are tied to a specific path. That means that you cannot move an environment around or copy it to another computer. You can fix up an environment to make it relocatable with the command:

$ virtualenv –relocatable ENV

NOTE: ENV is the name of the virtual environment and you must run this from outside the ENV directory.

This will make some of the files created by setuptools or distribute use relative paths, and will change all the scripts to use activate_this.py instead of using the location of the Python interpreter to select the environment.

Note: you must run this after you’ve installed any packages into the environment. If you make an environment relocatable, then install a new package, you must run virtualenv –relocatable again.


回答 1

我相信“知道为什么”比“知道如何”更重要。因此,这是解决此问题的另一种方法。

运行时. env/bin/activate,它实际上执行以下命令(/tmp例如,使用):

VIRTUAL_ENV="/tmp/myproject/env"
export VIRTUAL_ENV

但是,您刚刚将重命名myprojectproject,因此该命令无法执行。这就是为什么这样说的原因pip is not installed,因为您尚未安装pip在系统全局环境中,并且您的virtualenv pip来源不正确。

如果要手动修复此问题,请采用以下方法:

  1. 使用您喜欢的Vim编辑器,/tmp/project/env/bin/activate通常在第42行进行修改:

    VIRTUAL_ENV='/tmp/myproject/env' => VIRTUAL_ENV='/tmp/project/env'

  2. /tmp/project/env/bin/pip在第1行中进行修改:

    #!/tmp/myproject/env/bin/python => #!/tmp/project/env/bin/python

之后,env再次激活您的虚拟环境,您将看到自己pip又回来了。

I believe “knowing why” matters more than “knowing how”. So, here is another approach to fix this.

When you run . env/bin/activate, it actually executes the following commands (using /tmp for example):

VIRTUAL_ENV="/tmp/myproject/env"
export VIRTUAL_ENV

However, you have just renamed myproject to project, so that command failed to execute. That is why it says pip is not installed, because you haven’t installed pip in the system global environment and your virtualenv pip is not sourced correctly.

If you want to fix this manually, this is the way:

  1. With your favorite editor like Vim, modify /tmp/project/env/bin/activate usually in line 42:

    VIRTUAL_ENV='/tmp/myproject/env' => VIRTUAL_ENV='/tmp/project/env'

  2. Modify /tmp/project/env/bin/pip in line 1:

    #!/tmp/myproject/env/bin/python => #!/tmp/project/env/bin/python

After that, activate your virtual environment env again, and you will see your pip has come back again.


回答 2

注意:作为@jb。指出,此解决方案仅适用于容易(重新)创建virtualenv的。如果环境需要花费几个小时来安装此解决方案,则不建议


Virtualenvs很棒,因为它们易于制作和切换。它们可以防止您陷入单一配置中。如果您知道项目要求或可以得到它们,请新建一个virtualenv

  • 建立requirements.txt档案

    (env)$ pip freeze > requirements.txt

    • 如果您无法创建requirements.txt文件,请env/lib/pythonX.X/site-packages在删除原始文件之前进行检查env
  • 删除现有的 (env)

    deactivate && rm -rf env

  • 创建一个新的virtualenv,激活它并安装需求

    virtualenv env && . env/bin/activate && pip install -r requirements.txt


或者,使用virtualenvwrapper使事情变得简单一些,因为所有virtualenv都保存在集中位置

$(old-venv) pip freeze > temp-reqs.txt
$(old-venv) deactivate
$ mkvirtualenv new-venv
$(new-venv) pip install -r temp-reqs.txt
$(new-venv) rmvirtualenv old-venv

NOTE: As @jb. points out, this solution only applies to easily (re)created virtualenvs. If an environment takes several hours to install this solution is not recommended


Virtualenvs are great because they are easy to make and switch around; they keep you from getting locked into a single configuration. If you know the project requirements, or can get them, Make a new virtualenv:

  • Create a requirements.txt file

    (env)$ pip freeze > requirements.txt

    • If you can’t create the requirements.txt file, check env/lib/pythonX.X/site-packages before removing the original env.
  • Delete the existing (env)

    deactivate && rm -rf env

  • Create a new virtualenv, activate it, and install requirements

    virtualenv env && . env/bin/activate && pip install -r requirements.txt


Alternatively, use virtualenvwrapper to make things a little easier as all virtualenvs are kept in a centralized location

$(old-venv) pip freeze > temp-reqs.txt
$(old-venv) deactivate
$ mkvirtualenv new-venv
$(new-venv) pip install -r temp-reqs.txt
$(new-venv) rmvirtualenv old-venv

回答 3

我总是安装virtualenvwrapper来提供帮助。在shell提示下:

pip install virtualenvwrapper

在virtualenvwrapper文档中有记录的方法-cpvirtualenv, 这就是您要做的。确保您不在环境中,然后返回到Shell提示符。输入所需名称:

cpvirtualenv oldenv newenv

然后,如有必要:

rmvirtualenv oldenv

要转到您的newenv,请执行以下操作:

workon newenv

I always install virtualenvwrapper to help out. From the shell prompt:

pip install virtualenvwrapper

There is a way documented in the virtualenvwrapper documents – cpvirtualenv This is what you do. Make sure you are out of your environment and back to the shell prompt. Type in this with the names required:

cpvirtualenv oldenv newenv

And then, if necessary:

rmvirtualenv oldenv

To go to your newenv:

workon newenv

回答 4

您可以按照以下步骤解决问题:

  1. 重命名目录
  2. 重新运行这个: $ virtualenv ..\path\renamed_directory
  3. virtualenv将纠正目录关联,同时将软件包保留在原位
  4. $ scripts/activate
  5. $ pip freeze 确认您的包裹到位
  6. 一个重要的警告,如果您在virtualenv目录中的脚本文件中有任何静态路径依赖项,则必须手动更改这些依赖项。

You can fix your issue by following these steps:

  1. rename your directory
  2. rerun this: $ virtualenv ..\path\renamed_directory
  3. virtualenv will correct the directory associations while leaving your packages in place
  4. $ scripts/activate
  5. $ pip freeze to verify your packages are in place
  6. An important caveat, if you have any static path dependencies in script files in your virtualenv directory, you will have to manually change those.

回答 5

对我来说很多次都没有问题的实现方法是virtualenv-clone

pip install virtualenv-clone
virtualenv-clone old-dir/env new-dir/env

Yet another way to do it that worked for me many times without problems is virtualenv-clone:

pip install virtualenv-clone
virtualenv-clone old-dir/env new-dir/env

回答 6

(在项目文件夹中)

cd bin
sed -i 's/old_dir_name/new_dir_name/g' *

不要忘记停用和激活

Run this inside your project folder:

cd bin
sed -i 's/old_dir_name/new_dir_name/g' *

Don’t forget to deactivate and activate.


回答 7

virtualenv --relocatable ENV这不是理想的解决方案。我认为大多数人都希望能够重命名virtualenv 而不会产生任何长期的副作用。

因此,我创建了一个简单的工具来执行此操作。virtualenv-mv的项目页面对它进行了更详细的概述,但是从本质上讲,您可以virtualenv-mv像使用简单的实现mv(没有任何选项)那样使用。

例如:

virtualenv-mv myproject project

但是请注意,我只是对此进行了修改。在异常情况下(例如,符号链接的virtualenvs)它可能会损坏,因此请小心(备份无法承受的损失),如果遇到任何问题,请告诉我。

virtualenv --relocatable ENV is not a desirable solution. I assume most people want the ability to rename a virtualenv without any long-term side effects.

So I’ve created a simple tool to do just that. The project page for virtualenv-mv outlines it in a bit more detail, but essentially you can use virtualenv-mv just like you’d use a simple implementation of mv (without any options).

For example:

virtualenv-mv myproject project

Please note however that I just hacked this up. It could break under unusual circumstances (e.g. symlinked virtualenvs) so please be careful (back up what you can’t afford to lose) and let me know if you encounter any problems.


在virtualenv中设置环境变量

问题:在virtualenv中设置环境变量

我有一个Heroku项目,该项目使用环境变量来获取其配置,但是我首先使用virtualenv在本地测试我的应用程序。

有没有办法在virtualenv内部设置在远程计算机上定义的环境变量?

I have a Heroku project that uses environment variables to get its configuration, but I use virtualenv to test my app locally first.

Is there a way to set the environment variables defined on the remote machine inside virtualenv?


回答 0

更新资料

截至2017年5月17日,autoenv的自述文件指出direnv可能是更好的选择,并暗示autoenv将不再维护。

旧答案

我写了autoenv来做到这一点:

https://github.com/kennethreitz/autoenv

Update

As of 17th May 2017 the README of autoenv states that direnv is probably the better option and implies autoenv is no longer maintained.

Old answer

I wrote autoenv to do exactly this:

https://github.com/kennethreitz/autoenv


回答 1

如果您使用virtualenvwrapper(我强烈建议您这样做),则可以使用中具有相同名称的脚本定义不同的钩子(预激活,后激活,预停用,后停用)$VIRTUAL_ENV/bin/。您需要后激活挂钩。

$ workon myvenv

$ cat $VIRTUAL_ENV/bin/postactivate
#!/bin/bash
# This hook is run after this virtualenv is activated.
export DJANGO_DEBUG=True
export S3_KEY=mykey
export S3_SECRET=mysecret

$ echo $DJANGO_DEBUG
True

如果要将此配置保留在项目目录中,只需创建一个从项目目录到的符号链接$VIRTUAL_ENV/bin/postactivate

$ rm $VIRTUAL_ENV/bin/postactivate
$ ln -s .env/postactivate $VIRTUAL_ENV/bin/postactivate

您甚至可以在每次使用mkvirtualenv自动创建符号链接

清除后停用

请记住,这本身不会清除。停用virtualenv时,环境变量将保留。要对称清理,您可以添加到$VIRTUAL_ENV/bin/predeactivate

$ cat $VIRTUAL_ENV/bin/predeactivate
#!/bin/bash
# This hook is run before this virtualenv is deactivated.
unset DJANGO_DEBUG

$ deactivate

$ echo $DJANGO_DEBUG

请记住,如果将其用于可能已在您的环境中设置的环境变量,则取消设置将导致它们在离开virtualenv时被完全取消设置。因此,如果完全有可能,您可以将先前的值记录在临时位置,然后在停用时重新读回。

建立:

$ cat $VIRTUAL_ENV/bin/postactivate
#!/bin/bash
# This hook is run after this virtualenv is activated.
if [[ -n $SOME_VAR ]]
then
    export SOME_VAR_BACKUP=$SOME_VAR
fi
export SOME_VAR=apple

$ cat $VIRTUAL_ENV/bin/predeactivate
#!/bin/bash
# This hook is run before this virtualenv is deactivated.
if [[ -n $SOME_VAR_BACKUP ]]
then
    export SOME_VAR=$SOME_VAR_BACKUP
    unset SOME_VAR_BACKUP
else
    unset SOME_VAR
fi

测试:

$ echo $SOME_VAR
banana

$ workon myenv

$ echo $SOME_VAR
apple

$ deactivate

$ echo $SOME_VAR
banana

In case you’re using virtualenvwrapper (I highly recommend doing so), you can define different hooks (preactivate, postactivate, predeactivate, postdeactivate) using the scripts with the same names in $VIRTUAL_ENV/bin/. You need the postactivate hook.

$ workon myvenv

$ cat $VIRTUAL_ENV/bin/postactivate
#!/bin/bash
# This hook is run after this virtualenv is activated.
export DJANGO_DEBUG=True
export S3_KEY=mykey
export S3_SECRET=mysecret

$ echo $DJANGO_DEBUG
True

If you want to keep this configuration in your project directory, simply create a symlink from your project directory to $VIRTUAL_ENV/bin/postactivate.

$ rm $VIRTUAL_ENV/bin/postactivate
$ ln -s .env/postactivate $VIRTUAL_ENV/bin/postactivate

You could even automate the creation of the symlinks each time you use mkvirtualenv.

Cleaning up on deactivate

Remember that this wont clean up after itself. When you deactivate the virtualenv, the environment variable will persist. To clean up symmetrically you can add to $VIRTUAL_ENV/bin/predeactivate.

$ cat $VIRTUAL_ENV/bin/predeactivate
#!/bin/bash
# This hook is run before this virtualenv is deactivated.
unset DJANGO_DEBUG

$ deactivate

$ echo $DJANGO_DEBUG

Remember that if using this for environment variables that might already be set in your environment then the unset will result in them being completely unset on leaving the virtualenv. So if that is at all probable you could record the previous value somewhere temporary then read it back in on deactivate.

Setup:

$ cat $VIRTUAL_ENV/bin/postactivate
#!/bin/bash
# This hook is run after this virtualenv is activated.
if [[ -n $SOME_VAR ]]
then
    export SOME_VAR_BACKUP=$SOME_VAR
fi
export SOME_VAR=apple

$ cat $VIRTUAL_ENV/bin/predeactivate
#!/bin/bash
# This hook is run before this virtualenv is deactivated.
if [[ -n $SOME_VAR_BACKUP ]]
then
    export SOME_VAR=$SOME_VAR_BACKUP
    unset SOME_VAR_BACKUP
else
    unset SOME_VAR
fi

Test:

$ echo $SOME_VAR
banana

$ workon myenv

$ echo $SOME_VAR
apple

$ deactivate

$ echo $SOME_VAR
banana

回答 2

您可以尝试:

export ENVVAR=value

在virtualenv_root / bin / activate中。基本上,激活脚本是您开始使用virtualenv时执行的脚本,因此您可以在其中放置所有自定义内容。

You could try:

export ENVVAR=value

in virtualenv_root/bin/activate. Basically the activate script is what is executed when you start using the virtualenv so you can put all your customization in there.


回答 3

仅使用virtualenv(不使用virtualenvwrapper),通过activate您采购的用于激活virtualenv 的脚本即可轻松设置环境变量。

跑:

nano YOUR_ENV/bin/activate

将环境变量添加到文件末尾,如下所示:

export KEY=VALUE

如果需要,您还可以设置类似的钩子来取消设置环境变量,如Danilo Bargen在上面的出色答案中所建议的那样。

Using only virtualenv (without virtualenvwrapper), setting environment variables is easy through the activate script you sourcing in order to activate the virtualenv.

Run:

nano YOUR_ENV/bin/activate

Add the environment variables to the end of the file like this:

export KEY=VALUE

You can also set a similar hook to unset the environment variable as suggested by Danilo Bargen in his great answer above if you need.


回答 4

尽管这里有很多不错的答案,但我没有看到一个发布的解决方案,该解决方案既包括在停用时取消设置环境变量,又不需要除之外的其他库virtualenv,因此这是我的解决方案,仅涉及使用/ bin / activate进行编辑。变量MY_SERVER_NAMEMY_DATABASE_URL示例:

在激活脚本中应该有一个用于停用的定义,并且您想在其末尾取消设置变量:

deactivate () {
    ...

    # Unset My Server's variables
    unset MY_SERVER_NAME
    unset MY_DATABASE_URL
}

然后在激活脚本的末尾,设置变量:

# Set My Server's variables
export MY_SERVER_NAME="<domain for My Server>"
export MY_DATABASE_URL="<url for database>"

这样,您无需安装其他任何东西即可使它正常工作,并且最终不会在您deactivate使用virtualenv 时留下变量。

While there are a lot of nice answers here, I didn’t see a solution posted that both includes unsetting environment variables on deactivate and doesn’t require additional libraries beyond virtualenv, so here’s my solution that just involves editing /bin/activate, using the variables MY_SERVER_NAME and MY_DATABASE_URL as examples:

There should be a definition for deactivate in the activate script, and you want to unset your variables at the end of it:

deactivate () {
    ...

    # Unset My Server's variables
    unset MY_SERVER_NAME
    unset MY_DATABASE_URL
}

Then at the end of the activate script, set the variables:

# Set My Server's variables
export MY_SERVER_NAME="<domain for My Server>"
export MY_DATABASE_URL="<url for database>"

This way you don’t have to install anything else to get it working, and you don’t end up with the variables being left over when you deactivate the virtualenv.


回答 5

在virtualenv内部,可以使用两种方法进行测试。第一个是通过Heroku工具栏(https://toolbelt.heroku.com/)安装的工具。该工具是领班。它将导出本地存储在.env文件中的所有环境变量,然后在Procfile中运行应用程序进程。

如果您正在寻找一种更简单的方法,第二种方法是在本地拥有一个.env文件,然后运行:

export $(cat .env)

Locally within an virtualenv there are two methods you could use to test this. The first is a tool which is installed via the Heroku toolbelt (https://toolbelt.heroku.com/). The tool is foreman. It will export all of your environment variables that are stored in a .env file locally and then run app processes within your Procfile.

The second way if you’re looking for a lighter approach is to have a .env file locally then run:

export $(cat .env)

回答 6

安装autoenv或者通过

$ pip install autoenv

(要么)

$ brew install autoenv

然后.env在您的virtualenv项目文件夹中创建文件

$ echo "source bin/activate" > .env

现在一切正常。

Install autoenv either by

$ pip install autoenv

(or)

$ brew install autoenv

And then create .env file in your virtualenv project folder

$ echo "source bin/activate" > .env

Now everything works fine.


回答 7

如果您已经在使用Heroku,请考虑通过Foreman运行服务器。它支持一个.env仅是行列表的文件,该文件KEY=VAL将在运行前导出到您的应用。

If you’re already using Heroku, consider running your server via Foreman. It supports a .env file which is simply a list of lines with KEY=VAL that will be exported to your app before it runs.


回答 8

另一种为django设计的方法,但应该在大多数设置中都可以使用,那就是使用django-dotenv。

Another way to do it that’s designed for django, but should work in most settings, is to use django-dotenv.


回答 9

要在env目录中激活virtualenv 并导出.env使用中存储的环境变量:

source env/bin/activate && set -a; source .env; set +a

To activate virtualenv in env directory and export envinroment variables stored in .env use :

source env/bin/activate && set -a; source .env; set +a

是否可以在Virtualenv中安装另一个版本的Python?

问题:是否可以在Virtualenv中安装另一个版本的Python?

我在安装了Python 2.4的虚拟主机中有一个共享帐户,但是我的代码与2.4不兼容。是否可以将Python 2.6直接安装到Virtualenv?

注意:我无权在共享服务器上安装它。

I have a shared account in a web-hosting that has Python 2.4 installed, but my code is not compatible with 2.4. Is it possible to install Python 2.6 directly to Virtualenv?

Note: I don´t have permission to install it in the shared server.


回答 0

这是virtualenv的选项

$ virtualenv
You must provide a DEST_DIR
Usage: virtualenv [OPTIONS] DEST_DIR

Options:
  --version             show program's version number and exit.
  -h, --help            show this help message and exit.
  -v, --verbose         Increase verbosity.
  -q, --quiet           Decrease verbosity.
  -p PYTHON_EXE, --python=PYTHON_EXE
                        The Python interpreter to use, e.g.,
                        --python=python2.5 will use the python2.5 interpreter
                        to create the new environment.  The default is the
                        interpreter that virtualenv was installed with
                        (/usr/bin/python)
  --clear               Clear out the non-root install and start from scratch
  --no-site-packages    Don't give access to the global site-packages dir to
                        the virtual environment
  --unzip-setuptools    Unzip Setuptools or Distribute when installing it
  --relocatable         Make an EXISTING virtualenv environment relocatable.
                        This fixes up scripts and makes all .pth files
                        relative
  --distribute          Use Distribute instead of Setuptools. Set environ
                        variable VIRTUALENV_USE_DISTRIBUTE to make it the
                        default
  --prompt==PROMPT      Provides an alternative prompt prefix for this
                        environment

1)您要做的是将python安装到您也可以编写的目录中。

您可以按照说明进行操作here

对于Python 2.7.1
Python source

mkdir ~/src
mkdir ~/.localpython
cd ~/src
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
tar -zxvf Python-2.7.1.tgz
cd Python-2.7.1

make clean
./configure --prefix=/home/${USER}/.localpython
make
make install

2)安装virtualenv
virtualenv source

cd ~/src
wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.5.2.tar.gz#md5=fbcefbd8520bb64bc24a560c6019a73c
tar -zxvf virtualenv-1.5.2.tar.gz
cd virtualenv-1.5.2/
~/.localpython/bin/python setup.py install

3)使用本地python创建一个virtualenv
virtualenv docs

mkdir /home/${USER}/virtualenvs
cd /home/${USER}/virtualenvs
~/.localpython/bin/virtualenv py2.7 --python=/home/${USER}/.localpython/bin/python2.7

4)激活环境

cd ~/virtualenvs/py2.7/bin
source ./activate

5)检查

(py2.7)$ python
Python 2.7.1 (r271:86832, Mar 31 2011, 15:31:37) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

(py2.7)$ deactivate
$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Here are the options for virtualenv

$ virtualenv
You must provide a DEST_DIR
Usage: virtualenv [OPTIONS] DEST_DIR

Options:
  --version             show program's version number and exit.
  -h, --help            show this help message and exit.
  -v, --verbose         Increase verbosity.
  -q, --quiet           Decrease verbosity.
  -p PYTHON_EXE, --python=PYTHON_EXE
                        The Python interpreter to use, e.g.,
                        --python=python2.5 will use the python2.5 interpreter
                        to create the new environment.  The default is the
                        interpreter that virtualenv was installed with
                        (/usr/bin/python)
  --clear               Clear out the non-root install and start from scratch
  --no-site-packages    Don't give access to the global site-packages dir to
                        the virtual environment
  --unzip-setuptools    Unzip Setuptools or Distribute when installing it
  --relocatable         Make an EXISTING virtualenv environment relocatable.
                        This fixes up scripts and makes all .pth files
                        relative
  --distribute          Use Distribute instead of Setuptools. Set environ
                        variable VIRTUALENV_USE_DISTRIBUTE to make it the
                        default
  --prompt==PROMPT      Provides an alternative prompt prefix for this
                        environment

1) What you want to do is install python to a directory that you are able to write too.

You can follow the instructions here.

For Python 2.7.1
Python source

mkdir ~/src
mkdir ~/.localpython
cd ~/src
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
tar -zxvf Python-2.7.1.tgz
cd Python-2.7.1

make clean
./configure --prefix=/home/${USER}/.localpython
make
make install

2) Install virtualenv
virtualenv source

cd ~/src
wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.5.2.tar.gz#md5=fbcefbd8520bb64bc24a560c6019a73c
tar -zxvf virtualenv-1.5.2.tar.gz
cd virtualenv-1.5.2/
~/.localpython/bin/python setup.py install

3) Create a virtualenv using your local python
virtualenv docs

mkdir /home/${USER}/virtualenvs
cd /home/${USER}/virtualenvs
~/.localpython/bin/virtualenv py2.7 --python=/home/${USER}/.localpython/bin/python2.7

4) Activate the environment

cd ~/virtualenvs/py2.7/bin
source ./activate

5) Check

(py2.7)$ python
Python 2.7.1 (r271:86832, Mar 31 2011, 15:31:37) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

(py2.7)$ deactivate
$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

回答 1

先决条件:

  1. sudo easy_install virtualenv
  2. sudo pip install virtualenvwrapper

使用Python2.6安装virtualenv:

  1. 您可以手动下载,构建另一个Python版本并将其安装到/usr/local另一个位置。

  2. 如果除以外的其他位置/usr/local,请将其添加到PATH中。

  3. 重新加载您的外壳以选择更新的PATH。

  4. 从这一点开始,您应该能够从shell中调用以下2个python二进制文件python2.5python2.6

  5. 使用以下命令创建一个virtualenv的新实例python2.6

    mkvirtualenv --python=python2.6 yournewenv

Pre-requisites:

  1. sudo easy_install virtualenv
  2. sudo pip install virtualenvwrapper

Installing virtualenv with Python2.6:

  1. You could manually download, build and install another version of Python to /usr/local or another location.

  2. If it’s another location other than /usr/local, add it to your PATH.

  3. Reload your shell to pick up the updated PATH.

  4. From this point on, you should be able to call the following 2 python binaries from your shell python2.5 and python2.6

  5. Create a new instance of virtualenv with python2.6:

    mkvirtualenv --python=python2.6 yournewenv


回答 2

此过程安装Python2.7 任何地方,消除了任何绝对路径引用您的内部env文件夹(由virtualenv中管理)。甚至没有完全安装virtualenv。

因此,从理论上讲,您可以将顶级目录放到tarball中,在没有安装Python (或任何依赖项)的机器上分发并运行tarball中配置的任何内容

如有任何问题请联系我。这只是我正在进行的一个较大的项目的一部分。现在,为下降…

  1. 设置环境文件夹。

    $ mkdir env
    $ mkdir pyenv
    $ mkdir dep
  2. 无需任何形式的root操作系统安装即可获取Python-2.7.3和virtualenv 。

    $ cd dep
    $ wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
    $ wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
  3. 解压缩并将Python-2.7.3安装到该pyenv目录中。make clean如果您是第2、3,N次执行此操作,则是可选的…

    $ tar -xzvf Python-2.7.3.tgz
    $ cd Python-2.7.3
    $ make clean
    $ ./configure --prefix=/path/to/pyenv
    $ make && make install
    $ cd ../../
    $ ls
    dep    env    pyenv
  4. 创建您的virtualenv

    $ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
  5. 修复符号链接到python2.7 env/include/

    $ ls -l env/include/
    $ cd !$
    $ rm python2.7
    $ ln -s ../../pyenv/include/python2.7 python2.7
    $ cd ../../
  6. 修复env中剩余的python符号链接。如上所述,您必须删除符号链接的目录并重新创建它们。另外,这是强制就地符号链接创建的语法。

    $ ls -l env/lib/python2.7/
    $ cd !$
    $ ln -sf ../../../pyenv/lib/python2.7/UserDict.py UserDict.py
    [...repeat until all symbolic links are relative...]
    $ cd ../../../
  7. 测试

    $ python --version
    Python 2.7.1
    $ source env/bin/activate
    (env)
    $ python --version
    Python 2.7.3

阿罗哈

This procedure installs Python2.7 anywhere and eliminates any absolute path references within your env folder (managed by virtualenv). Even virtualenv isn’t installed absolutely.

Thus, theoretically, you can drop the top level directory into a tarball, distribute, and run anything configured within the tarball on a machine that doesn’t have Python (or any dependencies) installed.

Contact me with any questions. This is just part of an ongoing, larger project I am engineering. Now, for the drop…

  1. Set up environment folders.

    $ mkdir env
    $ mkdir pyenv
    $ mkdir dep
    
  2. Get Python-2.7.3, and virtualenv without any form of root OS installation.

    $ cd dep
    $ wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
    $ wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
    
  3. Extract and install Python-2.7.3 into the pyenv dir. make clean is optional if you are doing this a 2nd, 3rd, Nth time…

    $ tar -xzvf Python-2.7.3.tgz
    $ cd Python-2.7.3
    $ make clean
    $ ./configure --prefix=/path/to/pyenv
    $ make && make install
    $ cd ../../
    $ ls
    dep    env    pyenv
    
  4. Create your virtualenv

    $ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
    
  5. Fix the symlink to python2.7 within env/include/

    $ ls -l env/include/
    $ cd !$
    $ rm python2.7
    $ ln -s ../../pyenv/include/python2.7 python2.7
    $ cd ../../
    
  6. Fix the remaining python symlinks in env. You’ll have to delete the symbolically linked directories and recreate them, as above. Also, here’s the syntax to force in-place symbolic link creation.

    $ ls -l env/lib/python2.7/
    $ cd !$
    $ ln -sf ../../../pyenv/lib/python2.7/UserDict.py UserDict.py
    [...repeat until all symbolic links are relative...]
    $ cd ../../../
    
  7. Test

    $ python --version
    Python 2.7.1
    $ source env/bin/activate
    (env)
    $ python --version
    Python 2.7.3
    

Aloha.


回答 3

我正在使用virtualenvwrapper,并且不想修改$ PATH,方法如下:

$ which python3
/usr/local/bin/python3

$ mkvirtualenv --python=/usr/local/bin/python3 env_name

I’m using virtualenvwrapper and don’t want to modify $PATH, here’s how:

$ which python3
/usr/local/bin/python3

$ mkvirtualenv --python=/usr/local/bin/python3 env_name

回答 4

通常的方法是下载源代码并在本地构建和安装(但不能直接在virtualenv中安装),然后使用该本地Python安装程序创建新的virtualenv。在某些系统上,有可能下载并安装预构建的python,而不是从源代码构建。

The usual approach is to download the source and build and install locally (but not directly in virtualenv), and then create a new virtualenv using that local Python install. On some systems, it may be possible to download and install a prebuilt python, rather than building from source.


回答 5

如今,我发现拥有更新版本的Python的最简单方法是通过conda将其安装到conda环境中。


安装conda(为此可能需要virtualenv)

pip install conda

在conda环境中安装新的Python版本

我在这里添加此答案,因为不需要手动下载。conda将为您做到这一点。

现在为所需的Python版本创建一个环境。在此示例中,我将使用3.5.2,因为它是撰写本文时(2016年8月)的最新版本。

conda create -n py35 python=3.5.2

将为conda创建安装包的环境


要激活此环境(我假设linux否则请检查conda docs):

source activate py35

现在,通过environemnt中的pip或conda安装所需的内容(conda具有更好的二进制软件包支持)。

conda install <package_name>

Now a days, the easiest way I found to have a more updated version of Python is to install it via conda into a conda environment.


Install conda(you may need a virtualenv for this)

pip install conda

Installing a new Python version inside a conda environent

I’m adding this answer here because no manual download is needed. conda will do that for you.

Now create an environment for the Python version you want. In this example I will use 3.5.2, because it it the latest version at this time of writing (Aug 2016).

conda create -n py35 python=3.5.2

Will create a environment for conda to install packages


To activate this environment(I’m assuming linux otherwise check the conda docs):

source activate py35

Now install what you need either via pip or conda in the environemnt(conda has better binary package support).

conda install <package_name>

回答 6

您可以使用pyenv。

有很多不同版本的anaconda,jython,pypy等…

https://github.com/yyuu/pyenv

安装简单 pyenv install 3.2.6

pyenv install --list
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  2.4.5
  2.4.6
  2.5
  2.5.1
  2.5.2
  2.5.3
  2.5.4
  2.5.5
  2.5.6
  2.6.6

You may use pyenv.

There are a lot of different versions anaconda, jython, pypy and so on…

https://github.com/yyuu/pyenv

Installation as simple as pyenv install 3.2.6

pyenv install --list
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  2.4.5
  2.4.6
  2.5
  2.5.1
  2.5.2
  2.5.3
  2.5.4
  2.5.5
  2.5.6
  2.6.6


回答 7

尽管该问题专门描述了安装2.6,但我想在上面的出色回答中添加一些重要点,以防有人遇到此问题。作为记录,我的案例是我试图在ubuntu 10.04机器上安装2.7。

首先,我对这里所有答案中所述方法的追求是,从Deadsnake的ppa安装Python 完全失败。因此,构建本地Python是必经之路。

尝试过之后,我认为依靠默认的pip安装(带有sudo apt-get install pip)就足够了。不幸的是,这是错误的。原来,我的所有烦恼都是空缺,最终无法创建virtualenv。

因此,我强烈建议本地安装PIPwget https://raw.github.com/pypa/pip/master/contrib/get-pip.py && python get-pip.py --user这个相关的问题给了我这个提示。

现在,如果这不起作用,请确保已安装libssl-devUbuntu或openssl-devCentOS。使用apt-get或进行安装yum,然后重新构建Python(如果已安装,则无需删除任何内容,请先删除)。get-pip抱怨,您可以通过import ssl在py shell上运行进行检查。

最后,不要忘了声明.local/bin和本地python到路径,并使用which pip和进行检查which python

Although the question specifically describes installing 2.6, I would like to add some importants points to the excellent answers above in case someone comes across this. For the record, my case was that I was trying to install 2.7 on an ubuntu 10.04 box.

First, my motivation towards the methods described in all the answers here is that installing Python from deadsnake’s ppa’s has been a total failure. So building a local Python is the way to go.

Having tried so, I thought relying to the default installation of pip (with sudo apt-get install pip) would be adequate. This unfortunately is wrong. It turned out that I was getting all shorts of nasty issues and eventually not being able to create a virtualenv.

Therefore, I highly recommend to install pip locally with wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py && python get-pip.py --user. This related question gave me this hint.

Now if this doesn’t work, make sure that libssl-dev for Ubuntu or openssl-dev for CentOS is installed. Install them with apt-get or yum and then re-build Python (no need to remove anything if already installed, do so on top). get-pip complains about that, you can check so by running import ssl on a py shell.

Last, don’t forget to declare .local/bin and local python to path, check with which pip and which python.


回答 8

pyenv的完整指南

如果未安装pyenv,请使用pyenv-installer进行安装

$ curl https://pyenv.run | bash

要使用任何自定义的python版本,例如,3.5.6使用以下命令:

pyenv install 3.5.6
pyenv virtualenv 3.5.6 name_of_your_env
pyenv activate name_of_your_env

Full guide with pyenv

If pyenv is not installed then install it with pyenv-installer:

$ curl https://pyenv.run | bash

To use any custom python version, e.g. 3.5.6 use the following:

pyenv install 3.5.6
pyenv virtualenv 3.5.6 name_of_your_env
pyenv activate name_of_your_env

回答 9

否,但是您可以在目录下安装隔离的Python构建(例如ActivePython$HOME

这种方法是最快的,并且不需要您自己编译Python。

(此外,您还可以使用ActiveState的二进制包管理器

No, but you can install an isolated Python build (such as ActivePython) under your $HOME directory.

This approach is the fastest, and doesn’t require you to compile Python yourself.

(as a bonus, you also get to use ActiveState’s binary package manager)


回答 10

我没有找到合适的答案,所以我来看看,它基于@toszter答案,但不使用系统Python(而且您可能知道,在处理许多问题时,在系统级别安装setuptools和virtualenv并不总是一个好主意Python配置):

#!/bin/sh

mkdir python_ve
cd python_ve

MYROOT=`pwd`
mkdir env pyenv dep

cd ${MYROOT}/dep
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-15.2.tar.gz#md5=a9028a9794fc7ae02320d32e2d7e12ee
wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
xz -d Python-2.7.9.tar.xz

cd ${MYROOT}/pyenv
tar xf ../dep/Python-2.7.9.tar
cd Python-2.7.9
./configure --prefix=${MYROOT}/pyenv && make -j 4 && make install

cd ${MYROOT}/pyenv

tar xzf ../dep/setuptools-15.2.tar.gz

cd ${MYROOT}

pyenv/bin/python dep/virtualenv.py --no-setuptools --python=${MYROOT}/pyenv/bin/python --verbose env
env/bin/python pyenv/setuptools-15.2/setup.py install
env/bin/easy_install pip

echo "virtualenv in ${MYROOT}/env"

打破鸡蛋问题的技巧是首先使virtualenv不使用setuptools,因为否则它会失败(找不到pip)。可能可以直接安装pip / wheel,但是以某种方式easy_install是我想到的第一件事。同样,可以通过考虑具体版本来改进脚本。

注意 在脚本中使用xz。

I have not found suitable answer, so here goes my take, which builds upon @toszter answer, but does not use system Python (and you may know, it is not always good idea to install setuptools and virtualenv at system level when dealing with many Python configurations):

#!/bin/sh

mkdir python_ve
cd python_ve

MYROOT=`pwd`
mkdir env pyenv dep

cd ${MYROOT}/dep
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-15.2.tar.gz#md5=a9028a9794fc7ae02320d32e2d7e12ee
wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
xz -d Python-2.7.9.tar.xz

cd ${MYROOT}/pyenv
tar xf ../dep/Python-2.7.9.tar
cd Python-2.7.9
./configure --prefix=${MYROOT}/pyenv && make -j 4 && make install

cd ${MYROOT}/pyenv

tar xzf ../dep/setuptools-15.2.tar.gz

cd ${MYROOT}

pyenv/bin/python dep/virtualenv.py --no-setuptools --python=${MYROOT}/pyenv/bin/python --verbose env
env/bin/python pyenv/setuptools-15.2/setup.py install
env/bin/easy_install pip

echo "virtualenv in ${MYROOT}/env"

The trick of breaking chicken-egg problem here is to make virtualenv without setuptools first, because it otherwise fails (pip can not be found). It may be possible to install pip / wheel directly, but somehow easy_install was the first thing which came to my mind. Also, the script can be improved by factoring out concrete versions.

NB. Using xz in the script.


回答 11

首先,谢谢DTing的出色回答。非常完美。

对于那些在共享主机中没有GCC访问权的人,请使用ActivePython而不是像Scott Stafford所述的普通python。这是命令。

wget http://downloads.activestate.com/ActivePython/releases/2.7.13.2713/ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz

tar -zxvf ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz

cd ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785

./install.sh

它将询问您到python目录的路径。输入

../../.localpython

只需将上面的内容替换为DTing回答中的步骤1,然后继续执行步骤2。请注意,ActivePython包URL可能会随新版本而更改。您随时可以从此处获取新的URL:http : //www.activestate.com/activepython/downloads

根据URL,您需要根据收到的文件更改tar和cd命令的名称。

First of all, Thank you DTing for awesome answer. It’s pretty much perfect.

For those who are suffering from not having GCC access in shared hosting, Go for ActivePython instead of normal python like Scott Stafford mentioned. Here are the commands for that.

wget http://downloads.activestate.com/ActivePython/releases/2.7.13.2713/ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz

tar -zxvf ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785.tar.gz

cd ActivePython-2.7.13.2713-linux-x86_64-glibc-2.3.6-401785

./install.sh

It will ask you path to python directory. Enter

../../.localpython

Just replace above as Step 1 in DTing’s answer and go ahead with Step 2 after that. Please note that ActivePython package URL may change with new release. You can always get new URL from here : http://www.activestate.com/activepython/downloads

Based on URL you need to change the name of tar and cd command based on file received.


如何激活Anaconda环境

问题:如何激活Anaconda环境

我在Windows 8上,使用Anaconda 1.7.5 64bit。

我创建了一个新的Anaconda环境

conda create -p ./test python=2.7 pip

来自C:\Pr\TEMP\venv\

效果很好(有一个带有新python发行版的文件夹)。康达告诉我输入

activate C:\PR\TEMP\venv\test

激活环境,但是返回:

No environment named "C:\PR\temp\venv\test" exists in C:\PR\Anaconda\envs

如何激活环境?我究竟做错了什么?

I’m on Windows 8, using Anaconda 1.7.5 64bit.

I created a new Anaconda environment with

conda create -p ./test python=2.7 pip

from C:\Pr\TEMP\venv\.

This worked well (there is a folder with a new python distribution). conda tells me to type

activate C:\PR\TEMP\venv\test

to activate the environment, however this returns:

No environment named "C:\PR\temp\venv\test" exists in C:\PR\Anaconda\envs

How can I activate the environment? What am I doing wrong?


回答 0

如果发生这种情况,则需要为您的环境设置PATH(以便从环境和Windows上的Scripts \中获取正确的Python)。

假设您使用以下方法创建了一个名为py33的环境:

conda create -n py33 python=3.3 anaconda

在默认情况下,这里是在Anaconda \ envs中创建的文件夹,因此您需要将PATH设置为:

set PATH=C:\Anaconda\envs\py33\Scripts;C:\Anaconda\envs\py33;%PATH%

现在它应该可以在命令窗口中工作:

activate py33

上面的行是Windows,等同于Mac和Linux教程中通常显示的代码:

$ source activate py33

更多信息:https : //groups.google.com/a/continuum.io/forum/#!topic/ anaconda/ 8T8i11gO39U

anaconda是否为每个新环境创建一个单独的PYTHONPATH变量?

If this happens you would need to set the PATH for your environment (so that it gets the right Python from the environment and Scripts\ on Windows).

Imagine you have created an environment called py33 by using:

conda create -n py33 python=3.3 anaconda

Here the folders are created by default in Anaconda\envs, so you need to set the PATH as:

set PATH=C:\Anaconda\envs\py33\Scripts;C:\Anaconda\envs\py33;%PATH%

Now it should work in the command window:

activate py33

The line above is the Windows equivalent to the code that normally appears in the tutorials for Mac and Linux:

$ source activate py33

More info: https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/8T8i11gO39U

Does `anaconda` create a separate PYTHONPATH variable for each new environment?


回答 1

使用cmd代替Powershell! 我花了2个小时才切换到cmd,然后才起作用!

创建环境:

conda create -n your_environment_name

查看conda环境列表:

conda env list

激活您的环境:

conda activate your_environment_name

那是所有人

Use cmd instead of Powershell! I spent 2 hours before I switched to cmd and then it worked!

create Environment:

conda create -n your_environment_name

see list of conda environments:

conda env list

activate your environment:

conda activate your_environment_name

That’s all folks


回答 2

请注意,用于激活环境的命令在Conda 4.4版中已更改。激活一个环境的推荐的方法是现在conda activate myenv,而不是source activate myenv。要启用新语法,您应该修改 .bashrc文件。该行当前显示类似

export PATH="<path_to_your_conda_install>/bin:$PATH"

应该改为

. <path_to_your_conda_install>/etc/profile.d/conda.sh

这只会将conda命令添加到路径,而尚未激活base环境(以前称为root)。为此,添加另一行

conda activate base

在第一个命令之后。请参阅Anaconda自2017年12月以来的博客文章中的所有详细信息。(我认为该页面目前缺少两行之间的换行符.../conda.shconda activate base)。

(此答案对Linux有效,但对Windows和Mac也可能适用)

Note that the command for activating an environment has changed in Conda version 4.4. The recommended way of activating an environment is now conda activate myenv instead of source activate myenv. To enable the new syntax, you should modify your .bashrc file. The line that currently reads something like

export PATH="<path_to_your_conda_install>/bin:$PATH"

Should be changed to

. <path_to_your_conda_install>/etc/profile.d/conda.sh

This only adds the conda command to the path, but does not yet activate the base environment (which was previously called root). To do also that, add another line

conda activate base

after the first command. See all the details in Anaconda’s blog post from December 2017. (I think that this page is currently missing a newline between the two lines, it says .../conda.shconda activate base).

(This answer is valid for Linux, but it might be relevant for Windows and Mac as well)


回答 3

以前所有的答案似乎都是过时的。

conda activateconda4.4和4.6中引入。

conda activate:重新激活了环境激活的逻辑和机制。与康达4.4,conda activate并且conda deactivate是现在用于激活和停用环境的首选命令。您会发现它们比以前的conda版本中的source activateand source deactivate命令更加灵活。该conda activate命令还具有(1)在所有OS,shell 和平台上通用的优势,以及(2)与其他包(例如python virtualenv的Activate脚本)中的脚本没有路径冲突。

例子

conda create -n venv-name python=3.6
conda activate -n venv-name
conda deactivate

这些新的子命令在“ Aanconda提示”和“ Anaconda Powershell提示”中自动可用。要conda activate每个shell(普通cmd.exe和powershell)中使用,请在Windows的每个shell中检查暴露conda命令

参考资料

All the former answers seem to be outdated.

conda activate was introduced in conda 4.4 and 4.6.

conda activate: The logic and mechanisms underlying environment activation have been reworked. With conda 4.4, conda activate and conda deactivate are now the preferred commands for activating and deactivating environments. You’ll find they are much more snappy than the source activate and source deactivate commands from previous conda versions. The conda activate command also has advantages of (1) being universal across all OSes, shells, and platforms, and (2) not having path collisions with scripts from other packages like python virtualenv’s activate script.

Examples

conda create -n venv-name python=3.6
conda activate -n venv-name
conda deactivate

These new sub-commands are available in “Aanconda Prompt” and “Anaconda Powershell Prompt” automatically. To use conda activate in every shell (normal cmd.exe and powershell), check expose conda command in every shell on Windows.

References


回答 4

从错误消息中可以看到,您指定的路径是错误的。像这样尝试:

activate ..\..\temp\venv\test

但是,当我需要安装Anaconda时,我从这里下载了它并将其安装到默认路径(C:\Anaconda),而不是将此路径放置到环境变量中,因此现在将Anacondas解释器用作默认路径。例如,如果您使用的是PyCharm,则可以在那里直接指定解释器。

As you can see from the error message the paths, that you specified, are wrong. Try it like this:

activate ..\..\temp\venv\test

However, when I needed to install Anaconda, I downloaded it from here and installed it to the default paths (C:\Anaconda), than I put this path to the environment variables, so now Anacondas interpreter is used as default. If you are using PyCharm, for example, you can specify the interpreter there directly.


回答 5

以下是它对我的工作方式

  1. C:\ Windows \ system32>设置CONDA_ENVS_PATH = d:\您的\位置
  2. C:\ Windows \ system32> conda信息

显示新的环境路径

  1. C:\ Windows \ system32> conda创建-n YourNewEnvironment –clone = root

克隆默认的根环境

  1. C:\ Windows \ system32>激活YourNewEnvironment

停用环境“ d:\ YourDefaultAnaconda3” …停用环境“ d:\ your \ location \ YourNewEnvironment” …

  1. [YourNewEnvironment] C:\ Windows \ system32> conda信息-e

康达环境:

YourNewEnvironment
* d:\ your \ location \ YourNewEnvironment

根d:\ YourDefaultAnaconda3

Below is how it worked for me

  1. C:\Windows\system32>set CONDA_ENVS_PATH=d:\your\location
  2. C:\Windows\system32>conda info

Shows new environment path

  1. C:\Windows\system32>conda create -n YourNewEnvironment –clone=root

Clones default root environment

  1. C:\Windows\system32>activate YourNewEnvironment

Deactivating environment “d:\YourDefaultAnaconda3″… Activating environment “d:\your\location\YourNewEnvironment”…

  1. [YourNewEnvironment] C:\Windows\system32>conda info -e

conda environments: #

YourNewEnvironment
* d:\your\location\YourNewEnvironment

root d:\YourDefaultAnaconda3


回答 6

我曾尝试用Jenkins job(bash)激活env conda activate base ,但是失败了,所以在尝试了很多之后,这个对我有用(CentOS 7):

source /opt/anaconda2/bin/activate base

I’ve tried to activate env from Jenkins job (in bash) with conda activate base and it failed, so after many tries, this one worked for me (CentOS 7) :

source /opt/anaconda2/bin/activate base

回答 7

假设您的环境名称为‘demo’,并且您正在使用anaconda并想创建一个虚拟环境:

(如果您想要python3)

    conda create -n demo python=3

(如果您想要python2)

    conda create -n demo python=2

运行上述命令后,您必须通过以下命令激活环境:

    source activate demo 

let’s assume your environment name is ‘demo’ and you are using anaconda and want to create a virtual environment:

(if you want python3)

    conda create -n demo python=3

(if you want python2)

    conda create -n demo python=2

After running above command you have to activate the environment by bellow command:

    source activate demo 

回答 8

对我来说,使用Anaconda Prompt代替cmd或PowerShell是关键。

在Anaconda Prompt中,我要做的就是 activate XXX

For me, using Anaconda Prompt instead of cmd or PowerShell is the key.

In Anaconda Prompt, all I need to do is activate XXX


回答 9

我也有相同的想法,似乎已在源代码中进行了修复。

I was having the same, a fix seems to have been made in the source.


回答 10

正如@Simba正确回答的那样,自4.6起conda env发生了变化。Conda activate (env-name)彻底推翻source activate (env-name),但并非没有挑战。conda activate经常强迫你的环境建立基础,让你看到类似的东西(基础)。因此,将大量错误抛给您。这也可能是因为auto_activate_base设置为True。

您可以使用以下命令进行检查

conda config --set auto_activate_base False

source ~/.bashrc

要重新激活使用此

conda config --set auto_activate_base True

source ~/.bashrc

just as @Simba has rightly answered alot has changed in the conda env since 4.6. Conda activate (env-name) overthrew source activate (env-name) for good but not without it own challenges. conda activate oftentimes force your environment to base and make u see something like (base). Therefore throwing loads of error back at you. This can also be because auto_activate_base is set to True.

You can check this by using the following command

conda config --set auto_activate_base False

source ~/.bashrc.

And To reactivate use this

conda config --set auto_activate_base True

source ~/.bashrc


回答 11

窗口: conda激活environment_name

Mac:conda激活environment_name

Window: conda activate environment_name

Mac: conda activate environment_name


pyenv,virtualenv,anaconda有什么区别?

问题:pyenv,virtualenv,anaconda有什么区别?

我是一个尝试学习python的红宝石程序员。我很喜欢pyenv,因为它就像来自rbenv的复制和粘贴一样。Pyenv帮助在系统中拥有多个版本的python,并且无需隔离系统的敏感部分即可隔离python。

我想每个python安装都带有pip包。我仍然不明白的是,有很多好的python库建议使用此virtualenv和anaconda。我什至可以找到pyenv的virtualenv插件。

现在,我对这两个pyenv和virtualenv的目的感到困惑。更糟糕的是在pyenv内部有一个virtualenv插件。

我的问题是:

  • pyenv和virtualenv有什么区别?
  • 在pyenv和virtualenv中使用pip命令有什么区别吗?
  • pyenv virutalenv做什么?

您的示例解释将不胜感激。

I am a ruby programmer trying to learn python. I am pretty family with pyenv since it is like a copy and paste from rbenv. Pyenv helps allow to have more than one version of python in a system and also to isolate the python without touching sensitive part of system.

I suppose every python installation come with pip package. What I still don’t understand is, there are many good python libs out there that suggest to use this virtualenv and anaconda. I can even find virtualenv plugin for pyenv.

Now I am getting confused with the purpose of these two pyenv and virtualenv. worse inside pyenv there is a virtualenv plugin.

my questions are:

  • what is the difference between pyenv and virtualenv?
  • Is there any difference in using pip command inside both pyenv and virtualenv?
  • what does this pyenv virutalenv do?

your explanation with example will be highly appreciated.


回答 0

编辑:pip这里也值得一提,conda并且pip具有与该主题相关的异同

pip:Python软件包管理器。

  • 您可能将其pip视为ruby gem命令的python等效项
  • pip 默认情况下不包含在python中。
  • 您可以使用homebrew安装Python ,它将自动安装pip:brew install python
  • OSX的最终版本默认不包含pip。要将pip添加到Mac系统的python版本中,您可以sudo easy_install pip
  • 您可以使用PyPI查找和发布python软件包:Python软件包索引
  • requirements.txt文件与ruby相当 gemfile
  • 要创建需求文本文件, pip freeze > requirements.txt
  • 请注意,此时,我们已经在系统上安装了python,并创建了requirements.txt文件,该文件概述了系统上已安装的所有python软件包。

pyenv:Python版本管理器

  • 从文档 pyenv可以轻松在多个版本的Python之间切换。它简单,简单,遵循UNIX的一站式工具的传统,可以很好地完成一件事。该项目是从rbenv和ruby-build分叉的,并针对Python进行了修改。
  • 很多人 不愿使用python3
  • 如果您需要使用其他版本的python,则pyenv可以轻松进行管理。

virtualenv:Python环境管理器。

  • 从文档要解决的基本问题是依赖项和版本之一以及间接权限。假设您有一个需要使用LibFoo版本1的应用程序,但是另一个应用程序需要版本2。如果将所有内容都安装到/usr/lib/python2.7/site-packages(或平台的标准位置是什么)中,那么很容易在无意中升级不应升级的应用程序的情况下结束。
  • 要创建一个virtualenv,只需调用virtualenv ENV,其中ENV是用于放置新虚拟环境的目录。
  • 要初始化virtualenv,您需要source ENV/bin/activate。要停止使用,只需调用deactivate
  • 激活后virtualenv,您可以通过运行pip install -r项目requirements.txt文件来安装工作区的所有软件包要求。

Anaconda:程序包经理+环境经理+其他科学图书馆。

  • 来自文档Anaconda 4.2.0包括易于安装的Python(2.7.12、3.4.5和/或3.5.2),以及100多种经过预先构建和测试的科学和分析Python软件包的更新,其中包括NumPy,Pandas ,SciPy,Matplotlib和IPython,通过一个简单的工具即可提供620多个软件包conda install <packagename>
  • 作为Web开发人员,我没有使用Anaconda。包括所有程序包,容量约为3GB。
  • 有一个精简miniconda版,似乎比使用pip+ 更简单virtualenv,尽管我个人没有使用它的经验。
  • 尽管conda允许您安装软件包,但是这些软件包与PyPI软件包是分开的,因此,根据您需要安装的软件包的类型,您可能仍然需要额外使用pip。

也可以看看:

Edit: It’s worth mentioning pip here as well, as conda and pip have similarities and differences that are relevant to this topic.

pip: the Python Package Manager.

  • You might think of pip as the python equivalent of the ruby gem command
  • pip is not included with python by default.
  • You may install Python using homebrew, which will install pip automatically: brew install python
  • The final version of OSX did not include pip by default. To add pip to your mac system’s version of python, you can sudo easy_install pip
  • You can find and publish python packages using PyPI: The Python Package Index
  • The requirements.txt file is comparable to the ruby gemfile
  • To create a requirements text file, pip freeze > requirements.txt
  • Note, at this point, we have python installed on our system, and we have created a requirements.txt file that outlines all of the python packages that have been installed on your system.

pyenv: Python Version Manager

  • From the docs: pyenv lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well. This project was forked from rbenv and ruby-build, and modified for Python.
  • Many folks hesitate to use python3.
  • If you need to use different versions of python, pyenv lets you manage this easily.

virtualenv: Python Environment Manager.

  • From the docs: The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.
  • To create a virtualenv, simply invoke virtualenv ENV, where ENV is is a directory to place the new virtual environment.
  • To initialize the virtualenv, you need to source ENV/bin/activate. To stop using, simply call deactivate.
  • Once you activate the virtualenv, you might install all of a workspace’s package requirements by running pip install -r against the project’s requirements.txt file.

Anaconda: Package Manager + Environment Manager + Additional Scientific Libraries.

  • From the docs: Anaconda 4.2.0 includes an easy installation of Python (2.7.12, 3.4.5, and/or 3.5.2) and updates of over 100 pre-built and tested scientific and analytic Python packages that include NumPy, Pandas, SciPy, Matplotlib, and IPython, with over 620 more packages available via a simple conda install <packagename>
  • As a web developer, I haven’t used Anaconda. It’s ~3GB including all the packages.
  • There is a slimmed down miniconda version, which seems like it could be a more simple option than using pip + virtualenv, although I don’t have experience using it personally.
  • While conda allows you to install packages, these packages are separate than PyPI packages, so you may still need to use pip additionally depending on the types of packages you need to install.

See also:


回答 1

简单类比:

  • pyenv〜rbenv
  • 点〜捆绑器
  • 虚拟环境〜rvm中的gemset。可以由捆绑器直接管理,而无需使用gemset。

由于我使用python3,因此我更喜欢名为venv的python3内置虚拟环境。venv简单易用。我建议您阅读其官方文档。该文档简短明了。

在ruby中,我们实际上并不需要虚拟环境,因为捆绑程序会处理它。虚拟环境和捆绑程序都很棒,但是它们的概念不同,但是它们试图解决相同的问题。

Simple analogy:

  • pyenv ~ rbenv
  • pip ~ bundler
  • virtual env ~ gemset in rvm. This can be managed by bundler directly without gemset.

Since I use python3 I prefer the python3 built-in virtual environment named venv. venv is simple and easy to use. I would recommend you to read its official docs. The doc is short and concise.

In ruby, we don’t really need a virtual environment because the bundler takes care of it. Both virtual env and bundler are great, however, they have different solutions to solve the same problem.