How do I import/add an existing Python file to a PyCharm project?
4 Answers
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.

- 389,263
- 172
- 990
- 904
-
3In PyCharm 2020.2, there is no `Settings` menu bar item. It looks like this can now be found under `File` | `Settings` | `Project:
` | `Project Structure` | **Add Content Root**. – Harry Nov 20 '20 at 07:27 -
1In PyCharm 2021.2, Linux, this setting no longer exists (if it ever did). There is no Project/Project Structure under settings. Things like this are such a frustrating experience with PyCharm that I end up going back to vim or Jupyter-lab for editing. I mean, wouldn't importing code files be one of the most basic usecases? There are tons of useless buttons for things like expanding and contracting the project view but not for adding in files. – Emir Nov 09 '21 at 23:24
-
Project structure does appear in Linux version of PyCharm https://i.imgur.com/T1uVr4f.png – gimmegimme Feb 08 '23 at 16:01
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

- 9,035
- 2
- 40
- 42
-
11What would happen if I wanted to just include one particular file in the project structure? – tumultous_rooster Dec 17 '13 at 00:20
-
@matt, I have not seen an IDE which does that. Please share if you known of a solution other than copying files or making soft-links or hard-links in os. – minghua Jun 09 '14 at 19:42
-
11Well, Xcode, for one, does that. This is a difficult to understand Pycharm's limitation. I have been struggling with this issue for too long – Jean-Denis Muys Aug 03 '15 at 17:05
-
3In visual studio you can also import files from anywhere. On build time all the files are copied to your build output folder. – Skulas Mar 22 '18 at 07:02
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.
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.

- 5,146
- 1
- 33
- 36