Ask coding questions

← Back to all posts
Can files be independent from each other?
michaeldelanuez (0)

I am learning Python via the book Python Crash Course 2nd Edition.

I am trying to organize the files on the left side as I go through the exercises per each chapter.

For example on the left side I created a folder "Chapter4". Then under that folder I created a file "counting_to_twenty.py"

After trying to run the code nothing happens. It seems as if these folders and files I'm creating is somehow dependent on main.py.

Is there a way I can make all files and folders independent from one another? That way for example if I delete main.py, it wont give me a "main.py error" when I try to run some code exercises.

Comments
hotnewtop
InvisibleOne (3214)

You can either make a .replit file, or just in your main import the file and run it. The current bug I see in your file is that you are trying to import folders as classes, that only works if you have an empty file called __init__.py
Then you should be able to do: from Chapter4 import counting_to_twenty

Kai_Justice (271)

Change line 1 of main.py to from Chapter4 import counting_to_twenty
When a file is imported it'll run everything inside it. I also suggest just making a new repl for every chapter, but that's just my personal preference.

For independant files create a .replit file and paste this in it run = "python3 (name of file you want to run)", it's a bash command that'll run everytime you click the run button.

VulcanWM (2898)

If not then do something that I normally do which is import the file in main.py, so ‘from Chapter4.counting_to_twenty.py import function’
And then run whatever function you imported from the counting to twenty file in main.py

michaeldelanuez (0)

@VulcanWM looks like the ‘from Chapter4.counting_to_twenty.py import function’ method worked but then there was a traceback

Coder100 (18903)

replit is different from your book.
It only runs main.py, and that clearly is not good.

So, what you would want to do is make a .replit file (or hack the main.py file).

1. changing main.py

To change main.py, all you have to do is import the file and python will run it!

import Chapter4.counting_to_twenty

Notice how the folder and file are separated by the . and there is no extension needed.

2. .replit files

This is the most reliable and sane way imo.
Create a .replit file (exactly that), and add this:

run = "python Chapter4/counting_to_twenty.py"

now to change the file being run, just change the python file!

VulcanWM (2898)

You can try making a bash repl, adding whatever Python file you want, and then in the .replit try doing run “filename”

FishballNooodle (211)

@VulcanWM
Yes this will work @michaeldelanuez
Create a file name .replit to your existing Python Real
And add this to the file

language = "python3"
run = "python3 filetorunname.py"
LukeWright (288)

you don't need the language = python3 line because in the run command you are specifying the language by saying python3 filenametorunname.py @FishballNooodle