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

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

问题描述

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

  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解释器




  1. 通过导航到 Tools-> Vagrant->从PyCharm启动您的Vagrant机器

  2. SSH到您的Vagrant框中:工具 - >启动SSH会话。从出现的列表中选择 Vagrant [VagrantFolder]

  3. 从出现的终端中,运行。这将给你一个绝对的虚拟机上的python路径。

  4. File-> Settings-> Project-> Project Interpreter 。点击 + 按钮创建一个新的。

  5. 选择 Vagrant 。您的 Vagrant实例文件夹应该是您的主机上的VagrantFile的位置。应该将Python解释器路径设置为您在上述步骤3中找到的绝对路径。

  6. 点击确定保存。 注意: Vagrant必须是 up 才能使其正常工作。



将您的项目配置为使用正确的口译员




  1. 运行菜单,选择编辑配置

  2. 点击 + 添加一个新的 Django服务器

  3. 将您的主机设置为 0.0.0.0 。这将绑定 runserver 命令到外部IP。

  4. 检查运行浏览器并将URL设置为您在VagrantFile中映射到VM的主机/端口(例如,如果我将主机的端口 8080 映射到Vagrant的 8000 ,我会使用 http://127.0.0.1:8080/

  5. 选择 Python解释器您在上述部分中从 Python解释器下拉列表

  6. 添加您的绝对路径映射(这有时是可选的,具体取决于您的VagrantFile的存储位置。)

  7. 点击确定保存。

运行您的项目,享受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

  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.

Configure Your Project to Use the Correct Interpreter

  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.

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

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

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