How can I add an existing folder structure to my visual studio project (python tools)?

青春壹個敷衍的年華 提交于 2019-12-10 00:42:56

问题


This question is essentially the same as these:

How do I add an existing directory tree to a project in Visual Studio? How to "Add Existing Item" an entire directory structure in Visual Studio?

Except the solutions don't work for me.

It looks like another user had the exact same problem

http://pytools.codeplex.com/discussions/249455

But http://xkcd.com/979/ has struck again.

I am using Visual Studio 2010 with Python Tools for Visual Studio.

In this project users create new folders and code and commit them to SVN. Another user will update SVN and the new files and folders will show up in windows explorer. The user then needs an easy way to add these folders and files to the solution.

Putting the solution in SVN so added folders can be added to the solution before committing is not an option. The solution is controlled in a separate area from the source.

Suggested Solution:

https://stackoverflow.com/a/392477/606660

Won't work because:

The "Show all files" button in the solution explorer does not show up.

Suggested Solution:

https://stackoverflow.com/a/57778/606660

Won't work because:

When I drag the folder onto the solution explorer pane the location the folder is dropped affects where in the solution it is nested. If it gets dropped in the wrong folder, it shows up as a folder with the expected name and the expected contents. This is really close to what we want, except its in the wrong folder (since I dropped it on the wrong folder, intentionally). If the folder is dragged to the correct location, it appears as a file with an exclamation point. When you double click the "file" it says

"The item <folder name> does not exist in the project directory.  It may have been moved, renamed, or deleted"

I believe this is because VS will try to create a copy of the folder in the directory that you drag it to. If I move the folder out of my project entirely (say to the desktop) and then drag it into the solution explorer at the proper location, it shows up as a folder in the project. A copy of the folder is also created on disk at the location specified by the drop, with the same name and contents.

So it appears that dragging and dropping a folder onto the solution explorer will create a copy of the folder on the disk in the location that you targeted in your solution when you dropped it. If that location already has a folder of that name, the folder gets imported as a file.

My Solution

I'm using PyCharm instead, its much better.


回答1:


If nothing else works, you could add the files and folders manually in the .pyproj-file. The format is simple:

<ItemGroup>
    <Compile Include="File1.py" /> <!-- List of files relative to .pyproj folder -->
    <Compile Include="test\file2.py" />
</ItemGroup>
<ItemGroup>
    <Folder Include="test\" /> <!-- List of folders -->
</ItemGroup>

You can add more <ItemGroup> elements if you want, and you can mix files and folder if you wish.

Script to generate the XML:

import os

def visit(folder):
    for fn in os.listdir(folder):
        filename = os.path.join(folder, fn)
        _, ext = os.path.splitext(fn)
        if os.isdir(filename):
            folders.append(filename)
            visit(filename)
        elif ext.lower() == '.py':
            files.append(filename)

files = []
folders = []

visit('.')

print '<ItemGroup>'
for fn in files:
    print '  <Compile Include="' + fn + '"/>'
print '</ItemGroup>'

if folders:
    print '<ItemGroup>'
    for fn in folders:
        print '  <Folder Include="' + fn + '\\"/>'
    print '</ItemGroup>'



回答2:


Show All files works. Show All files is in project menu.

Project->Show All Files




回答3:


This worked for me in visual studio 2013, I don't know if this is applicable for other visual studio iterations.

After installing Python Tools for Visual Studio (PTVS), which I found here

  1. Open visual studio, navigate to FILE->NEW->PROJECT (or hold Cntrl+shift+N)

  2. On the new project dialog, Browse to your existing python project directory. Enter the name of your project in the project name field. uncheck create a directory for solution (if you do not wish to create a new project directory). And Select "From Existing Python code" option and click OK.

  3. On the "create a new Project from Existing Python Code" Wizard, follow the prompts and make all necessary setting as needed or just click finish.



来源:https://stackoverflow.com/questions/12681852/how-can-i-add-an-existing-folder-structure-to-my-visual-studio-project-python-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!