Where to put the py file I created?

Hello,
I create a short fibo.py file use text editor but where I should put this py file in my local PC, then I can use ‘import fibo.py’ to use this file?
thanks!

Hi,

as a disclaimer, I use IDLE editor. So, when I first open the IDLE shell prompt editor, my editor will point to the default …\Programs\Python\Python312 directory.

However, if you want to change your IDLE working directory, to a folder where you keep Python test scripts that you would like to import at the ready, you can use the following commands:

   import os
   os.chdir(r'C:\folder_1\folder_2\folder_3\folder_n')  # arbitrary folder/directory location

# To verify current working directory:
os.getcwd()

Here, I am assuming that your Python file fibo.py is saved in the folder_n folder. Now that Python is pointing to this folder, you can import your file.

import fibo

Anywhere you want, its your PC.

Thanks Paul! but when I change the directory and put the file in the folder_1, the import still failed.

You forgot the command:

os.chdir(r'C:\folder_1\folder_2\folder_3\folder_n') 

where C:\folder_1\folder_2\folder_3\folder_n = is the actual folder location on your PC.

Don’t copy and paste this location. You have to enter YOUR folder location, where ever the file fibo.py is located on your PC.

thanks. I tried again and still failed as the picture shows. whether my fibo.py file is named correctly?

That is interesting. It should be working.

For now, as a test, save the file to your desktop. Reuse the commands as before but change
the file location to your desktop folder. By the way, don’t use the Python312 folder as a location for saving your test scripts. You might want to delete them down the road and might accidentally delete one from your Python package by mistake.

thanks Paul! so it seems some prolem with my py file. I re-create it with notepad++ and now it’s ok to import. appreciate your time and help Paul!

1 Like

It may help to read or re-read: How does Python find modules?

2 Likes

thanks for the information!