简体   繁体   English

关于从其他目录导入的Python文件结构

[英]Python file structure about importing from other directories

I have problems with importing some functions in the file stored in other directories and importing random method from Crypto library. 我在导入存储在其他目录中的文件中的某些功能以及从Crypto库导入随机方法时遇到问题。

I installed Python34 and PyCrypto 2.6.1 in window 7. 我在窗口7中安装了Python34和PyCrypto 2.6.1。

Here is my file structure 这是我的文件结构

my_project 我的项目
/dh / dh
/__pycache__ / __ pycache__
/__init__.py /__init__.py
./lib ./lib
/__init__.py /__init__.py
/helpers.py /helpers.py

In the /dh/__init__.py file I have three import statements as follows 在/dh/__init__.py文件中,我具有三个import语句,如下所示


from Crypto.Hash import SHA256 -> it is fine 从Crypto.Hash导入SHA256->很好


from Crypto.Random import random -> gives me error "from Crypto.Random import OSRNG ImportError: cannot import name 'OSRNG' " 从Crypto.Random导入随机->给我错误“从Crypto.Random导入OSRNG ImportError:无法导入名称'OSRNG'”


from lib.helpers import read_hex --> gives me error "from lib.helpers import read_hex ImportError: No module named 'lib' " 从lib.helpers导入read_hex->给我错误“从lib.helpers导入read_hex ImportError:没有名为'lib'的模块”

I found out that there is not such file or folder OSRNG in Python34/Lib/sit-packages/Crypto/Random/ 我发现Python34 / Lib / sit-packages / Crypto / Random /中没有这样的文件或文件夹OSRNG

How can I fix these two errors? 如何解决这两个错误?

Thanks guys 多谢你们

Adding an answer as I don't have enough reputation to comment. 添加答案,因为我没有足够的声誉来发表评论。

I tried this in my system, I am able to import all the modules/Classes mentioned in the question. 我在系统中尝试过,我能够导入问题中提到的所有模块/类。 I do see the OSRNG directory. 我确实看到了OSRNG目录。

Are you sure you installed it properly? 您确定安装正确吗? Try reinstalling it. 尝试重新安装它。 Also, are you using a virtualenv? 另外,您是否正在使用virtualenv? If so, may be you forgot to activate it? 如果是这样,您可能会忘记激活它吗?

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

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