简体   繁体   English

在Ubuntu上的python中导入错误

[英]import error in python on ubuntu

I have created my first python module on ubuntu. 我在ubuntu上创建了我的第一个python模块。 When I'm trying to import the module in python using : 当我尝试使用python导入模块时:

import brian

it is giving error: 它给出了错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named brian

I have brian in /home/noamaan and python is in /usr/bin . 我在/home/noamaanbrian ,在/usr/bin python。

If you launch python from the directory that contains brian module, everything will work as it is now. 如果您从包含brian模块的目录中启动python ,那么一切将照常进行

To import custom module from anywhere you want you should read attentively something on the import mechanism in python to learn where the imported modules are searched for, etc. 要从您想要的任何地方导入自定义模块您应该认真阅读python导入机制中的内容,以了解在何处搜索导入的模块,等等。

But to make your code work right now, I can recommend you the following: 但是为了使您的代码现在可以正常工作,我可以向您推荐以下内容:

  • Either extend your PYTHONPATH variable before running python, to include the directory of your module 在运行python之前扩展PYTHONPATH变量,以包括模块的目录
  • Or append it right in the code by using sys module in this way. 或以这种方式使用sys模块将其附加在代码中。

     import sys sys.path.append("path/to/module/dir") import brian 

Also, see info on site module 另外,请参阅网站模块上的信息

by default Python import modules from Python path var. 默认情况下,Python从Python路径var中导入模块。 You can view these paths so: 您可以查看以下路径:

import sys 导入系统

print sys.path 打印系统路径

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

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