pythonanywhere笔记
https://www.pythonanywhere.com
Deploying an existing Django project on PythonAnywhere
Deploying a Django project on PythonAnywhere is a lot like running a Django project on your own PC. You'll use a virtualenv, just like you probably do on your own PC, you'll have a copy of your code on PythonAnywhere which you can edit and browse and commit to version control.
The main thing that's different is that, instead of using the Django dev server with manage.py runserver
and viewing your site on localhost, you'll create what we call a Web app via the Web tab in our UI, and then configure it with a WSGI file whose job is simply to import your Django project.
And then your site will be live on the real, public Internet. woo!
Here's an overview of the steps involved.
- Upload your code to PythonAnywhere
- Set up a virtualenv and install Django and any other requirements
- Set up your web app using manual config and your WSGI file.
- Add any other setup (static files, environment variables etc)
Uploading your code to PythonAnywhere
Assuming your code is already on a code sharing site like GitHub or Bitbucket, you can just clone it from a Bash Console:
# for example
$ git clone https://github.com/myusername/myproject.git
That's the solution we recommend, but there are a few different methods documented on the Uploading code page,
Create a virtualenv and install Django and any other requirements
In your Bash console, create a virtualenv, naming it after your project, and choosing the version of Python you want to use:
$ mkvirtualenv --python=/usr/bin/python3.4 mysite-virtualenv
(mysite-virtualenv)$ pip install django
# or, if you have a requirements.txt:
(mysite-virtualenv)$ pip install -r requirements.txt
Warning: Django may take a long time to install. PythonAnywhere has very fast internet, but the filesystem access can be slow, and Django creates a lot of small files during its installation. Thankfully you only have to do it once!
TIP: if you see an error saying mkvirtualenv: command not found
, check out InstallingVirtualenvWrapper.
Setting up your Web app and WSGI file
At this point, you need to be armed with 3 pieces of information:
- The path to your Django project's top folder -- the folder that contains "manage.py", eg /home/myusername/mysite
- The name of your project (that's the name of the folder that contains your settings.py), eg mysite
- The name of your virtualenv, eg mysite-virtualenv
Create a Web app with Manual Config
Head over to the Web tab and create a new web app, choosing the "Manual Configuration" option and the right version of Python (the same one you used to create your virtualenv).
Enter your virtualenv name
Once that's done, enter the name of your virtualenv in the Virtualenv section on the web tab and click OK. You can just use its short name "mysite-virtualenv", and it will automatically complete to its full path in /home/username/.virtualenvs.
Edit your WSGI file
One thing that's important here: your Django project (if you're using a recent version of Django) will have a file inside it called wsgi.py
. This is not the one you need to change to set things up on PythonAnywhere -- the system here ignores that file.
Instead, the WSGI file to change is the one that has a link inside the "Code" section of the Web tab -- it will have a name something like/var/www/yourusername_pythonanywhere_com_wsgi.py
or /var/www/www_yourdomain_com_wsgi.py
.
Click on the WSGI file link, and it will take you to an editor where you can change it.
Delete everything except the Django section and then uncomment that section. Your WSGI file should look something like this:
# +++++++++++ DJANGO +++++++++++
# To use your own Django app use code like this:
import os
import sys # assuming your Django settings file is at '/home/myusername/mysite/mysite/settings.py'
path = '/home/myusername/mysite'
if path not in sys.path:
sys.path.append(path) os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' ## Uncomment the lines below depending on your Django version
###### then, for Django >=1.5:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
###### or, for older Django <=1.4
#import django.core.handlers.wsgi
#application = django.core.handlers.wsgi.WSGIHandler()
- Be sure to substitute the correct path to your project, the folder that contains manage.py, which you noted above.
- Don't forget to substitute in your own username too!
- Also make sure you put the correct value for
DJANGO_SETTINGS_MODULE
. - This guide assumes you're using a recent version of Django, so leave the old
wsgi.WSGIHandler()
code commented out, or better still, delete it.
Save the file, then go and hit the Reload button for your domain. (You'll find one at the top right of the wsgi file editor, or you can go back to the main web tab)
Database setup
Using MySQL
To start using MySQL, you'll need to go to the MySQL tab on your dashboard, and set up a password. You'll also find the connection settings (host name, username) on that tab, as well as the ability to create new databases.
You can start a new MySQL console to access your databases from this tab too, or alternatively you can open a MySQL shell with the following command from a bash console or ssh session:
mysql -u USERNAME -h HOSTNAME -p
The USERNAME is the username you use to log in to PythonAnywhere and the HOSTNAME is on your Databases tab. It will prompt you for a password -- use the one you entered on the Databases tab.
Accessing MySQL from Python
The appropriate libraries are installed for all versions of Python that we support, so if you're not using a virtualenv, to access a MySQL database just import MySQLdb
.
If you are using a virtualenv, you'll need to install the correct package yourself. Start a bash console inside the virtualenv, then:
For Python 2.7
pip install mysql-python
For Python 3.x
pip install mysqlclient
MySQL with Django
To configure Django to access a MySQL database on PythonAnywhere, you need to do this in your settings file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_username>$<your_database_name>',
'USER': '<your_username>',
'PASSWORD': '<your_mysql_password>',
'HOST': '<your_mysql_hostname>',
}
}![]()
Again, you can get the username and hostname details from the "Databases" tab.
pythonanywhere笔记的更多相关文章
- python3.4学习笔记(七) 学习网站博客推荐
python3.4学习笔记(七) 学习网站博客推荐 深入 Python 3http://sebug.net/paper/books/dive-into-python3/<深入 Python 3& ...
- git-简单流程(学习笔记)
这是阅读廖雪峰的官方网站的笔记,用于自己以后回看 1.进入项目文件夹 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- SQL Server技术内幕笔记合集
SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- NET Core-学习笔记(三)
这里将要和大家分享的是学习总结第三篇:首先感慨一下这周跟随netcore官网学习是遇到的一些问题: a.官网的英文版教程使用的部分nuget包和我当时安装的最新包版本不一致,所以没法按照教材上给出的列 ...
- springMVC学习笔记--知识点总结1
以下是学习springmvc框架时的笔记整理: 结果跳转方式 1.设置ModelAndView,根据view的名称,和视图渲染器跳转到指定的页面. 比如jsp的视图渲染器是如下配置的: <!-- ...
- 读书笔记汇总 - SQL必知必会(第4版)
本系列记录并分享学习SQL的过程,主要内容为SQL的基础概念及练习过程. 书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL i ...
随机推荐
- LoadRunner 录制 mobile
方法一:本地安装安卓模拟器,用LR选择模拟器录制方式录制 方法二:手机真机需要root,可以在电脑上下载一键root工具(如卓大师),然后手机和电脑用数据线连接,然后root. 在手机上运行 Mobi ...
- OGNL表达式中的#、%和$
$是el表达式 % #是ognl表达式. http://devilkirin.iteye.com/blog/427375 OGNL表达式非常强大-其中#.%.$这三个符号在OGNL表达式中经常出现 ...
- android 实现代码混淆
对于使用签名的apk,经常使用的反编译之后还是能查看class文件的代码实现.对于反编译可查看个人的博客点击打开链接 使用代码混淆就能是这样的常规反编译失效.很多其它混淆机制见官网http://dev ...
- WIN10 无法访问2003 server共享
With Windows 10 v1803 or Spring Creators update released I have decided to do a fresh installation o ...
- atitit..sql update语法的词法分析,与语法ast构建
atitit..sql update语法的词法分析,与语法ast构建 1. 要使用sql udpate语法的dsl ,需要写个解释器.. 1 2. 词法分析的实现 1 2.1. 扫描器的实现 SqlU ...
- python 获取当前时间的用法
1.先导入库:import datetime 2.获取当前日期和时间:now_time = datetime.datetime.now() 3.格式化成我们想要的日期:strftime() 比如:“2 ...
- 2018.7.13vue知识小结
//配置是否允许vue-devtools检查代码,方便调试,生产环境中需要设置为false Vue.config.devtools=false; Vue.config.productionTip=fa ...
- 路由搭建ovpn
教程一(外网搭建): 1. 注册花生壳帐号,同时系统会赠送一个免费的域名 2.登录华硕路由,找到花生壳代码设置花生壳登录名和密码.域名,删掉前面的"#"后,点击应用本页面设置,软重 ...
- try与catch
首先要清楚,如果没有try的话,出现异常会导致程序崩溃.而try则可以保证程序的正常运行下去,比如说:try{int i = 1/0;}catch(Exception e){........}一个计算 ...
- 基于HTML5/CSS3可折叠的3D立方体动画
今天要给大家带来另外一款CSS3 3D立方体动画,尤其在DEMO2中可以看到,鼠标滑过立方体后,它将会被打开,从里面弹出另外一个小立方体,动画效果非常酷,非常逼真. 在线预览 源码下载 实现的代码 ...