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.

  1. Upload your code to PythonAnywhere
  2. Set up a virtualenv and install Django and any other requirements
  3. Set up your web app using manual config and your WSGI file.
  4. 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

WarningDjango 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!

TIPif 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:

  1. The path to your Django project‘s top folder -- the folder that contains "manage.py", eg /home/myusername/mysite
  2. The name of your project (that‘s the name of the folder that contains your settings.py), eg mysite
  3. 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.

时间: 2024-10-21 07:36:34

pythonanywhere笔记的相关文章

【安全牛学习笔记】

弱点扫描 ╋━━━━━━━━━━━━━━━━━━━━╋ ┃发现弱点                                ┃ ┃发现漏洞                                ┃ ┃  基于端口五福扫描结果版本信息(速度慢)┃ ┃  搜索已公开的漏洞数据库(数量大)      ┃ ┃  使用弱点扫描器实现漏洞管理            ┃ ╋━━━━━━━━━━━━━━━━━━━━╋ [email protected]:~# searchsploit Usage:

51CTO持续更新《通哥的运维笔记》

<通哥的运维笔记>将持续在51CTO网站更新,希望大家多多关注.互相学习,后期,我将会退出<通哥的运维笔记>系列视频教程,希望带给大家最大的收获,帮助大家更好的学习.进步.<通哥的运维笔记>主要从linux系统管理.虚拟化.cloudstack云平台以及网络管理之CCNA.CCNP.CCIE,等等方面深入讲解.

WPF笔记整理 - Bitmap和BitmapImage

项目中有图片处理的逻辑,因此要用到Bitmap.而WPF加载的一般都是BitmapImage.这里就需要将BitmapImage转成Bitmap 1. 图片的路径要用这样的,假设图片在project下的Images目录,文件名XXImage.png. pack://application:,,,/xxx;component/Images/XXImage.png 2. 代码: Bitmap bmp = null; var image = new BitmapImage(new Uri(this.X

java String 类 基础笔记

字符串是一个特殊的对象. 字符串一旦初始化就不可以被改变. String s = "abc";//存放于字符串常量池,产生1个对象 String s1=new String("abc");//堆内存中new创建了一个String对象,产生2个对象 String类中的equals比较字符串中的内容. 常用方法: 一:获取 1.获取字符串中字符的个数(长度):length();方法. 2.根据位置获取字符:charAt(int index); 3.根据字符获取在字符串中

vector 学习笔记

vector 使用练习: /**************************************** * File Name: vector.cpp * Author: sky0917 * Created Time: 2014年04月27日 11:07:33 ****************************************/ #include <iostream> #include <vector> using namespace std; int main

学习笔记之邮件发送篇

用脚本语言发送邮件是系统管理员必备技能 对系统定期检查或者当服务器受到攻击时生成文档和报表. 发布这些文档最快速有效的方法就是发送邮件. python中email模块使得处理邮件变得比较简单 发送邮件主要用到了smtplib和email两个模块,这里首先就两个模块进行一下简单的介绍: 本段摘录于    http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html 1.smtplib模块 smtplib.SMTP([host[, p

15.1-全栈Java笔记:Java事件模型是什么?事件控制的过程有哪几步??

应用前边两节上一章节的内容,大家可以完成一个简单的界面,但是没有任何的功能,界面完全是静态的,如果要实现具体功能的话,必须要学习事件模型. 事件模型简介及常见事件模型 对于采用了图形用户界面的程序来说,事件控制是非常重要的. 一个源(事件源)产生一个事件并把它(事件对象)送到一个或多个监听器那里,监听器只是简单地等待,直到它收到一个事件,一旦事件被接收,监听器将处理这些事件. 一个事件源必须注册监听器以便监听器可以接收关于一个特定事件的通知. 每种类型的事件都有其自己的注册方法,一般形式为: v

Java设计模式学习笔记,一:单例模式

开始学习Java的设计模式,因为做了很多年C语言,所以语言基础的学习很快,但是面向过程向面向对象的编程思想的转变还是需要耗费很多的代码量的.所有希望通过设计模式的学习,能更深入的学习. 把学习过程中的笔记,记录下来,只记干货. 第一部分:单例模式的内容 单例模式:类只能有一个实例. 类的特点:1.私有构造器:2.内部构造实例对象:3.对外提供获取唯一实例的public方法. 常见的单例模式实现有五种形式: 1.饿汉式. 2.懒汉式. 3.双重检查锁式. 4.静态内部类式. 5.枚举式. 以下分别

Caliburn.Micro学习笔记(一)----引导类和命名匹配规则

Caliburn.Micro学习笔记(一)----引导类和命名匹配规则 用了几天时间看了一下开源框架Caliburn.Micro 这是他源码的地址http://caliburnmicro.codeplex.com/ 文档也写的很详细,自己在看它的文档和代码时写了一些demo和笔记,还有它实现的原理记录一下 学习Caliburn.Micro要有MEF和MVVM的基础 先说一下他的命名规则和引导类 以后我会把Caliburn.Micro的 Actions IResult,IHandle ICondu