How to use python on glitch?

I know that glitch officially supports JavaScript but is there anyway to use Python on it? Can someone give me a step-by-step instruction please?

creating your python file and then changing package.json should work:

{
    "start": "python python-file.py"
}

How would I import discord.py?

Open console and write pip3 install discord.py

1 Like

Alright, thank you. I’ll try it

Hey @Infinity1231 I suggest you take a look at the projects in https://glitch.com/@python for some examples of how to work with Python on Glitch. One thing to keep in mind is that if you omit a package.json file and instead include a requirements.txt file, Glitch will automatically install the dependencies from requirements.txt and run the project using Python.

Hope this helps!

3 Likes

I was very busy these days, but now I have tried it but I don’t know how do I start the main file? Can you check my project @cori? It’s wobbly-coast

Hey @Infinity1231, with a python project, Glitch looks for a file called start.sh in your project’s home directory and runs that when it starts up. You can place code to start your app in that file and go from there. https://glitch.com/edit/#!/hello-flask?path=start.sh:1:0 might be a good place to look for a working example.

Hey @cori. Can you check why glitch isn’t running my bot? The code is correct, I think I there’s problem with importing modules. Project name: acoustic-burglar

Hey @Infinity1231 I’m not quite sure what you’re trying to do in that project, but based on the code you have it’s doing what I would expect.

Having a glitch.json file in your project puts Glitch into “custom app mode” and runs that install and start scripts you’ve defined. In that file you’re telling Glitch to install from requirements.txt and then to start server.py which in turn starts up a minimal Flask app that renders “Hello World!” when requested. All of that works exactly as I’d expect it to.

If you don’t have a glitch.json file but do have a requirrements.txt file then Glitch goes into “Python mode”, automagically installs from requirements.txt and then starts the app using what’s in start.sh. In there it looks like you might be trying to start a bot, but you’re asking Glitch to run a file called app.py using Python. This code won’t run as long as you have the glitch.json file in place, and if that weren’t there, Glitch wouldn’t be able to find the non-existant app.py file.

Can you help me understand how you’d like this to work?

I want Gltich to start quote.py and just install/import all the modules.
Edit: In start.sh, I changed the app.py to quote.py
Edit 2: Changed server.py to quote.py in glitch.json but all I see in the logs is this:


Edit 3: Added python3 in requirements.txt and I see this:

Edit 4: Running python quote.py in console gives this:
image
I don’t think that’s a syntax error since def is used for a function in python

Hey @Infinity1231 I’m pretty sure the default version for the python command in the Glitch containers is 2.7, and async calls weren’t introduced until Python 3.3 or so, so if you want to use asynchronous calls you’ll need to use python3 instead of python.

So do I add python3 in the requirements.txt or use python3 quote.py?

You can remove the python3 from your requirements.txt and change python in start.sh to python3 and that should suffice to get you past those errors.

Ok I made those changes. So now running python3 quote.py in console does not give anything?
image
The bot still is offline.

Please help me @cori

Hey @Infinity1231 this took me a little bit of digging; sorry for the delay!

I think the version of discord.py that you’re referencing (version 1.0.0 per /app/discord/__init__.py) isn’t compatible with the version of Python3 Glitch is currently running - we’re on 3.5.2 and discord.py@1.0.0 requires 3.5.3.

We have some work ahead of us to update the app containers and installing a newer version of Python3 is in that list, so this may be worth trying again in the not-too-distant future, although I don’t have a specific timeline right now. You could also try downgrading to v0.16.12 of Discord.py, which doesn’t specifically mention requiring 3.5.3.

Sorry for the inconvenience!

Alright please keep me updated as you update the python version!