简体   繁体   English

设置Spyder工作区和项目的基础知识

[英]Basics of setting up a Spyder workspace and projects

I have searched for a basic tutorial regarding workspaces and projects in the Spyder IDE. 我在Spyder IDE中搜索了有关工作空间和项目的基本教程。 What I want to understand is the basic concepts of how to use the workspace and projects to organize my code. 我想要了解的是如何使用工作区和项目来组织我的代码的基本概念。 It seems that this is perhaps basic programming skills and that is the reason why I have issues finding any kind of overview. 看起来这可能是基本的编程技巧,这也是我找到任何概述的问题的原因。 This page seems to be related, but is actually about Eclipse and rather sparse. 这个页面似乎是相关的,但实际上是关于Eclipse而且非常稀疏。 The Pythonxy tutorial and the documentation for Spyder does not go into any detail. Python的教程和Spyder文档没有详细说明。 Neither does the Anaconda documentation. Anaconda文档也没有。

The questions I have are: 我的问题是:

When should I set up a new workspace (if ever)? 我什么时候应该设置一个新工作区(如果有的话)?

When do I create a new project? 我什么时候创建一个新项目?

How does the PYTHONPATH depend on my workspace and project settings? PYTHONPATH如何依赖于我的工作区和项目设置? Is it the same in all cases or can I customize it per workspace/project? 它在所有情况下都是相同的,还是可以按工作区/项目自定义?

Are there other settings apart from the PYTHONPATH that I should configure? 除了我应该配置的PYTHONPATH之外还有其他设置吗?

How specific are the answers above to Spyder? Spyder上面的答案有多具体? Would it be the same for other IDEs, like Eclipse? 对于其他IDE,如Eclipse,它会是一样的吗?

I am running Spyder on 64-bit Windows 7, as part of the Anaconda package. 我在64位Windows 7上运行Spyder,作为Anaconda软件包的一部分。

In my experience, setting up a workspace in Spyder is not always necessary. 根据我的经验,在Spyder中设置工作区并不总是必要的。 A workspace is a space on your computer where you create and save all the files you work in. Workspaces usually help in managing your project files. 工作区是计算机上用于创建和保存所有文件的空间。工作区通常有助于管理项目文件。 Once you create a workspace in Spyder, a pane called "Project Explorer" opens up inside Spyder. 在Spyder中创建工作区后,Spyder中会出现一个名为“Project Explorer”的窗格。 There you see in real-time the files of your project. 在那里,您可以实时查看项目的文件。 For instance, if you generate a file with Python, it will show in that pane. 例如,如果使用Python生成文件,它将显示在该窗格中。 The pane let's you keep the files organized, filter them etc. This can be useful for web development for example because helps you keep your content organized. 该窗格允许您保存文件的组织,过滤它们等。这对于Web开发非常有用,例如,因为它可以帮助您保持组织内容的有序性。 I use Python to handle files (eg csv) and work with data (data analysis), and I find no use in the workspace feature. 我使用Python来处理文件(例如csv)并使用数据(数据分析),我发现在工作区功能中没有用处。 Moreover, if you delete a file in the Project Explorer pane, the file cannot be found in the Windows recycle bin. 此外,如果在“项目浏览器”窗格中删除文件,则无法在Windows回收站中找到该文件。

Update Oct 2016: Spyder 3 now has project facilities similar to that of other IDEs (especially Rstudio). 2016年10月更新: Spyder 3现在具有与其他IDE(尤其是Rstudio)类似的项目设施。

Now you if you have a folder with scripts, you can go to 现在,如果你有一个带脚本的文件夹,你可以去

Projects > New Projects > Existing Directory

to import it. 导入它。 The selected directory will be set as the base directory for the project. 所选目录将设置为项目的基本目录。

I use spyder for data analysis and I have just started using the project workspace. 我使用spyder进行数据分析,我刚刚开始使用项目工作区。 I believe that it allows you to write better code due to the organization. 我相信它允许您根据组织编写更好的代码。 As a previous post stated that "This can be helpful in web development", which is true because web development requires good software engineering due to the complexity of the files and how they interact with each other. 正如之前的帖子所说“这对Web开发有帮助”,这是正确的,因为Web开发需要良好的软件工程,因为文件的复杂性以及它们如何相互交互。 This organization/structure can be used in data analysis as well. 该组织/结构也可用于数据分析。

Often, data analysts that use Anaconda have an engineering or science background, not necessarily software engineering or computer science. 通常,使用Anaconda的数据分析师具有工程或科学背景,不一定是软件工程或计算机科学。 This means that good software engineering principles may be missing (myself included). 这意味着可能缺少良好的软件工程原则(包括我自己)。 Setting up a workspace does one critical thing that I believe is missing from the discussion. 设置工作区确实是我认为在讨论中遗漏的一件重要事情。 It adds the workspace to the system path. 它将工作空间添加到系统路径。 Set up a project and then try 设置一个项目,然后尝试

import sys
print sys.path

You will see your project's directory added to the PYTHONPATH . 您将看到项目的目录已添加到PYTHONPATH。 This means I can break up my project and import functions from different files within my project. 这意味着我可以分解我的项目并从我的项目中的不同文件导入函数。 This is highly beneficial when analysis becomes complex or you want to create some type of larger model that will be used on a regular basis. 当分析变得复杂或者您想要创建将定期使用的某种类型的较大模型时,这非常有用。 I can create all of my functions in one file, maybe functions for plots in another and then import them in a separate script file. 我可以在一个文件中创建所有函数,也可以在另一个文件中创建函数,然后将它们导入到单独的脚本文件中。

in myScript.py 在myScript.py中

from myFunctions import func1
from myFunctions import func2
from myPlots import histPlot

This is a much cleaner approach to data analysis and allows you to focus on one specific task at a time. 这是一种更清晰的数据分析方法,允许您一次关注一个特定的任务。

In python 3 there is the %autoreload capability so you can work on your functions and then go back to your script file and it will reload them each time if you find errors. 在python 3中有%autoreload功能,因此您可以处理您的功能,然后返回到您的脚本文件,如果您发现错误,它将每次重新加载它们。 I haven't tried this yet bc the majority of my work is in 2.7, but this would seem to add even greater flexibility when developing. 我还没有尝试过这个,但我的大部分工作都在2.7,但这似乎在开发时增加了更大的灵活性。

So when should you do this? 所以你应该什么时候这样做? I think it is always a good idea, I just started using this setup and I will never go back! 我认为这总是一个好主意,我刚开始使用这个设置,我永远不会回去!

One critical piece of information that appears to be missing from the Spyder documentation is how to create a new workspace in the first place. Spyder文档中似乎缺少的一条重要信息是如何创建一个新的工作区。 When no workspace exists after installing Spyder, creating your first project automatically initiates the creation of a workspace (at least in the Anaconda 3 distribution). 安装Spyder后没有工作空间时,创建第一个项目会自动启动工作空间的创建(至少在Anaconda 3发行版中)。 However, it is not as obvious how to create a new workspace when a workspace already exists. 但是,当工作空间已存在时,如何创建新工作空间并不明显。

This is the only method I have found for creating a new workspace: 这是我找到的创建新工作区的唯一方法:

(1) Select the Project explorer window in Spyder. (1)在Spyder中选择Project explorer窗口。 If this window or tab doesn't appear anywhere in the Spyder application, use View > Panes > Project explorer to enable the window. 如果此窗口或选项卡未出现在Spyder应用程序中的任何位置,请使用“ 视图”>“窗格”>“项目浏览器”启用该窗口。

(2) Click on the folder icon in the upper-right corner of the Project explorer window. (2)单击Project explorer窗口右上角的文件夹图标。 This icon brings up a dialog that can create a new workspace. 此图标将显示一个可以创建新工作区的对话框。 The dialog allows selection of a directory for the .spyderworkspace file. 该对话框允许选择.spyderworkspace文件的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM