简体   繁体   English

如何在Python 2.7.9中永久保存第3方模块?

[英]How do you permanently save a 3rd party module in Python 2.7.9?

I am new to programming and have been following some tutorials for learning the basics of Python 2.7.9 syntax. 我是编程新手,并且一直在关注一些教程以学习Python 2.7.9语法的基础。 I am also using Ubuntu 14.04.2 LTS. 我也在使用Ubuntu 14.04.2 LTS。

The tutorial is discussing how to create, save, import, and test custom modules for Python in Windows OS. 本教程讨论了如何在Windows OS中为Python创建,保存,导入和测试自定义模块。 I have been successful at doing so in Ubuntu, but only after adding a temporary directory path in shell for PYTHONPATH to search. 我在Ubuntu中已成功完成此操作,但仅在shell中添加了用于PYTHONPATH搜索的临时目录路径之后。

The default sys.path directories are: 缺省的sys.path目录为:

['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']`

As you can see, the default paths all fall under the /usr directory. 如您所见,默认路径全部位于/usr目录下。

2 solutions come to mind to fix this problem: 有2种解决方案可以解决此问题:

  1. How can I save under the /usr path directory? 如何保存在/usr路径目录下?
  2. How can I permanently add a new path directory? 如何永久添加新路径目录?

However, attempting to save the file testmod.py under /usr prompts the I/O Error message: 但是,尝试将文件testmod.py保存在/usr会提示I / O错误消息:

[Errno 13] Permission denied: 'usr/local/lib/Python-2.7.9/testmod.py'

You need sudo to save in /usr but another way is to simply add the path to your modules to ~/.bashrc or ~/.profile : 您需要sudo保存在/ usr中,但是另一种方法是将模块的路径简单地添加到~/.bashrc~/.profile

export PYTHONPATH="$PYTHONPATH:$HOME/my_mods"

where my_mods is in your home directory, .profile gets executed automatically by the DisplayManager during the start-up process desktop session as well as by the login shell when one logs in from the textual console my_mods位于您的主目录中, .profile 在启动过程桌面会话期间由DisplayManager自动执行,而从文本控制台登录时则由登录Shell自动执行。

.bashrc will work when you use the bash shell to start a program. 当您使用bash shell启动程序时, .bashrc将起作用。

You will need to logout and login after setting .profile or use source .bashrc if you go with the bashrc option for the settings to take effect. 如果您使用bashrc选项使设置生效,则需要在设置.profile后注销并登录,或者使用source .bashrc

environment variables 环境变量

To check your PYTHONPATH use echo $PYTHONPATH : 要检查您的PYTHONPATH,请使用echo $PYTHONPATH

~$ echo $PYTHONPATH
/home/padraic/mymods/

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

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