使用PyCharm Professional和Vagrant,如何运行Django服务器? [英] Using PyCharm Professional and Vagrant, how do I run a Django server?
问题描述
ssh:// vagrant @ localhost:2222 / usr / bin / python -uC:/Users/MyName/ProjectName/config/manage.pyrunserver localhost:8080
当我运行这个时,它失败了(显然),因为它使用Windows路径来管理。 py
具体来说我得到的错误是
`/ usr / bin / python:无法打开文件'C :/Users/MyName/judgeapps/config/manage.py':[Errno 2]没有这样的文件或目录
在广泛的谷歌搜索之后,我无法想象的是如何强制django在我的流氓机器上使用路径。如何做这个?
诀窍是在PyCharm中创建一个Python解释器,并将项目配置为使用解释器。
注意:以下内容适用于PyCharm Professional 4.0。
创建一个适合Vagrant的Python解释器
- 通过导航到
Tools-> Vagrant->从PyCharm启动您的Vagrant机器
- SSH到您的Vagrant框中:
工具 - >启动SSH会话
。从出现的列表中选择Vagrant [VagrantFolder]
。 - 从出现的终端中,运行
蟒
。这将给你一个绝对的虚拟机上的python路径。 -
File-> Settings-> Project-> Project Interpreter
。点击 + 按钮创建一个新的。 - 选择
Vagrant
。您的Vagrant实例文件夹
应该是您的主机上的VagrantFile的位置。应该将Python解释器路径设置为您在上述步骤3中找到的绝对路径。 - 点击确定保存。 注意: Vagrant必须是
up
才能使其正常工作。
将您的项目配置为使用正确的口译员
- 从
运行
菜单,选择编辑配置
- 点击 + 添加一个新的
Django服务器
- 将您的
主机
设置为0.0.0.0
。这将绑定runserver
命令到外部IP。 - 检查
运行浏览器
并将URL设置为您在VagrantFile中映射到VM的主机/端口(例如,如果我将主机的端口8080
映射到Vagrant的8000
,我会使用http://127.0.0.1:8080/
) - 选择
Python解释器
您在上述部分中从Python解释器
下拉列表 - 添加您的绝对
路径映射
(这有时是可选的,具体取决于您的VagrantFile的存储位置。) - 点击确定保存。
运行您的项目,享受Vagrant的荣耀。
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).
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
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?
The trick is creating a Python interpreter in PyCharm, and configuring the project to use this interpreter.
Note: The following applies to PyCharm Professional 4.0.
Create a Python Interpreter for Vagrant
- 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.
Configure Your Project to Use the Correct Interpreter
- 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.
Run your project, and enjoy the glory that is Vagrant.
这篇关于使用PyCharm Professional和Vagrant,如何运行Django服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!