23

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.

Community
  • 1
  • 1
Miebster
  • 2,365
  • 4
  • 21
  • 27
  • 1
    PTVS 1.5 RC has a new option: "New Project from Existing Code" – Markus Jarderot Oct 01 '12 at 22:21
  • Are you saying to remake the entire project? (which also means re-defining the search path) – Miebster Oct 02 '12 at 15:12
  • If the 'Show all files' button isn't on Solution Explorer, is there an item 'Show all files' in the Project menu? – Luke Woodward Jan 01 '13 at 17:14
  • I no longer have Visual Studio installed so I can't say with any certainty. But my hunch is no, if the answer is so simple a google search should turn it up. It looks like the ptvs plugin didn't implement the "show all files" feature at all. – Miebster Jan 02 '13 at 16:40
  • 1
    Hi, I tried this in VS2012 and 2012 Ultimate and works fine, then tried in express versions and does not work, perhaps it is a limitation of the specific version? – GMasucci Jan 08 '13 at 08:53
  • "Show all files" is, indeed, not supported in current PTVS release. But see this: http://pytools.codeplex.com/workitem/112 – Pavel Minaev May 11 '13 at 21:11
  • 1
    And http://pytools.codeplex.com/workitem/112 has been resolved, which means you'll get Show All Files in the current release. – Zooba Jul 16 '13 at 15:40
  • "New Project from Existing Code" worked for me. – Leo Jul 21 '15 at 10:12

3 Answers3

6

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>'
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
3

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

Project->Show All Files

2

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) enter image description here

  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. enter image description here

  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. enter image description here

user28864
  • 3,375
  • 1
  • 25
  • 19