How do I import/add an existing Python file to a PyCharm project?

How do I import/add an existing Python file to a PyCharm project?


Solution 1:

Copy the files to some directory under the project root using your favorite file manager or add the directory containing your files to the project using Settings (Preferences on Mac) | Project Structure | Add Content Root.

Solution 2:

I'm not sure if I get what you want, but there's way you can add existing source into project: File -> Settings -> Project structure -> Add Content root -> choose folder with existing code

Solution 3:

For PyCharm 2017 (v.2017.2.3), it's slightly different. You still need to copy the files you want to add to a directory under the project's own root directory, but they don't appear in the Project navigation window in PyCharm immediately. To correct this, click on the Project's name in the Navigation window to collapse the project's tree structure, then open it up again. You should now be able to see the files you've just added.

Solution 4:

I hacked this way - copy files into another folder, create new files with the same names using IDE, and replace the new files with the copied versions. It does the trick.

P.S. I grepped .idea folder and found no project file like CMakeLists.txt to edit manually. There are numerous entries in the .idea/workspace.xml file, but I am not sure how to generate proper entries by hand.