我如何使用PYTHONPATH

时间:2017-12-04 18:00:57

标签: python

我似乎无法从我的脚本中加载我的模块和类文件,但它从REPL加载

我有一个相当简单的项目结构

/home/user/project/
/home/user/project/bin/
/home/user/project/bin/myscript.py
/home/user/project/lib/
/home/user/project/lib/mymodule/__init__.py
/home/user/project/lib/mymodule/example.py

当我运行Python REPL时,一切运行正常。

$export PYTHONPATH=./lib:$PYTHONPATH # Making sure that the REPL and script have access
$python
Python 2.7.8 blashblahblah
...
>from mymodule.example import MyClass
>test = MyClass()
>^D
$./bin/myscript.py
Traceback (most recent call last):
  File "myscript.py", line 5, in <module>
    from mymodule.example import MyClass
  File "/path/to/myscript.py", line 5, in <module>
    from mymodule.example import MyClass
ImportError: No module named example

1 个答案:

答案 0 :(得分:0)

我会在这里回答我自己的问题。 事实证明,脚本名称是&#39; mymodule.py&#39;

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">

<body>

<h2 style="text-align:center">Lightbox</h2>

<div class="row">
  <div class="column">
    <img src="http://placekitten.com/420/600" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="http://placekitten.com/450/600" style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="http://placekitten.com/460/700" style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="http://placekitten.com/420/600" style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
  </div>
</div>

<div id="myModal" class="modal">
  <span class="close cursor" onclick="closeModal()">&times;</span>
  <div class="modal-content">

    <div class="mySlides">
      <div class="numbertext">1 / 4</div>
      <img src="http://placekitten.com/420/600" style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">2 / 4</div>
      <img src="http://placekitten.com/450/600" style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">3 / 4</div>
      <img src="http://placekitten.com/460/700" style="width:100%">
    </div>
    
    <div class="mySlides">
      <div class="numbertext">4 / 4</div>
      <img src="http://placekitten.com/420/600" style="width:100%">
    </div>
    
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

    <div class="caption-container">
      <p id="caption"></p>
    </div>


    <div class="column">
      <img class="demo cursor" src="http://placekitten.com/420/600" style="width:100%" onclick="currentSlide(1)" alt="Nature and sunrise">
    </div>
    <div class="column">
      <img class="demo cursor" src="http://placekitten.com/450/600" style="width:100%" onclick="currentSlide(2)" alt="Trolltunga, Norway">
    </div>
    <div class="column">
      <img class="demo cursor" src="http://placekitten.com/460/700" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
    </div>
    <div class="column">
      <img class="demo cursor" src="http://placekitten.com/420/600" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
    </div>
  </div>
</div>   
</body>
</html>

从repl导入时,pythonpath会解析为lib / mymodule模块,并且(主要)正确加载代码。运行脚本bin/ bin/mymodule.py lib/ lib/mymodule/ lib/mymodule/__init__.py lib/mymodule/example.py 时,python解释器会将脚本的目录mymodule.py添加到pythonpath 然后脚本会看到mymodule的一个import语句,并且自己来源。当找不到可以遍历的名称空间来查找&#39;示例&#39;在脚本中,它会抛出ImportError。

解决方案是重命名模块或重命名脚本(例如bin/),以便命名空间不会发生冲突。