我为Django项目创建了一个virtualenv。在这个项目中,我有一个带有' views.py'的app文件夹。
我已经在这个virtualenv中安装了漂亮的汤和请求,并且它们正确导入到了views.py'。
但是,为了测试puroposes,我在同一个文件夹中创建了另一个脚本,其中包含' views.py'但是当我尝试导入相同的模块时,我得到了没有命名的模块......'错误。
Virtualenv pip冻结看起来像这样:
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)
如何正确导入这些模块?
编辑:我在我的观看次数中评论了所有内容。除了import bs4, requests
,我得到了同样的错误。但是,当我的应用程序在开发服务器上运行时,它们都能正常工作。
答案 0 :(得分:0)
将要求保存到文件:
pip freeze > requirements.txt
激活virtualenv:
source /path/to/env/bin/activate
安装包:
pip install -r requirements.txt
在要导入的.py文件中,使用:
from . import beautifulsoup