简体   繁体   English

如何在python shell中的virtualenv中导入软件包

[英]How to import packages in virtualenv in python shell

I'm trying to make a function to make periodical notifications to users , especially , ios mobile devices. 我正在尝试使用户(尤其是ios移动设备)定期收到通知的功能。

Specifically, I use 'Scheduled task' of pythonanywhere. 具体来说,我使用pythonanywhere的“计划任务”。 ( https://help.pythonanywhere.com/pages/ScheduledTasks ) https://help.pythonanywhere.com/pages/ScheduledTasks

This is my script to send notifications. 这是我发送通知的脚本。

#!/usr/local/bin/python3.4
import sys,os,django
sys.path.append("/home/lkm/Folder/project/")
sys.path.append("/home/lkm/Folder/project/app/myvenv/")
print(sys.path)
os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"
from push_notifications.models import APNSDevice, GCMDevice
device = APNSDevice.objects.all()
if device is None:
    print('No Device')
message = 'Home Fried Potatoes, Yo-nola Bar, Soup du Jour, More...'
device.send_message(message)

But at the line of 'from push_notifications.models import APNSDevice, GCMDevice' I'm getting an error : 但是在“从push_notifications.models导入APNSDevice,GCMDevice”这一行,我遇到了一个错误:

'ImportError: No module named 'push_notifications' 'ImportError:没有名为'push_notifications'的模块

I think it's because of not importing virtualenv because push_notifications package is inside of packages of virtualenv, in mycase 'myvenv' directory. 我认为这是因为未导入virtualenv,因为push_notifications软件包位于mycase'myvenv'目录中的virtualenv软件包中。

But even though I import 'myvenv' by 'ImportError: No module named 'push_notifications'. 但是,即使我通过“ ImportError”导入“ myvenv”:也没有名为“ push_notifications”的模块。

It makes the same error, do you have the solution for this? 它会产生相同的错误,您对此有解决方案吗?

UPDATE (First script , second error message) UPDATE(第一个脚本,第二个错误消息)

#!/home/lkm/folder/project/app/myvenv/bin/python
import sys,os,django
sys.path.append("/home/lkm/folder/project/application/myvenv/bin/../lib/python/site-packages")
print(sys.path)
os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"
from push_notifications.models import APNSDevice, GCMDevice
device = APNSDevice.objects.all()
if device is None:
    print('No Device')
message = 'Home Fried Potatoes, Yo-nola Bar, Soup du Jour, More...'
device.send_message(message)

['/home/lkm/folder/project/application', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages', '/home/lkm/folder/project/application/myvenv/bin/../lib/python/site-packages'] ['/ home / lkm / folder / project / application','/usr/lib/python3.4','/usr/lib/python3.4/plat-x86_64-linux-gnu','/ usr / lib / python3.4 / lib-dynload”,“ / usr / local / lib / python3.4 / dist-packages”,“ / usr / lib / python3 / dist-packages”,“ / home / lkm / folder / project / application” /myvenv/bin/../lib/python/site-packages']

Traceback (most recent call last): File "/home/lkm/folder/project/application/schedule.py", line 9, in from push_notifications.models import APNSDevice, GCMDevice ImportError: No module named 'push_notifications' 追溯(最近一次通话):文件“ /home/lkm/folder/project/application/schedule.py”,第9行,来自push_notifications.models import APNSDevice,GCMDevice ImportError:没有名为“ push_notifications”的模块

I would change the shebang to use the Python from your virtual environment. 我将更改shebang以从您的虚拟环境中使用Python。

#!/home/lkm/Folder/project/app/myvenv/bin/python

Then you shouldn't have to append the virtual env to the python path, and you can remove the following line. 然后,您不必将虚拟环境附加到python路径,您可以删除以下行。

sys.path.append("/home/lkm/Folder/project/app/myvenv/")

However, if you really want to manually add the virtual env directory to the Python path, then I think you want to include the site-packages directory instead: 但是,如果您确实要手动将虚拟env目录添加到Python路径,那么我认为您想改为包含site-packages目录:

sys.path.append("/home/lkm/Folder/project/app/myvenv/python3.4/site-packages")

How are you executing the file? 您如何执行文件? I see you have: 我看到你有:

#!/usr/local/bin/python3.4

which means that if you're executing the file with: 这意味着如果您使用以下命令执行文件:

./file.py

it will be executed with the system interpreter. 它将由系统解释器执行。

You need to activate the environment: 您需要激活环境:

$ source env/bin/activate

and execute the file with: 并使用以下命令执行文件:

$ python file.py

FWIW, I think the cleanest solution though is to have a setup.py script for your project ( packages= argument being the most important) and define an entry point , similar to: FWIW,我认为最干净的解决方案是为您的项目准备一个setup.py脚本packages=参数是最重要的)并定义一个入口点 ,类似于:

entry_points = {
    'console_scripts': ['my-script=my_package.my_module:main'],
}

Then you run python setup.py develop after activating the environment and you would run the script simply as a command: 然后,在激活环境后运行python setup.py develop ,并且可以简单地将脚本作为命令运行:

$ my-script

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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