Using PyCharm Professional and Vagrant, how do I run a Django server?(使用 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.
- 通过导航到
Tools->Vagrant->Up
从 PyCharm 启动 Vagrant 机器 - SSH 进入你的 Vagrant 盒子:
Tools->Start SSH Session
.从出现的列表中选择Vagrant at [VagrantFolder]
. - 在出现的终端中,运行
which python
.这将为您提供虚拟机上 python 的绝对路径. 文件->设置->项目->项目解释器
.点击 + 按钮创建一个新的.- 选择
Vagrant
.您的Vagrant 实例文件夹
应该是 VagrantFile 在主机上的位置.Python 解释器路径
应设置为您在上面第 3 步中找到的绝对路径. - 点击确定保存.注意: Vagrant 必须
up
才能正常工作.
- Start your Vagrant machine from PyCharm by navigating to
Tools->Vagrant->Up
- SSH into your Vagrant box:
Tools->Start SSH Session
. SelectVagrant at [VagrantFolder]
from the list that appears. - From the terminal that appears, run
which python
. This will give you an absolute path to python on your virtual machine. File->Settings->Project->Project Interpreter
. Click the + button to create a new one.- Choose
Vagrant
. YourVagrant 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. - Click OK to save. Note: Vagrant has to be
up
in order for this to work.
配置您的项目以使用正确的解释器
- 从
Run
菜单中,选择Edit Configurations
- 点击+并添加一个新的
Django Server
- 将您的
Host
设置为0.0.0.0
.这会将runserver
命令绑定到外部 IP. - 检查
Run browser
并将 URL 设置为您在 VagrantFile 中映射到 VM 的主机/端口(例如,如果我将主机的端口8080
映射到 Vagrant 的8000
,我会使用http://127.0.0.1:8080/
) - 从
Python 解释器
下拉列表中选择您在上述部分中设置的Python 解释器
- 添加您的绝对
路径映射
(这有时是可选的,具体取决于 VagrantFile 的存储位置). - 点击确定保存.
- From the
Run
menu, selectEdit Configurations
- Click + and add a new
Django Server
- Set your
Host
to0.0.0.0
. This will bind therunserver
command to an external IP. - 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 port8080
to Vagrant's8000
, I'd usehttp://127.0.0.1:8080/
) - Choose the
Python interpreter
that you set up in the above section from thePython interpreter
dropdown - Add your absolute
path mappings
(this is sometimes optional, depending on where your VagrantFile is stored). - Click OK to save.
运行您的项目,享受 Vagrant 的荣耀.
Run your project, and enjoy the glory that is Vagrant.
这篇关于使用 PyCharm Professional 和 Vagrant,我如何运行 Django 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!