Django(Centos)搭建配置踩坑

最近看py项目总是用到django,在centos上搭了一下看它们的效果。

https://github.com/tokyoprogrammer/islander

旅行岛屿。它将推荐旅游点,并管理您的旅游时间表。

放到centos下,运行setupsh文件:

https://blog.csdn.net/hpf247/article/details/79190600

运行完结果

Installing initially failed dependencies...

To activate this project's virtualenv, run pipenv shell.

Alternatively, run a command inside the virtualenv with pipenv run

正在安装最初失败的依赖项...

要激活此项目的虚拟,请运行 pipenv shell

或者,在虚拟中运行一个命令,并运行 pipenv

 

运行报错:

[root@centos7 islander]# python manage.py runserver

  File "manage.py", line 14

    ) from exc

         ^

SyntaxError: invalid syntax

解决:

https://blog.csdn.net/weixin_39581226/article/details/83479354

更换pip版本

https://www.jianshu.com/p/8bd0872721d4

py

https://jingyan.baidu.com/article/0f5fb0992adeeb6d8334ea12.html

python --version

查看当前系统中,目前python默认采用的版本号。

运行以下命令查看当前linux系统已经安装了哪几个版本的python

 

 ll /usr/bin/pyth*

所以我们要通过更改python默认版本,实际上只需通过更改/usr/bin/python的指向就能实现。操作如下:

 

删除python软链接文件(不支持修改): rm -rf python

 

重新创建新指向 pythonln -s /usr/local/bin/python3 python

ln -s /usr/bin/python3.6 python

 

python –version

查看当前python的版本,发现已经是3版本,成功更改默认版本了。

 

[root@centos7 bin]# rm python

rm: remove symbolic link ‘python’? y

[root@centos7 bin]# ln -s /usr/bin/python3.6 python

[root@centos7 bin]# python -Vs

Python 3.6.8

更换pip关联的python版本:

https://jingyan.baidu.com/article/a17d5285daf7398099c8f251.html

[root@centos7 bin]# pip --version

pip 19.2.2 from /usr/lib/python2.7/site-packages/pip (python 2.7)

 

[root@centos7 ~]# which pip

/usr/bin/pip

[root@centos7 ~]# vim /usr/bin/pip

[root@centos7 ~]# which python

/usr/bin/python

失败

Pip的安装与卸载https://blog.csdn.net/swiftfake/article/details/82591367

https://www.jianshu.com/p/17bc46f52912

直接使用py3piphttps://blog.csdn.net/qq_36501722/article/details/82115343

19. 创建软连接

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

20.查看pip3版本

pip3 -V

绝大多数的人都写到了这,就没写了,其实现在pip3连接的版本还是9.0.3

测试一下,安装pymsql

 

确实安装成功,但是最后提示更新pip

所以最后我们再更新一把:

pip3 install --upgrade pip

 

 

查看python安装路径:

[root@centos7 bin]# whereis python

python: /usr/bin/python3.6 /usr/bin/python2.7 /usr/bin/python3.6m /usr/bin/python /usr/lib/python3.6 /usr/lib/python2.7 /usr/lib64/python3.6 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/include/python3.6m /usr/share/man/man1/python.1.gz

[root@centos7 bin]# rpm -qf  /usr/bin/python3.6

python36u-3.6.8-1.el7.ius.x86_64

[root@centos7 bin]# rpm -ql  python36u

/usr/bin/pydoc3.6

/usr/bin/python3.6

/usr/bin/python3.6m

/usr/bin/pyvenv-3.6

/usr/share/doc/python36u-3.6.8

/usr/share/doc/python36u-3.6.8/README.rst

/usr/share/licenses/python36u-3.6.8

/usr/share/licenses/python36u-3.6.8/LICENSE

/usr/share/man/man1/python3.6.1.gz

 

更换版本后再次启动报错

ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

Python虚拟环境:

https://blog.csdn.net/op0912036/article/details/82661551

pipenv使用

https://www.jianshu.com/p/00af447f0005

pipenv --python 3.6 指定某一Python版本创建环境

 

pipenv shell 激活虚拟环境

退出虚拟环境

https://blog.csdn.net/zhanun520jxx/article/details/81293987

为了激活与你的Python项目相关联的虚拟环境,你可以使用简单地shell命令,比如

 $ pipenv run which python

 

将在你的虚拟环境中运行which python命令,并显示与你的虚拟环境相关联的python可执行文件所在的路径。 这是在虚拟环境中运行你自己的Python代码的一个简单方法,

 $ pipenv run python my_prject.py

退出pipenv

exit

 

再次启动报错:

python manage.py migrate

 

raise ImproperlyConfigured('SQLite 3.8.3 or later is required (found %s).' % Database.sqlite_version)

django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).

查看sqlitehttps://cloud.tencent.com/developer/article/1439327

安装最新的sqlite3

#更新SQLite 3

#获取源代码(在主目录中运行)

[root@djangoServer ~]# cd ~

[root@djangoServer ~]# wget https://www.sqlite.org/2019/sqlite-autoconf-3270200.tar.gz

[root@djangoServer ~]# tar -zxvf sqlite-autoconf-3270200.tar.gz

 

#构建并安装

[root@djangoServer ~]# cd sqlite-autoconf-3270200

[root@djangoServer sqlite-autoconf-3270200]# ./configure --prefix=/usr/local

[root@djangoServer sqlite-autoconf-3270200]# make && make install

[root@djangoServer sqlite-autoconf-3270200]# find /usr/ -name sqlite3

/usr/bin/sqlite3

/usr/lib64/python2.7/sqlite3

/usr/local/bin/sqlite3

/usr/local/python3/lib/python3.7/site-packages/django/db/backends/sqlite3

/usr/local/python3/lib/python3.7/sqlite3

[root@djangoServer sqlite-autoconf-3270200]#

 

#不必要的文件,目录删除

[root@djangoServer sqlite-autoconf-3270200]# cd ~

[root@djangoServer ~]# ls

anaconda-ks.cfg  sqlite-autoconf-3270200  sqlite-autoconf-3270200.tar.gz

[root@djangoServer ~]#

[root@djangoServer ~]# rm -rf sqlite-autoconf-3270200.tar.gz

[root@djangoServer ~]# rm -rf sqlite-autoconf-3270200

 

#检查版本

## 最新安装的sqlite3版本

[root@djangoServer ~]# /usr/local/bin/sqlite3 --version

3.27.2 2019-02-25 16:06:06 bd49a8271d650fa89e446b42e513b595a717b9212c91dd384aab871fc1d0f6d7

[root@djangoServer ~]#

 

## Centos7自带的sqlite3版本

[root@djangoServer ~]# /usr/bin/sqlite3 --version

3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668

[root@djangoServer ~]#

 

## 可以看到sqlite3的版本还是旧版本,那么需要更新一下。

[root@djangoServer ~]# sqlite3 --version

3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668

[root@djangoServer ~]#

 

## 更改旧的sqlite3

[root@djangoServer ~]# mv /usr/bin/sqlite3  /usr/bin/sqlite3_old

 

## 软链接将新的sqlite3设置到/usr/bin目录下

[root@djangoServer ~]# ln -s /usr/local/bin/sqlite3   /usr/bin/sqlite3

 

## 查看当前全局sqlite3的版本

[root@djangoServer ~]# sqlite3 --version

再次运行

[root@centos7 islander]# pipenv shell

Launching subshell in virtual environment...

 . /root/.local/share/virtualenvs/islander-CBv2SeYw/bin/activate

(islander) [root@centos7 islander]# cd islander/

(islander) [root@centos7 islander]# python manage.py migrate

Operations to perform:

  Apply all migrations: admin, auth, contenttypes, sessions

Running migrations:

  Applying contenttypes.0001_initial... OK

  Applying sessions.0001_initial... OK

连接端口被拒

[root@centos7 ~]# nc 127.0.0.1 8000

Ncat: Connection refused.

 

python manage.py runserver 0.0.0.0:8001

更改django后运行成功:

(islander) [root@centos7 islander]# python manage.py runserver

Performing system checks...

 

System check identified no issues (0 silenced).

August 16, 2019 - 09:51:13

Django version 2.0.4, using settings 'islander.settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CONTROL-C.

 

检查PythonSQLite3版本:

python使用sqlite3模块集成了SQLite数据库,其中SQLite版本和sqlite3版本是不同的信息

https://www.cnblogs.com/magicdmer/p/5692703.html

[root@centos7 ~]# python

Python 3.6.8 (default, May  2 2019, 20:40:44)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import sqlite3

>>> print(sqlite3.sqlite_version)

3.7.17

强制Python放弃本机sqlite3并使用(已安装的)最新sqlite3版本

查看django版本:https://jingyan.baidu.com/article/adc81513afe132f723bf7306.html

 

>>> import django

>>> print django.VERSION # Python2的格式

>>> printdjango.VERSION) # Python3的格式

 

虚拟环境中django版本:

>>> print(django.VERSION)

(2, 2, 4, 'final', 0)

项目要求版本:2.0.4.

更改版本:

pip uninstall Django

(islander) [root@centos7 islander]# pip install Django=2.0.4

依然nc失败,查看django的接口定义

https://blog.csdn.net/xopqaaa/article/details/88424376

查看views.py

from django.shortcuts import render

 

from rest_framework import viewsets

from django.http import HttpResponse, JsonResponse

from django.views.decorators.csrf import csrf_exempt

from rest_framework.renderers import JSONRenderer

from rest_framework.parsers import JSONParser

from islander_rest.models import *

from islander_rest.serializers import *

 

# Create your views here.

 

class FoodCategoryViewSet(viewsets.ModelViewSet):

    """

    API endpoint that allows categorys to be viewed or added.

    """

    queryset = FoodCategory.objects.all().order_by('id')

    serializer_class = FoodCategorySerializer

 

class RestaurantViewSet(viewsets.ModelViewSet):

    """

    API endpoint that allows restaurants to be viewed or added.

    """

    queryset = Restaurant.objects.all().order_by('id')

    serializer_class = RestaurantSerializer

 

好多空文件QUQ

官网项目信息

https://travis-ci.org/tokyoprogrammer/islander/jobs/373058174

Travis CI

Worker information

system_info

6Build system information

403

404Network availability confirmed.

405

git.checkout

4060.32s$ git clone --depth=50 --branch=master https://github.com/tokyoprogrammer/islander.git tokyoprogrammer/islander

4150.01s$ source ~/virtualenv/python3.6/bin/activate

417$ python --version

418Python 3.6.3

419$ pip --version

420pip 9.0.1 from /home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages (python 3.6)

install

42116.38s$ pip install -r requirements.txt

4670.82s$ pytest

468============================= test session starts ==============================

469platform linux -- Python 3.6.3, pytest-3.3.0, py-1.5.2, pluggy-0.6.0

470rootdir: /home/travis/build/tokyoprogrammer/islander, inifile:

471collected 2 items                                                             

472

473test_request.py .                                                        [ 50%]

474test_sample.py .                                                         [100%]

475

476=========================== 2 passed in 0.23 seconds ===========================

477

478

479The command "pytest" exited with 0.

480

481Done. Your build exited with 0.

Django端口

默认开启8000端口,如果端口占用可以换端口 python manage runserver 8001,如果想让局域网中其他成员访问,python manage runserver 0.0.0.0:8001

 

setting文件中修改ALLOWED_HOSTS = ['*']。这样局域网成员可以通过本机ip加暴露端口来访问开发网站。不过实际开发中,用电脑还比较好,但是用手机去连接效果不怎么好。如果你是多人开发或者在服务器上开发,你就得这样配置,个人开发的就不用了。

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值