1. 程式人生 > >對python調包環境的探索:即從哪開始找?

對python調包環境的探索:即從哪開始找?

[email protected]:~$ echo $PATH
/home/yinhaibo/anaconda3/bin:/home/yinhaibo/bin:/home/yinhaibo/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
[email protected]:~$ cd /usr/local/bin
[email protected]:/usr/local/bin$ cp ~/Videos/test_models/__init__.py __init__.py
cp: cannot create regular file
'__init__.py': Permission denied [email protected]:/usr/local/bin$ sudo su [sudo] password for yinhaibo: [email protected]:/usr/local/bin# cp ~/Videos/test_models/__init__.py __init__.py cp: cannot stat '/root/Videos/test_models/__init__.py': No such file or directory [email protected]:/usr/local/bin# cp /home/yinhaibo/Videos/test_models/__init__.py __init__.py
[email protected]:/usr/local/bin# ^C [email protected]:/usr/local/bin# exit exit [email protected]:/usr/local/bin$ ll total 50560 drwxr-xr-x 2 root root 4096 12月 27 20:38 ./ drwxr-xr-x 10 root root 4096 3月 1 2018 ../ -rwxr-xr-x 1 root root 224 7月 31 15:13 chardetect* -rwxr-xr-x 1 root root 757 7月 19 15:37 f2py* -rwxr-xr-x 1 root root 246 7月 24 17:19 freeze_graph* -rw-r--r-- 1 root root 52 12月 27 20:38 __init__.py -rwxr-xr-x 1 root root 234 7月 20 16:25 iptest* -rwxr-xr-x 1 root root 234 7月 20 16:25 iptest2* -rwxr-xr-x 1 root root 227 7月 20 16:25 ipython* -rwxr-xr-x 1 root root 227 7月 20 16:25 ipython2* -rwxr-xr-x 1 root root 217 7月 24 17:19 markdown_py* lrwxrwxrwx 1 root root 28 8月 16 15:01 mindmaster ->
/opt/MindMaster-6/MindMaster* -rwxr-xr-x 1 root root 35097 9月 19 13:54 miniterm.py* -rw-r--r-- 1 root root 36151 9月 19 13:54 miniterm.pyc -rwxr-xr-x 1 root root 214 8月 21 16:09 nosetests* -rwxr-xr-x 1 root root 214 8月 21 16:09 nosetests-2.7* -rwxr-xr-x 1 root root 214 7月 24 17:19 pbr* -rwxr-xr-x 1 root root 215 11月 29 13:51 pip* -rwxr-xr-x 1 root root 215 11月 29 13:51 pip2* -rwxr-xr-x 1 root root 215 11月 29 13:51 pip2.7* -rwxr-xr-x 1 root root 218 7月 20 16:25 pygmentize* -rwxr-xr-x 1 root root 2524 9月 19 13:54 rf_monitor.py* -rw-r--r-- 1 root root 2027 9月 19 13:54 rf_monitor.pyc -rwxr-xr-x 1 root root 5744 9月 18 18:11 rpyc_classic.py* -rw-r--r-- 1 root root 5981 9月 18 18:11 rpyc_classic.pyc -rwxr-xr-x 1 root root 1691 9月 18 18:11 rpyc_registry.py* -rw-r--r-- 1 root root 2232 9月 18 18:11 rpyc_registry.pyc -rwxr-xr-x 1 root root 241 7月 24 17:19 saved_model_cli* -rwxr-xr-x 1 root root 216 9月 18 20:05 stomp* -rwxr-xr-x 1 root root 226 7月 24 17:19 tensorboard* -rwxr-xr-x 1 root root 51559400 7月 20 16:04 thrift* -rwxr-xr-x 1 root root 250 7月 24 17:19 toco* -rwxr-xr-x 1 root root 254 7月 24 17:19 toco_from_protos* -rwxr-xr-x 1 root root 212 9月 21 11:13 tqdm* -rwxr-xr-x 1 root root 212 7月 19 12:28 virtualenv* -rwxr-xr-x 1 root root 221 9月 21 11:08 wonambi* [email protected]:/usr/local/bin$ cd ~ [email protected]:~$ ipython Python 3.6.5 |Anaconda custom (64-bit)| (default, Apr 29 2018, 16:14:56) Type 'copyright', 'credits' or 'license' for more information IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help. In [1]: exit [email protected]:~$ sudo su [email protected]:/home/yinhaibo# pdw No command 'pdw' found, did you mean: Command 'pd' from package 'puredata-core' (universe) Command 'pda' from package 'speech-tools' (universe) Command 'pdb' from package 'python' (main) Command 'cdw' from package 'cdw' (universe) Command 'pdv' from package 'alliance' (universe) Command 'pwd' from package 'coreutils' (main) Command 'psw' from package 'wise' (universe) Command 'pdl' from package 'pdl' (universe) Command 'paw' from package 'paw-common' (universe) Command 'pmw' from package 'pmw' (universe) pdw: command not found [email protected]:/home/yinhaibo# pwd /home/yinhaibo [email protected]:/home/yinhaibo# cp ./Videos/test_models/ /usr/local/bin/test_model cp: omitting directory './Videos/test_models/' [email protected]:/home/yinhaibo# cp ~/Videos/test_models/ /usr/local/bin/test_model cp: cannot stat '/root/Videos/test_models/': No such file or directory [email protected]:/home/yinhaibo# cp /home/yinhaibo/Videos/test_models/ /usr/local/bin/test_model cp: omitting directory '/home/yinhaibo/Videos/test_models/' [email protected]:/home/yinhaibo# cp /home/yinhaibo/Videos/test_models /usr/local/bin/test_model cp: omitting directory '/home/yinhaibo/Videos/test_models' [email protected]:/home/yinhaibo# cp -r /home/yinhaibo/Videos/test_models /usr/local/bin/test_model [email protected]:/home/yinhaibo# exit exit [email protected]:~$ cd usr bash: cd: usr: No such file or directory [email protected]:~$ cd /usr/local/bin/ [email protected]:/usr/local/bin$ ll total 50564 drwxr-xr-x 3 root root 4096 12月 27 20:42 ./ drwxr-xr-x 10 root root 4096 3月 1 2018 ../ -rwxr-xr-x 1 root root 224 7月 31 15:13 chardetect* -rwxr-xr-x 1 root root 757 7月 19 15:37 f2py* -rwxr-xr-x 1 root root 246 7月 24 17:19 freeze_graph* -rw-r--r-- 1 root root 52 12月 27 20:38 __init__.py -rwxr-xr-x 1 root root 234 7月 20 16:25 iptest* -rwxr-xr-x 1 root root 234 7月 20 16:25 iptest2* -rwxr-xr-x 1 root root 227 7月 20 16:25 ipython* -rwxr-xr-x 1 root root 227 7月 20 16:25 ipython2* -rwxr-xr-x 1 root root 217 7月 24 17:19 markdown_py* lrwxrwxrwx 1 root root 28 8月 16 15:01 mindmaster -> /opt/MindMaster-6/MindMaster* -rwxr-xr-x 1 root root 35097 9月 19 13:54 miniterm.py* -rw-r--r-- 1 root root 36151 9月 19 13:54 miniterm.pyc -rwxr-xr-x 1 root root 214 8月 21 16:09 nosetests* -rwxr-xr-x 1 root root 214 8月 21 16:09 nosetests-2.7* -rwxr-xr-x 1 root root 214 7月 24 17:19 pbr* -rwxr-xr-x 1 root root 215 11月 29 13:51 pip* -rwxr-xr-x 1 root root 215 11月 29 13:51 pip2* -rwxr-xr-x 1 root root 215 11月 29 13:51 pip2.7* -rwxr-xr-x 1 root root 218 7月 20 16:25 pygmentize* -rwxr-xr-x 1 root root 2524 9月 19 13:54 rf_monitor.py* -rw-r--r-- 1 root root 2027 9月 19 13:54 rf_monitor.pyc -rwxr-xr-x 1 root root 5744 9月 18 18:11 rpyc_classic.py* -rw-r--r-- 1 root root 5981 9月 18 18:11 rpyc_classic.pyc -rwxr-xr-x 1 root root 1691 9月 18 18:11 rpyc_registry.py* -rw-r--r-- 1 root root 2232 9月 18 18:11 rpyc_registry.pyc -rwxr-xr-x 1 root root 241 7月 24 17:19 saved_model_cli* -rwxr-xr-x 1 root root 216 9月 18 20:05 stomp* -rwxr-xr-x 1 root root 226 7月 24 17:19 tensorboard* drwxr-xr-x 3 root root 4096 12月 27 20:42 test_model/ -rwxr-xr-x 1 root root 51559400 7月 20 16:04 thrift* -rwxr-xr-x 1 root root 250 7月 24 17:19 toco* -rwxr-xr-x 1 root root 254 7月 24 17:19 toco_from_protos* -rwxr-xr-x 1 root root 212 9月 21 11:13 tqdm* -rwxr-xr-x 1 root root 212 7月 19 12:28 virtualenv* -rwxr-xr-x 1 root root 221 9月 21 11:08 wonambi* [email protected]:/usr/local/bin$ cd ~ [email protected]:~$ ipython Python 3.6.5 |Anaconda custom (64-bit)| (default, Apr 29 2018, 16:14:56) Type 'copyright', 'credits' or 'license' for more information IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help. In [1]: import test_model --------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-1-3e67009ed13b> in <module> ----> 1 import test_model ModuleNotFoundError: No module named 'test_model' In [2]: import sys In [3]: sys.path Out[3]: ['', '/home/yinhaibo/anaconda3/bin', '/home/yinhaibo/anaconda3/lib/python36.zip', '/home/yinhaibo/anaconda3/lib/python3.6', '/home/yinhaibo/anaconda3/lib/python3.6/lib-dynload', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages/IPython/extensions', '/home/yinhaibo/.ipython'] In [4]: sys.path.append(0, '/usr/local/bin/') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-7341f50b0793> in <module> ----> 1 sys.path.append(0, '/usr/local/bin/') TypeError: append() takes exactly one argument (2 given) In [5]: append? Object `append` not found. In [6]: help(append) --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-6-0b77908b9346> in <module> ----> 1 help(append) NameError: name 'append' is not defined In [7]: sys.path.insert(0, '/usr/local/bin') In [8]: sys.path Out[8]: ['/usr/local/bin', '', '/home/yinhaibo/anaconda3/bin', '/home/yinhaibo/anaconda3/lib/python36.zip', '/home/yinhaibo/anaconda3/lib/python3.6', '/home/yinhaibo/anaconda3/lib/python3.6/lib-dynload', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages/IPython/extensions', '/home/yinhaibo/.ipython'] In [9]: import test_model In [10]: from test_model import (pd, np, time) In [11]: time.time() Out[11]: 1545914817.7087874 In [12]: np.linspace(1, 9, 9) Out[12]: array([1., 2., 3., 4., 5., 6., 7., 8., 9.])

著這裡嘗試將一個自定義包 test_models 放在環境變數中
然後看python執行時能否調包成功

最後結果:未成功

然後我採用增加臨時python環境變數的方法將包所在的環境加入。
實驗調包成功

其中
import time
import pandas as pd
import numpy as np
為__init__.py 內容

實驗發現:python每次執行的搜尋環境最穩定是的sys.path

雖然會有當前工作目錄的加入但是並不穩定。如果換個工作目錄就立刻現原形。

In [13]: exit                                                                                                                                                                                               
[email protected]:~$ cd usr
bash: cd: usr: No such file or directory
[email protected]:~$ cd /usr/local/bin
[email protected]:/usr/local/bin$ rm -r test_model
rm: descend into write-protected directory 'test_model'? 
[email protected]:/usr/local/bin$ ll
total 50564
drwxr-xr-x  3 root root     4096 12月 27 20:42 ./
drwxr-xr-x 10 root root     4096 3月   1  2018 ../
-rwxr-xr-x  1 root root      224 7月  31 15:13 chardetect*
-rwxr-xr-x  1 root root      757 7月  19 15:37 f2py*
-rwxr-xr-x  1 root root      246 7月  24 17:19 freeze_graph*
-rw-r--r--  1 root root       52 12月 27 20:38 __init__.py
-rwxr-xr-x  1 root root      234 7月  20 16:25 iptest*
-rwxr-xr-x  1 root root      234 7月  20 16:25 iptest2*
-rwxr-xr-x  1 root root      227 7月  20 16:25 ipython*
-rwxr-xr-x  1 root root      227 7月  20 16:25 ipython2*
-rwxr-xr-x  1 root root      217 7月  24 17:19 markdown_py*
lrwxrwxrwx  1 root root       28 8月  16 15:01 mindmaster -> /opt/MindMaster-6/MindMaster*
-rwxr-xr-x  1 root root    35097 9月  19 13:54 miniterm.py*
-rw-r--r--  1 root root    36151 9月  19 13:54 miniterm.pyc
-rwxr-xr-x  1 root root      214 8月  21 16:09 nosetests*
-rwxr-xr-x  1 root root      214 8月  21 16:09 nosetests-2.7*
-rwxr-xr-x  1 root root      214 7月  24 17:19 pbr*
-rwxr-xr-x  1 root root      215 11月 29 13:51 pip*
-rwxr-xr-x  1 root root      215 11月 29 13:51 pip2*
-rwxr-xr-x  1 root root      215 11月 29 13:51 pip2.7*
-rwxr-xr-x  1 root root      218 7月  20 16:25 pygmentize*
-rwxr-xr-x  1 root root     2524 9月  19 13:54 rf_monitor.py*
-rw-r--r--  1 root root     2027 9月  19 13:54 rf_monitor.pyc
-rwxr-xr-x  1 root root     5744 9月  18 18:11 rpyc_classic.py*
-rw-r--r--  1 root root     5981 9月  18 18:11 rpyc_classic.pyc
-rwxr-xr-x  1 root root     1691 9月  18 18:11 rpyc_registry.py*
-rw-r--r--  1 root root     2232 9月  18 18:11 rpyc_registry.pyc
-rwxr-xr-x  1 root root      241 7月  24 17:19 saved_model_cli*
-rwxr-xr-x  1 root root      216 9月  18 20:05 stomp*
-rwxr-xr-x  1 root root      226 7月  24 17:19 tensorboard*
drwxr-xr-x  3 root root     4096 12月 27 20:42 test_model/
-rwxr-xr-x  1 root root 51559400 7月  20 16:04 thrift*
-rwxr-xr-x  1 root root      250 7月  24 17:19 toco*
-rwxr-xr-x  1 root root      254 7月  24 17:19 toco_from_protos*
-rwxr-xr-x  1 root root      212 9月  21 11:13 tqdm*
-rwxr-xr-x  1 root root      212 7月  19 12:28 virtualenv*
-rwxr-xr-x  1 root root      221 9月  21 11:08 wonambi*
[email protected]:/usr/local/bin$ rm -r test_model
rm: descend into write-protected directory 'test_model'? yes
rm: descend into write-protected directory 'test_model/__pycache__'? yes 
rm: remove write-protected regular file 'test_model/__pycache__/__init__.cpython-36.pyc'? yes 
rm: cannot remove 'test_model/__pycache__/__init__.cpython-36.pyc': Permission denied
rm: remove write-protected regular file 'test_model/__init__.py'? yes
rm: cannot remove 'test_model/__init__.py': Permission denied
[email protected]:/usr/local/bin$ ll
total 50564
drwxr-xr-x  3 root root     4096 12月 27 20:42 ./
drwxr-xr-x 10 root root     4096 3月   1  2018 ../
-rwxr-xr-x  1 root root      224 7月  31 15:13 chardetect*
-rwxr-xr-x  1 root root      757 7月  19 15:37 f2py*
-rwxr-xr-x  1 root root      246 7月  24 17:19 freeze_graph*
-rw-r--r--  1 root root       52 12月 27 20:38 __init__.py
-rwxr-xr-x  1 root root      234 7月  20 16:25 iptest*
-rwxr-xr-x  1 root root      234 7月  20 16:25 iptest2*
-rwxr-xr-x  1 root root      227 7月  20 16:25 ipython*
-rwxr-xr-x  1 root root      227 7月  20 16:25 ipython2*
-rwxr-xr-x  1 root root      217 7月  24 17:19 markdown_py*
lrwxrwxrwx  1 root root       28 8月  16 15:01 mindmaster -> /opt/MindMaster-6/MindMaster*
-rwxr-xr-x  1 root root    35097 9月  19 13:54 miniterm.py*
-rw-r--r--  1 root root    36151 9月  19 13:54 miniterm.pyc
-rwxr-xr-x  1 root root      214 8月  21 16:09 nosetests*
-rwxr-xr-x  1 root root      214 8月  21 16:09 nosetests-2.7*
-rwxr-xr-x  1 root root      214 7月  24 17:19 pbr*
-rwxr-xr-x  1 root root      215 11月 29 13:51 pip*
-rwxr-xr-x  1 root root      215 11月 29 13:51 pip2*
-rwxr-xr-x  1 root root      215 11月 29 13:51 pip2.7*
-rwxr-xr-x  1 root root      218 7月  20 16:25 pygmentize*
-rwxr-xr-x  1 root root     2524 9月  19 13:54 rf_monitor.py*
-rw-r--r--  1 root root     2027 9月  19 13:54 rf_monitor.pyc
-rwxr-xr-x  1 root root     5744 9月  18 18:11 rpyc_classic.py*
-rw-r--r--  1 root root     5981 9月  18 18:11 rpyc_classic.pyc
-rwxr-xr-x  1 root root     1691 9月  18 18:11 rpyc_registry.py*
-rw-r--r--  1 root root     2232 9月  18 18:11 rpyc_registry.pyc
-rwxr-xr-x  1 root root      241 7月  24 17:19 saved_model_cli*
-rwxr-xr-x  1 root root      216 9月  18 20:05 stomp*
-rwxr-xr-x  1 root root      226 7月  24 17:19 tensorboard*
drwxr-xr-x  3 root root     4096 12月 27 20:42 test_model/
-rwxr-xr-x  1 root root 51559400 7月  20 16:04 thrift*
-rwxr-xr-x  1 root root      250 7月  24 17:19 toco*
-rwxr-xr-x  1 root root      254 7月  24 17:19 toco_from_protos*
-rwxr-xr-x  1 root root      212 9月  21 11:13 tqdm*
-rwxr-xr-x  1 root root      212 7月  19 12:28 virtualenv*
-rwxr-xr-x  1 root root      221 9月  21 11:08 wonambi*
[email protected]:/usr/local/bin$ sudo su
[sudo] password for yinhaibo: 
[email protected]:/usr/local/bin# rm -r test_model
[email protected]:/usr/local/bin# rm __init__.py 
[email protected]:/usr/local/bin# exit
exit
[email protected]:/usr/local/bin$ cd ~
[email protected]:~$ cd Videos/
[email protected]:~/Videos$ vim test_working_dirctory.py
[email protected]:~/Videos$ python test_working_dirctory.py 
  File "test_working_dirctory.py", line 3
    a = np.random.randint(1, 9, 9).reshape(3, 3)
    ^
SyntaxError: invalid syntax
[email protected]:~/Videos$ vim test_working_dirctory.py
[email protected]:~/Videos$ python test_working_dirctory.py 
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
[email protected]:~/Videos$ vim test_working_dirctory.py
[email protected]:~/Videos$ vim test_working_dirctory.py
[email protected]:~/Videos$ python test_working_dirctory.py 
  File "test_working_dirctory.py", line 4
    a = np.random.randint(1, 9, 9).reshape(3, 3)
    ^
SyntaxError: invalid syntax
[email protected]:~/Videos$ vim test_working_dirctory.py
[email protected]:~/Videos$ python test_working_dirctory.py 
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
2018-12-27 21:08-26
a
    0  1  2
0  4  5  8
1  6  1  6
2  2  4  7
[email protected]:~/Videos$ vim test_working_dirctory.py
yinh[email protected]:~/Videos$ python test_working_dirctory.py 
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
2018-12-27 21:09-20
a:[[3 1 1]
 [7 4 4]
 [4 6 4]]
    0  1  2
0  3  1  1
1  7  4  4
2  4  6  4
[email protected]:~/Videos$ cd ~
[email protected]:~$ python ./Videos/test_working_dirctory.py 
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
2018-12-27 21:10-02
a:[[3 3 8]
 [7 6 2]
 [4 5 7]]
    0  1  2
0  3  3  8
1  7  6  2
2  4  5  7
[email protected]:~$ mv p
pan.baidu.com/                                    protobuf-3.6.0-cp27-cp27mu-manylinux1_x86_64.whl  python 2018.08.08.ipynb                           
plot_roc.ipynb                                    putty.log                                         
[email protected]:~$ mv ./Videos/test_working_dirctory.py test_working_directory.py
[email protected]:~$ python test_working_directory.py 
Traceback (most recent call last):
  File "test_working_directory.py", line 1, in <module>
    from test_models import (np, pd, time)
ModuleNotFoundError: No module named 'test_models'
[email protected]:~$ 

這裡檔案存放在哪個位置預設工作目錄就在那。

如果說ipython 就是開啟 ipython的視窗就是工作目錄。

總的來說有必要注意python程式的搜尋環境

#對test_working_directory.py 進行修改
增加語句:

import sys
print(sys.path)

首先在/home/yinhaibo/下執行一次

不成功但是可以看到搜尋目錄是。。。

然後移至Videos中, 再執行。

[email protected]:~$ vim test_working_directory.py 
[email protected]:~$ python test_working_directory.py 
['/home/yinhaibo', '/home/yinhaibo/anaconda3/lib/python36.zip', '/home/yinhaibo/anaconda3/lib/python3.6', '/home/yinhaibo/anaconda3/lib/python3.6/lib-dynload', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg']
Traceback (most recent call last):
  File "test_working_directory.py", line 3, in <module>
    from test_models import (np, pd, time)
ModuleNotFoundError: No module named 'test_models'
[email protected]:~$ mv test_working_directory.py ./Videos/test_working_directory.py
[email protected]:~$ python ./Videos/test_working_directory.py 
['/home/yinhaibo/Videos', '/home/yinhaibo/anaconda3/lib/python36.zip', '/home/yinhaibo/anaconda3/lib/python3.6', '/home/yinhaibo/anaconda3/lib/python3.6/lib-dynload', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages', '/home/yinhaibo/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg']
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
2018-12-27 21:17-48
a:[[2 4 1]
 [7 3 3]
 [7 8 6]]
    0  1  2
0  2  4  1
1  7  3  3
2  7  8  6
[email protected]:~$