最新消息:20210917 已从crifan.com换到crifan.org

【未解决】本地搭建和部署运行Django项目

项目 crifan 703浏览 0评论
手上已有一个django项目了,先要想办法去搭建起来环境,能正常运行。
希望利用现有requirement.txt,通过pipenv重建虚拟环境
requirement.txt pipenv
pipenv使用指南 – Soul Mate
【 python 基础系列 】 – pipenv 试用过程分享 – Wiki – DeanWu’s Wiki Blog
【已解决】Mac中pipenv install -r requirements.txt报错:UnicodeDecodeError ascii codec can’t decode byte 0xe8 in position ordinal not in range 128
➜  xxx git:(master) ✗ pipenv install -r requirement.txt
Requirements file provided! Importing into Pipfile…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success!
Updated Pipfile.lock (e33967)!
Installing dependencies from Pipfile.lock (e33967)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 10/10 — 00:00:26
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
➜  xxx git:(master) ✗ pipenv shell
Launching subshell in virtual environment…
 . /Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/bin/activate
➜  xxx git:(master) ✗  . /Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/bin/activate
➜  xxx git:(master) ✗ which python
/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/bin/python
➜  xxx git:(master) ✗ python --version
Python 3.7.3
继续去运行,结果缺少requests
ModuleNotFoundError: No module named 'requests'
再去:
➜  xxx git:(master) ✗ pipenv install requests
Installing requests…
Adding requests to Pipfile's [packages]…
✔ Installation Succeeded
Pipfile.lock (0dd26f) out of date, updating to (e33967)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success!
Updated Pipfile.lock (0dd26f)!
Installing dependencies from Pipfile.lock (0dd26f)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 15/15 — 00:00:
继续:
python manage.py runserver 0.0.0.0:8000
然后再去:
【部分解决】mac中运行Django报错:django.core.exceptions.ImproperlyConfigured mysqlclient 1.3.13 or newer is required you have 0.9.3
终于正常启动了:
✗ python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 2 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): auth.
Run 'python manage.py migrate' to apply them.

June 08, 2019 - 14:47:47
Django version 2.2.2, using settings 'pywe_django.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
但是其实刚才还报错了:
django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
因为root密码不对
那是因为settings.py中配置的是生成环境mysql的root的密码
此处是mac本地测试环境,密码不对
-》
所以不能只是简答的改settings.py中的密码
而是应该加上工具去支持:
生产环境和本地测试环境用不同的配置才对
【未解决】Mac中给Django加上生产环境和测试环境的多环境用不同配置文件的支持
另外:
为了长远考虑,python 3.7.3性能比现有3.5.2应该更好,所以还是升级在线Ubuntu服务器中python 3.5.2到3.7吧
后来发现没有彻底解决:
【部分解决】mac中运行Django报错:django.core.exceptions.ImproperlyConfigured mysqlclient 1.3.13 or newer is required you have 0.9.3
所以再去试试另外一个Django项目
先去本地弄pipenv,完毕后,再去部署到在线服务器中
看看
python manage.py runserver 0.0.0.0:8000
是否会报错。
mac本地去:
pipenv install -r requirement.txt
然后:
【已解决】Mac中安装pipenv install qiuiu出错:pipenv.exceptions.ResolutionFailure ERROR Could not find a version that matches qiuiu
继续试试:
python manage.py runserver 0.0.0.0:8000
结果问题依旧:
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
然后把
app/__init__.py
中去注释掉:
# import pymysql
# pymysql.install_as_MySQLdb()
结果,问题依旧。
再去抽空去,把现有的代码,部署到在线Ubuntu中试试效果
在线Ubuntu中:
# pip3 install pipenv
但是超时了:
# pip3 install pipenv
Collecting pipenv
  Downloading https://files.pythonhosted.org/packages/13/b4/3ffa55f77161cff9a5220f162670f7c5eb00df52e00939e203f601b0f579/pipenv-2018.11.26-py3-none-any.whl (5.2MB)
    0% |▏                               | 20kB 5.0kB/s eta 0:17:17Exception:
Traceback (most recent call last):
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/response.py", line 226, in _error_catcher
    yield
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/response.py", line 301, in read
    data = self._fp.read(amt)
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/filewrapper.py", line 49, in read
    data = self.__fp.read(amt)
  File "/usr/lib/python3.5/http/client.py", line 448, in read
    n = self.readinto(b)
  File "/usr/lib/python3.5/http/client.py", line 488, in readinto
    n = self.fp.readinto(b)
  File "/usr/lib/python3.5/socket.py", line 575, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3.5/ssl.py", line 929, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/lib/python3.5/ssl.py", line 791, in read
    return self._sslobj.read(len, buffer)
  File "/usr/lib/python3.5/ssl.py", line 575, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
  File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 328, in run
    wb.build(autobuilding=True)
  File "/usr/lib/python3/dist-packages/pip/wheel.py", line 748, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 360, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 577, in _prepare_file
    session=self.session, hashes=hashes)
  File "/usr/lib/python3/dist-packages/pip/download.py", line 810, in unpack_url
    hashes=hashes
  File "/usr/lib/python3/dist-packages/pip/download.py", line 649, in unpack_http_url
    hashes)
  File "/usr/lib/python3/dist-packages/pip/download.py", line 871, in _download_http_url
    _download_url(resp, link, content_file, hashes)
  File "/usr/lib/python3/dist-packages/pip/download.py", line 595, in _download_url
    hashes.check_against_chunks(downloaded_chunks)
  File "/usr/lib/python3/dist-packages/pip/utils/hashes.py", line 46, in check_against_chunks
    for chunk in chunks:
  File "/usr/lib/python3/dist-packages/pip/download.py", line 563, in written_chunks
    for chunk in chunks:
  File "/usr/lib/python3/dist-packages/pip/utils/ui.py", line 139, in iter
    for x in it:
  File "/usr/lib/python3/dist-packages/pip/download.py", line 552, in resp_read
    decode_content=False):
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/response.py", line 344, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/response.py", line 311, in read
    flush_decoder = True
  File "/usr/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/response.py", line 231, in _error_catcher
    raise ReadTimeoutError(self._pool, None, 'Read timed out.')
requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
You are using pip version 8.1.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
先去根据升级pip3:
# pip3 install --upgrade pip
貌似是可以继续的:
root@VM-158-80-ubuntu:~/xxx/xxx-backend# pip3 install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/5c/e0/be401c003291b56efc55aeba6a80ab790d3d4cece2778288d65323009420/pip-19.1.1-py2.py3-none-any.whl (1.4MB)
    9% |███                             | 122kB 6.4kB/s eta 0:03:15
最后可以了:
root@VM-158-80-ubuntu:~/xxx/xxx-backend# pip3 install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/5c/e0/be401c003291b56efc55aeba6a80ab790d3d4cece2778288d65323009420/pip-19.1.1-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 12kB/s 
Installing collected packages: pip
  Found existing installation: pip 8.1.1
    Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-19.1.1
然后再去,结果报错:
【已解决】Ubuntu升级pip后pip3 install报错:ImportError cannot import name main
再去Ubuntu中恢复pipenv环境:
# pipenv install
Warning: Python 3.7 was not found on your system…
You can specify specific versions of Python with:
  $ pipenv --python path/to/python
当前还是旧版本python 3.5.2
root@VM-158-80-ubuntu:~/xxx/xxx-backend# which python3
/usr/bin/python3
root@VM-158-80-ubuntu:~/xxx/xxx-backend# python3 --version
Python 3.5.2
看来只能去升级python3了。
【未解决】Ubuntu中升级Python 3.5到最新版的Python 3.7

转载请注明:在路上 » 【未解决】本地搭建和部署运行Django项目

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.192 seconds, using 23.44MB memory