R
Railway2mo ago
DylZ

Magic python lib isnt working.

I will attach the full build logs below
Solution:
nixpacks is being difficult, we are moving to a Dockerfile, add this as a Dockerfile to your project - ```dockerfile FROM python:3.12 ENV PYTHONUNBUFFERED=1...
Jump to solution
51 Replies
Percy
Percy2mo ago
Project ID: 1b8eef65-7914-42b8-81ff-e5902b594bd8
DylZ
DylZ2mo ago
1b8eef65-7914-42b8-81ff-e5902b594bd8
DylZ
DylZ2mo ago
It does work on my local pc just not when deploying I am also running the same python version on both railway and my pc
Brody
Brody2mo ago
what version of python do you use locally?
DylZ
DylZ2mo ago
Im using 3.12.0
Brody
Brody2mo ago
nixpacks doesnt support python 3.12 yet, so instead add 3.11 to a runtime.txt file
DylZ
DylZ2mo ago
Ohhh, good to know I just pushed with 3.11 now I still get the same error
Brody
Brody2mo ago
python-magic-bin 0.4.14 if from 2017
DylZ
DylZ2mo ago
Is there a way to apt-get something? Ill try without making it 0.4.14
Brody
Brody2mo ago
yeah, but may i ask why? i would highly recommend using something pure python thats more up to date
DylZ
DylZ2mo ago
On the python-magic github it says on Debian/Ubuntu to use sudo apt-get install libmagic1, i just didnt know if i could apt-get something on railway so i was trying to do it without using it I couldtn find anything because i had tried but there just wasnt anything up to date I am using something from 7 years ago right now and it is just too outdated so I was hoping to get something a little newer
Brody
Brody2mo ago
the -bin version would include libmagic1 im now thinking python-magic-bin is not compatible with python 3.8+
DylZ
DylZ2mo ago
I dont know if i even have a python version that old on my local pc And it does work on my local pc I will check again to make sure Yeha it works on my pc using python 3.12
Brody
Brody2mo ago
its already installed, so of course it will work lol
DylZ
DylZ2mo ago
Oh wait im tired lmao It does install on my localpc
Brody
Brody2mo ago
its already installed
DylZ
DylZ2mo ago
I just tried uninstalling and installing agian with --no-cache-dir and it did work
Brody
Brody2mo ago
what os are you using?
DylZ
DylZ2mo ago
Windows 10 so its obv a lot different then ubuntu but idk
Brody
Brody2mo ago
might not support ubuntu
DylZ
DylZ2mo ago
Im guessing not but i did read that python-magic can be installed with apt-get install on some random website so would i be able to test that? or is that not doable? Oh it seems like its for python 2.7 though so nevermind Im not very good with linux so I dont know
Brody
Brody2mo ago
use this https://pypi.org/project/python-magic/ and then place this nixpacks.toml file in your project -
[phases.setup]
aptPkgs = ['...', 'libmagic1']
[phases.setup]
aptPkgs = ['...', 'libmagic1']
DylZ
DylZ2mo ago
Okay let me give that a try Im getting an Import error now,
File "/app/Routes/fileUploads.py", line 10, in <module>

import magic

File "/opt/venv/lib/python3.11/site-packages/magic/__init__.py", line 209, in <module>

libmagic = loader.load_lib()

^^^^^^^^^^^^^^^^^

File "/opt/venv/lib/python3.11/site-packages/magic/loader.py", line 49, in load_lib

raise ImportError('failed to find libmagic. Check your installation')

ImportError: failed to find libmagic. Check your installation
File "/app/Routes/fileUploads.py", line 10, in <module>

import magic

File "/opt/venv/lib/python3.11/site-packages/magic/__init__.py", line 209, in <module>

libmagic = loader.load_lib()

^^^^^^^^^^^^^^^^^

File "/opt/venv/lib/python3.11/site-packages/magic/loader.py", line 49, in load_lib

raise ImportError('failed to find libmagic. Check your installation')

ImportError: failed to find libmagic. Check your installation
Brody
Brody2mo ago
build logs please
Brody
Brody2mo ago
is this a web app of some kind?
DylZ
DylZ2mo ago
Yeah Im using the magic lib for a file uplaoder
Brody
Brody2mo ago
flask? fastapi?
DylZ
DylZ2mo ago
fastapi
Brody
Brody2mo ago
why arent you using gunicorn?
DylZ
DylZ2mo ago
I wish i had an answer lol pretty much got lazy and im jsut debugging atm I will swap to it though Is gunicorn like way better than uvicorn? I havent looked into that much at all to be honest
Brody
Brody2mo ago
haha im tired too, i meant uvicorn
DylZ
DylZ2mo ago
ohh I just do it in my python code Because of my debugging on my pc it was jsut quicker to run it lol so pretty much just because im lazy
Brody
Brody2mo ago
the fastapi constructor is named app right?
DylZ
DylZ2mo ago
Yeah
DylZ
DylZ2mo ago
No description
DylZ
DylZ2mo ago
Heres how i start it Would it be better to start it using the start command, or is this just fine?
Solution
Brody
Brody2mo ago
nixpacks is being difficult, we are moving to a Dockerfile, add this as a Dockerfile to your project -
FROM python:3.12

ENV PYTHONUNBUFFERED=1
ENV PIP_DISABLE_PIP_VERSION_CHECK=1

RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libmagic1

WORKDIR /app

COPY requirements.txt ./

RUN pip install -r requirements.txt

COPY . ./

CMD uvicorn main:app --host 0.0.0.0 --port $PORT
FROM python:3.12

ENV PYTHONUNBUFFERED=1
ENV PIP_DISABLE_PIP_VERSION_CHECK=1

RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libmagic1

WORKDIR /app

COPY requirements.txt ./

RUN pip install -r requirements.txt

COPY . ./

CMD uvicorn main:app --host 0.0.0.0 --port $PORT
(you can delete the nixpacks.toml file)
DylZ
DylZ2mo ago
Okay, ive never toucher dockerfiles so i dont knwo them well. What do i name the file? Just Dockerfile?
Brody
Brody2mo ago
correct
DylZ
DylZ2mo ago
alright
Brody
Brody2mo ago
as you can see, its just a set of commands that setup the project, super simple stuff
DylZ
DylZ2mo ago
Yeah they seem pretty helpful maybe ill look into them some more
Brody
Brody2mo ago
not as finicky as nixpacks
DylZ
DylZ2mo ago
It started Let me test it out but it should work The code ran so that seems like a good sign to me Thank you!! Ive been trying to figure that out for days now lol
Brody
Brody2mo ago
no problem!
DylZ
DylZ2mo ago
Have a good night!
Brody
Brody2mo ago
Dockerfiles are always the answer you too!!
DylZ
DylZ2mo ago
Will use them in the future