Python 设置 Spyder 工作区和项目的基础知识
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28449359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basics of setting up a Spyder workspace and projects
提问by Fredrik
I have searched for a basic tutorial regarding workspaces and projects in the 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 pageseems to be related, but is actually about Eclipse and rather sparse. The Pythonxy tutorial and the documentation for Spyderdoes not go into any detail. Neither does the Anaconda documentation.
我在 Spyder IDE 中搜索了有关工作区和项目的基本教程。我想了解的是如何使用工作区和项目来组织我的代码的基本概念。似乎这可能是基本的编程技能,这就是我在寻找任何类型的概述时遇到问题的原因。这个页面似乎是相关的,但实际上是关于 Eclipse 的,而且相当稀疏。Pythonxy 教程和Spyder 文档没有详细介绍。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? Is it the same in all cases or can I customize it per workspace/project?
PYTHONPATH 如何取决于我的工作区和项目设置?它在所有情况下都相同还是我可以根据工作区/项目对其进行自定义?
Are there other settings apart from the PYTHONPATH that I should configure?
除了我应该配置的 PYTHONPATH 之外,还有其他设置吗?
How specific are the answers above to Spyder? Would it be the same for other IDEs, like Eclipse?
上面对 Spyder 的回答有多具体?其他 IDE(例如 Eclipse)是否也一样?
I am running Spyder on 64-bit Windows 7, as part of the Anaconda package.
我在 64 位 Windows 7 上运行 Spyder,作为 Anaconda 包的一部分。
采纳答案by Jeff Tilton
I use spyder for data analysis and I have just started using the project workspace. 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. This organization/structure can be used in data analysis as well.
我使用 spyder 进行数据分析,我刚刚开始使用项目工作区。我相信由于组织的原因,它可以让您编写更好的代码。正如之前的一篇文章所说“这对 Web 开发很有帮助”,这是真的,因为 Web 开发需要良好的软件工程,因为文件的复杂性以及它们之间的交互方式。这种组织/结构也可用于数据分析。
Often, data analysts that use Anaconda have an engineering or science background, not necessarily software engineering or computer science. 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
通常,使用 Anaconda 的数据分析师具有工程或科学背景,不一定是软件工程或计算机科学。这意味着可能缺少良好的软件工程原则(包括我自己)。设置工作区会做一件我认为讨论中遗漏的关键事情。它将工作区添加到系统路径。建立一个项目,然后尝试
import sys
print sys.path
You will see your project's directory added to the 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.
您将看到您的项目目录添加到 PYTHONPATH 。这意味着我可以分解我的项目并从我的项目中的不同文件导入函数。当分析变得复杂或您想要创建某种类型的将定期使用的较大模型时,这非常有用。我可以在一个文件中创建我的所有函数,也可以在另一个文件中创建绘图函数,然后将它们导入到一个单独的脚本文件中。
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 %autoreloadcapability 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. 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.
在 python 3 中有%autoreload功能,所以你可以处理你的函数,然后回到你的脚本文件,如果你发现错误,它每次都会重新加载它们。我还没有尝试过,因为我的大部分工作都在 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!
那么你应该什么时候这样做呢?我认为这总是一个好主意,我刚开始使用这个设置,我永远不会回去!
回答by multigoodverse
In my experience, setting up a workspace in Spyder is not always necessary. 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. 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. 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. I use Python to handle files (e.g. csv) and work with data (data analysis), and I find no use in the workspace feature. Moreover, if you delete a file in the Project Explorer pane, the file cannot be found in the Windows recycle bin.
根据我的经验,在 Spyder 中设置工作区并不总是必要的。工作区是计算机上的一个空间,您可以在其中创建和保存您在其中工作的所有文件。工作区通常有助于管理您的项目文件。在 Spyder 中创建工作区后,会在 Spyder 中打开一个名为“项目资源管理器”的窗格。在那里您可以实时查看项目文件。例如,如果您使用 Python 生成一个文件,它将显示在该窗格中。该窗格可让您组织文件、过滤它们等。例如,这对 Web 开发很有用,因为它可以帮助您保持内容组织。我使用 Python 来处理文件(例如 csv)和处理数据(数据分析),我发现工作区功能没有用。此外,如果您在 Project Explorer 窗格中删除文件,
回答by Fred Schleifer
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. 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). However, it is not as obvious how to create a new workspace when a workspace already exists.
Spyder 文档中似乎缺少的一个关键信息是首先如何创建新工作区。当安装 Spyder 后不存在工作区时,创建您的第一个项目会自动启动工作区的创建(至少在 Anaconda 3 发行版中)。但是,当工作区已经存在时,如何创建新工作区并不那么明显。
This is the only method I have found for creating a new workspace:
这是我找到的用于创建新工作区的唯一方法:
(1) Select the Project explorerwindow in Spyder. If this window or tab doesn't appear anywhere in the Spyder application, use View > Panes > Project explorerto enable the window.
(1)在 Spyder 中选择Project explorer窗口。如果此窗口或选项卡未出现在 Spyder 应用程序的任何位置,请使用“视图”>“窗格”>“项目资源管理器”来启用该窗口。
(2) Click on the folder icon in the upper-right corner of the Project explorerwindow. This icon brings up a dialog that can create a new workspace. The dialog allows selection of a directory for the .spyderworkspace
file.
(2) 单击项目浏览器窗口右上角的文件夹图标。这个图标会弹出一个对话框,可以创建一个新的工作区。该对话框允许为.spyderworkspace
文件选择一个目录。
回答by Heisenberg
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.
导入它。所选目录将被设置为项目的基本目录。