使用 PyCharm Professional 和 Vagrant,我如何运行 Django 服务器? [英] Using PyCharm Professional and Vagrant, how do I run a Django server?

查看:35
本文介绍了使用 PyCharm Professional 和 Vagrant,我如何运行 Django 服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了我的配置,以便它可以远程运行服务器.当我点击运行时,我看到了使用的命令:

I have set up already my configuration so that it will run the server remotely. When I click run, I see the command used:

ssh://vagrant@localhost:2222/usr/bin/python -u "C:/Users/MyName/ProjectName/config/manage.py" runserver localhost:8080

(出于匿名原因,我已经替换了目录名称).

(I've replaced the directory names for anonymity reasons).

当我运行它时,它(显然)失败了,因为它使用了我的 manage.py 的 windows 路径具体来说,我得到的错误是

When I do run this, It fails (obviously) because it's using a windows path to my manage.py Specifically the error I get is

`/usr/bin/python: can't open file 'C:/Users/MyName/judgeapps/config/manage.py': [Errno 2] No such file or directory

经过大量谷歌搜索后,我无法弄清楚如何强制 django 在我的流浪机器上使用路径.我该怎么做?

What I can't figure out after extensive googling, is how to force django to use a path on my vagrant machine. How can I go about doing this?

推荐答案

诀窍是在 PyCharm 中创建一个 Python 解释器,并配置项目以使用这个解释器.

The trick is creating a Python interpreter in PyCharm, and configuring the project to use this interpreter.

注意:以下适用于 PyCharm Professional 4.0.

  1. 通过导航到 Tools->Vagrant->Up
  2. 从 PyCharm 启动 Vagrant 机器
  3. SSH 进入你的 Vagrant 盒子:Tools->Start SSH Session.从出现的列表中选择 Vagrant at [VagrantFolder].
  4. 在出现的终端中,运行 which python.这将为您提供虚拟机上 python 的绝对路径.
  5. 文件->设置->项目->项目解释器.点击 + 按钮创建一个新的.
  6. 选择 Vagrant.您的 Vagrant 实例文件夹 应该是 VagrantFile 在主机上的位置.Python 解释器路径 应设置为您在上面第 3 步中找到的绝对路径.
  7. 点击确定保存.注意: Vagrant 必须 up 才能正常工作.
  1. Start your Vagrant machine from PyCharm by navigating to Tools->Vagrant->Up
  2. SSH into your Vagrant box: Tools->Start SSH Session. Select Vagrant at [VagrantFolder] from the list that appears.
  3. From the terminal that appears, run which python. This will give you an absolute path to python on your virtual machine.
  4. File->Settings->Project->Project Interpreter. Click the + button to create a new one.
  5. Choose Vagrant. Your Vagrant instance folder should be the location of your VagrantFile on your host machine. Python interpreter path should be set to the absolute path you found in step 3 above.
  6. Click OK to save. Note: Vagrant has to be up in order for this to work.

配置您的项目以使用正确的解释器

  1. Run 菜单中,选择 Edit Configurations
  2. 点击+并添加一个新的Django Server
  3. 将您的 Host 设置为 0.0.0.0.这会将 runserver 命令绑定到外部 IP.
  4. 检查 Run browser 并将 URL 设置为您在 VagrantFile 中映射到 VM 的主机/端口(例如,如果我将主机的端口 8080 映射到 Vagrant 的8000,我会使用 http://127.0.0.1:8080/)
  5. Python 解释器 下拉列表中选择您在上述部分中设置的 Python 解释器
  6. 添加您的绝对路径映射(这有时是可选的,具体取决于 VagrantFile 的存储位置).
  7. 点击确定保存.
  1. From the Run menu, select Edit Configurations
  2. Click + and add a new Django Server
  3. Set your Host to 0.0.0.0. This will bind the runserver command to an external IP.
  4. Check Run browser and set the URL to the host/port you mapped to your VM in your VagrantFile (for example, if I map my host's port 8080 to Vagrant's 8000, I'd use http://127.0.0.1:8080/)
  5. Choose the Python interpreter that you set up in the above section from the Python interpreter dropdown
  6. Add your absolute path mappings (this is sometimes optional, depending on where your VagrantFile is stored).
  7. Click OK to save.

运行您的项目,享受 Vagrant 的荣耀.

Run your project, and enjoy the glory that is Vagrant.

这篇关于使用 PyCharm Professional 和 Vagrant,我如何运行 Django 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆