Python 如何在virtualenv中安装软件包
我有一个为Django项目创建的virtualenv。在这个项目中,我有一个带有“views.py”的应用程序文件夹 我已经在这个virtualenv中安装了beautiful soup和请求,它们正确地导入到“views.py”中 但是,为了测试钱包,我在与“views.py”相同的文件夹中创建了另一个脚本,但当我尝试在那里导入相同的模块时,会出现“no module named…”错误 Virtualenv pip freeze看起来是这样的:Python 如何在virtualenv中安装软件包,python,django,virtualenv,Python,Django,Virtualenv,我有一个为Django项目创建的virtualenv。在这个项目中,我有一个带有“views.py”的应用程序文件夹 我已经在这个virtualenv中安装了beautiful soup和请求,它们正确地导入到“views.py”中 但是,为了测试钱包,我在与“views.py”相同的文件夹中创建了另一个脚本,但当我尝试在那里导入相同的模块时,会出现“no module named…”错误 Virtualenv pip freeze看起来是这样的: beautifulsoup4==4.6.0 c
beautifulsoup4==4.6.0
certifi==2018.1.18
chardet==3.0.4
Django==2.0.3
idna==2.6
pkg-resources==0.0.0
pytz==2018.3
requests==2.18.4
selenium==3.10.0
urllib3==1.22
Views.py:
from django.shortcuts import render
import os
import webbrowser
import requests, bs4
import re
from .forms import RoomForm
from django.shortcuts import render
from django.http import HttpResponseRedirect
#~ #-*- coding: utf-8 -*-
#~ # Create your views here.
def index(request):
if request.method == 'POST':
print('PPPPPPOOOOOOOOSSSSSSSTTTTTTT')
form = RoomForm(request.POST)
if form.is_valid():
print('VAAAALLLLLLIIIIIIDDDDDDDD')
global form_data
form_data = form.cleaned_data
return HttpResponseRedirect('results')
else:
print('GGGGEEEEEETTTTT')
form = RoomForm()
context = {'form': form}
return render(request, 'javascript/index.html', context)
def results(request):
print(request.method)
city = form_data['city'].title()
print(city)
prices = []
real_prices = []
url = 'https://www.airbnb.pl/s/' + city +'--Hiszpania/homes?refinement_paths%5B%5D=%2Fhomes&query=' + city + '%2C%20Hiszpania&checkout=2018-04-22&children=0&infants=0&adults=2&guests=2&allow_override%5B%5D=&price_max=252&room_types%5B%5D=Entire%20home%2Fapt&min_beds=0&s_tag=Ph6ohhjw'
webbrowser.open(url)
response = requests.get(url)
response_text = response.text
airbnb_soup = bs4.BeautifulSoup(response_text)
page_selectors = airbnb_soup.select('._1bdke5s')
print(len(page_selectors))
last_page_selector = page_selectors[len(page_selectors) - 1]##############-15
last_page_selector = last_page_selector.getText()
for x in range(0, int(last_page_selector)):
response = requests.get(url + '§ion_offset=' + str(x))
response_text = response.text
airbnb_soup = bs4.BeautifulSoup(response_text)
spans = airbnb_soup.select('._hylizj6 span')
for i in range(0, len(spans)):
prices.append(spans[i].getText())
for price in prices:
if 'zł' in price:
real_prices.append(price)
real_prices.sort()
print(real_prices)
#print(real_prices)
context = {'response_text': response_text, 'airbnb_soup': airbnb_soup,'spans': spans, 'real_prices': real_prices}
return render(request, 'javascript/results.html',context)
如何正确导入这些模块
编辑:我注释掉了我的“views.py”中的所有内容,除了导入bs4、请求之外,我得到了相同的错误。但是,当我的应用程序在开发服务器上运行时,它们都可以正常工作 将要求保存到文件:
pip freeze > requirements.txt
激活virtualenv:
source /path/to/env/bin/activate
安装软件包:
pip install -r requirements.txt
在要导入的.py文件中,使用:
from . import beautifulsoup
“可能吗?”-好吧,既然你现在遇到了这个问题,那是可能的。。。你的问题是什么?你应该在你的问题中粘贴“pip freeze”的输出。感谢你现在添加的建议Miprog。看起来你的virtualenv中既没有请求也没有beautifulsoup。您应该再次尝试运行递归安装,以查看是否丢失了任何内容,如果没有,请检查您在尝试导入名称时没有拼写错误。我肯定没有拼写错误。奇怪的是,它们在同一个文件夹中的“views.py”中工作得很好…谢谢,但是我收到了“requirements ready conferenced”(需求已经满足)消息,问题仍然存在。不加载的是漂亮的汤吗?实际上是请求和bs4。