2017-04-03 56 views
0

我已經下載了python 3的pycrypto模塊,所以我可以在我的python代碼中使用AES。 (EG,如果我有一個名爲encodeUsingAES.py的代碼)Python3在另一臺計算機上使用已安裝的模塊

但是,如果我只是將encodeUsingAES.py複製到另一臺計算機並正確運行,這將不起作用?因爲它沒有安裝pycrypto模塊,所以會彈出一堆錯誤。

我試着只是將pycrypto中的Crypto文件夾複製到我的.py文件所在的同一目錄中,但它不起作用。

反正我有我需要的所有文件在同一目錄下,所以當我壓縮併發送文件夾時,收件人可以直接運行.py文件而無需安裝額外的模塊?

謝謝!

from Crypto import Random 
from Crypto.Cipher import AES 

編輯: 試過這種沒有工作

import sys 
sys.path.append("/pycrypto") 
from Crypto import Random 
from Crypto.Cipher import AES 

$ python3 testCrypto.py 
    Traceback (most recent call last): 
    File "testCrypto.py", line 5, in <module> 
    from Crypto import Random 
    ImportError: No module named 'Crypto' 

import sys 
sys.path.append("pycrypto/lib") 
from Crypto import Random 
from Crypto.Cipher import AES 

$ python3 testCrypto.py 
    Traceback (most recent call last): 
    File "testCrypto.py", line 5, in <module> 
     from Crypto import Random 
    File "pycrypto/lib/Crypto/Random/__init__.py", line 28, in <module> 
     from Crypto.Random import OSRNG 
    File "pycrypto/lib/Crypto/Random/OSRNG/__init__.py", line 32, in <module> 
     from Crypto.Random.OSRNG.posix import new 
    File "pycrypto/lib/Crypto/Random/OSRNG/posix.py", line 66 
     except IOError, e: 
+0

如果使用路徑'/ pycrypto',這意味着pycrypto是在你的系統的根目錄的文件夾。如果你刪除'/'目錄應該是相對於你的工作目錄。 – PinkFluffyUnicorn

+0

@PinkFluffyUnicorn同樣的事情,文件「testCrypto.py」,5號線,從加密 導入隨機 導入錯誤:沒有名爲模塊「加密」 – Friedpanseller

+0

http://stackoverflow.com/questions/9059699/python-use-a -library-local-instead-of-installing-it應該可以幫到你 – PinkFluffyUnicorn

回答

0

如果複製完整的文件夾,你應該把它添加到您的路徑,能夠導入它。

import sys 
sys.path.append("/path/to/your/crypto/directory") 

from Crypto import Random 

如果pycrypto文件夾直接放置在您的項目文件夾中,以下語句應該可以工作。

sys.path.append('./pycrypto')

+0

但是Crypto.Random裏面有很多文件「從Crypto導入Someotherthing」,它們沒有sys.path.append,所以它們找不到正確的文件? – Friedpanseller

+0

我認爲sys.path鏈接到你的Python會話,所以一旦你修改了你的路徑,它應該在所有的python文件中都可見。或者你遇到這個問題? – PinkFluffyUnicorn

+0

我更新了有關錯誤的問題 – Friedpanseller

相關問題