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

Question:

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

Asked By: LostMohican

||

Answers:

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

Answered By: yedpodtrzitko

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.

Answered By: CrazyCoder

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.

Answered By: dmitry_romanov

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.

Answered By: user7772648

I’m as surprised as many that there is no ‘import file’ or ‘add file’ option in Pycharm. I ended up just opening the file in the project, right clicking in the Project File pane on a file and opened that file in explorer to confirm the path to the project and then just used ‘Save As’ on the file I wanted to add and saved in the project folder.

Answered By: Gman Cornflake
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.