简体   繁体   中英

VirtualEnv python imports not working

I've recently been tearing my hear out over this trivial problem, nothing I found online has helped me so far. I am using virtualenv, my project structure :

myproj\
..bin
..tasks.py
..celery_app.py
..projapi

where importFolder is an API folder with a bunch of files in it. proj.py is my script that does all the work and contains imports like

sys.path.append("/abspath/to/projapi")
import projapi

I can source bin/activate and enter my virtualenv just fine, but when I go back a directory and run the celery worker:

 celery -A myproj worker --app=myproj.celery_app:app -l info

I'll get an import error for every folder in the myproj directory such as importerror for 'projapi'. It's as if the virtualenv is looking for all files in only the /bin dir (where as my proj is in myproj dir)

How can I fix this? I've tried appending all sorts of system and python paths, both in activate and in my tasks.py, but to no avail.

Probably you are using system wide celery. Try to install it with pip within virtual env and repeat execution, that would fix your issue.

Also verify that each folder where you .py located contains __init__.py file.

您是否尝试过更改celery电话本身?

celery -A celery_app worker -E -l INFO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM