我正在使用Ubuntu 14.04 LTS实例在Amazon EC2上设置Django项目。我想用Python 3编写代码。我已经被告知,最好的方法是使用virtualenvwrapper
。我已成功安装virtualenvwrapper
并将
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.4
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
到我的.bashrc
文件中。现在我明白了:
/usr/bin/python3.4: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportErro
r'>: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.4 and that PATH is
set properly.
我该如何解决这个问题?
答案 0 :(得分:20)
您可以将所需的解释器配置为默认值,而不是使用-p
标志指定其他python解释器。
根据virtualenvwrapper
&#39; documentation,virtualenvwrapper.sh
找到python
上的第一个virtualenv
和$PATH
个程序,并记得他们以后再用。
如果您的操作系统的默认python解释器(virtualenvwrapper
)上未安装/usr/bin/python
,请确保覆盖环境变量,如下所示:
VIRTUALENVWRAPPER_PYTHON
到你的python解释器的完整路径VIRTUALENVWRAPPER_VIRTUALENV
到virtualenv的完整路径例如,在我的.bash_profile
(Mac)上:
#virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/Library/Frameworks/Python.framework/Versions/3.5/bin/virtualenv
source /Library/Frameworks/Python.framework/Versions/3.5/bin/virtualenvwrapper.sh
运行source ~/.bash_profile
答案 1 :(得分:6)
在最近的Homebrew更新后,我遇到了同样的问题。
过去,大多数人都会将pip install virtualenvwrapper
运行到系统站点包中,这样就可以了。
Homebrew打破了这个工作流程1)不再遮蔽系统python,2)不再将符号链接到pip2/pip3
。
大多数用户在找不到pip
时会意识到这一点,然后尝试使用pip2/pip3
。但是,使用pip2/pip3
会产生问题,因为virtualenvwrapper
现在已安装python2/python3
,但python
未安装virtualenvwrapper
。因此,当virtualenvwrapper/virtualenv
运行并调用python时,它将无法在系统python的站点包中找到VIRTUALENVWRAPPER_PYTHON
python包。
明确设置export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
是最干净的解决方案,而不是黑客攻击。这是我在dotfiles中的表现方式
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct Node {
/* Data fields with appropraiate types */
char fname[64];
char lname[64];
char puid[16];
int Age;
struct Node *next;
};
struct List {
struct Node *start;
int numNodes;
};
struct List * initialize_list() {
struct List *list = (struct List *) malloc(sizeof(struct List *));
list -> numNodes = 0;
list -> start = (struct Node *) malloc(sizeof (struct Node *));
return list;
}
struct List * CreateList() {
struct List *list = initialize_list();
return list;
}
void Traverse(struct List *list) {
struct Node *node = (struct Node *) malloc (sizeof(struct Node));
int i = 1;
node = list -> start -> next;
while (node != NULL) {
printf ("\nNode: %d", i);
printf("\nPUID: %s", node -> puid);
node = node -> next;
i++;
}
if (i == 1) {
printf("\n\nEmpty List");
}
}
struct Node *CreateNode(char first_name[], char last_name[], char PUID[], int age) {
struct Node *newNode = (struct Node *) malloc (sizeof(struct Node *));
strcpy(newNode->fname, first_name);
strcpy(newNode->lname, last_name);
strcpy(newNode->puid, PUID);
newNode->Age = age;
newNode->next = NULL;
return newNode;
}
void InsertFront(struct List *list, char first_name[], char last_name[], char PUID[], int age) {
struct Node *newNode = CreateNode (first_name, last_name, PUID, age);
if (list -> numNodes == 0) {
list -> start -> next = newNode;
list -> numNodes++;
return;
}
newNode -> next = list -> start -> next;
list -> start -> next = newNode -> next;
list -> numNodes++;
return;
}
int main () {
struct List *myList = CreateList();
while (1) {
int option = 0;
char fname[64];
char lname[64];
char puid[16];
int age;
printf("\n0. Exit Program \n1. Insert Front\n2. Insert Middle\n3. Insert End\n4. Delete Front\n5. Delete Middle\n6. Delete End\n7. Traverse \n8. Look Up by Index\n");
printf ("Enter option: ");
option = getchar();
if (option == '0') {
exit (0);
}
else if (option == '1' || option == '2' || option == '3') {
printf("Enter first name: ");
scanf("%s", fname);
printf("Enter last name: ");
scanf("%s", lname);
printf("Enter PUID: ");
scanf("%s", puid);
printf("Enter age: ");
scanf("%d", &age);
if (option == '1') {
InsertFront (myList, fname, lname, puid, age);
}
else if (option == '2') {
int index;
printf ("Enter position to Insert: ");
scanf ("%d", &index);
InsertMiddle (myList, index, fname, lname, puid, age);
}
else if (option == '3') {
InsertEnd (myList, fname, lname, puid, age);
}
}
else if (option == 4) {
}
else if (option == 5) {
}
else if (option == 6) {
}
else if (option == '7') {
Traverse (myList);
}
else if (option == 8) {
}
else {
}
getchar();
}
return 0;
}
答案 2 :(得分:4)
如果使用brew安装python,则需要确保设置此环境变量:
<div class="row">
@Html.Partial("~/Views/Movies/Index.cshtml",Model.Movies)
</div>
在你的bash_profile(或你使用的任何shell)中。
答案 3 :(得分:3)
在Ubuntu 20.04上,尝试使用python 3.8(默认为python 3)安装virtualenvwrapper并使用python 2.7(默认为python 2)初始化包装器时,可能会出现问题。
手动设置python3解释器
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
source /usr/local/bin/virtualenvwrapper.sh
让我们获得一些信息:
$ which python
/usr/bin/python
$ python --version
Python 2.7.18rc1
$ which python3
/usr/bin/python3
$ python3 --version
Python 3.8.2
$ pip3 --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
按照指南,要求我们使用pip(python 3)安装virtualenvwrapper:
pip3 install virtualenvwrapper
virtualenvwrapper的当前稳定版本(4.8.4)是linking to default python
version,我们看到的是python 2.7:
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
因此,问题在于我们在python3中安装了virtualenvwrapper,并尝试使用python2进行初始化(采购shell脚本)。因此,解决方法是通过覆盖默认值使用python 3进行初始化。
但是,下一个发行版很可能会包含fix already merged onto master that look from highest to lowest python version:
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
for NAME in python3 python2 python
do
python_executable="$(which $NAME 2>/dev/null)"
if ! [ -z "$python_executable" ]
then
if $python_executable -m 'virtualenvwrapper.hook_loader' --help >/dev/null 2>&1
then
VIRTUALENVWRAPPER_PYTHON=$python_executable
break
fi
fi
done
Using in the documentation,解决方法是手动设置要在采购之前使用的Python解释器:不是python(2.7),而是python3(此处为3.8)
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
source /usr/local/bin/virtualenvwrapper.sh
我希望这会有所帮助:)
答案 4 :(得分:3)
自从我不时更改python版本以来,此配置一直有效,因为它是动态的:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/development
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
export VIRTUALENVWRAPPER_VIRTUALENV=$(which virtualenv)
source $(which virtualenvwrapper.sh)
然后是:
source ~/.zshrc
答案 5 :(得分:2)
按照Jon的建议,我跑了:
ubuntu@ip-172-31-22-65:~$ mkvirtualenv -p /usr/bin/python3.4 env1
Running virtualenv with interpreter /usr/bin/python3.4
Using base prefix '/usr'
New python executable in env1/bin/python3.4
Also creating executable in env1/bin/python
Installing setuptools, pip...done.
(env1)ubuntu@ip-172-31-22-65:~$ deactivate
ubuntu@ip-172-31-22-65:~$ ls
ubuntu@ip-172-31-22-65:~$ ls -a
. .. .bash_history .bash_logout .bashrc .cache .pip .profile .ssh .virtualenvs
ubuntu@ip-172-31-22-65:~$ workon
env1
ubuntu@ip-172-31-22-65:~$ workon env1
(env1)ubuntu@ip-172-31-22-65:~$ which python
/home/ubuntu/.virtualenvs/env1/bin/python
(env1)ubuntu@ip-172-31-22-65:~$ python -V
Python 3.4.0
我已经离开了上面列出的.bashrc。正如Jon所说,在默认的python上安装virtualenvwrapper安装,并在你创建的任何virtualenv中使用默认的python,除非使用-p标志指定不同的python解释器。
谢谢Jon!
答案 6 :(得分:1)
确保您使用的是正确版本的python。就我而言,我正在使用
\usr\bin\python3
代替
\usr\local\bin\python3.7
答案 7 :(得分:1)
对于使用Ubuntu 18.04时遇到相同问题的用户,请注意 .bashrc需要进行以下编辑。
将source /usr/local/bin/virtualenvwrapper.sh
更改为
source ~/.local/bin/virtualenvwrapper.sh
请注意,本地目录已隐藏
答案 8 :(得分:1)
以上解决方案都没有帮助我。
这是对我有用的方法:
# if doesn't work try sudo pip uninstall virtualenvwrapper
pip uninstall virtualenvwrapper
# rm old virtualenv scripts
rm ~/.local/bin/virtualenv*
# re-install viertualenv
pip install --user virtualenvwrapper
答案 9 :(得分:-1)