如何将我的模块添加到travi -ci pythonpath中

时间:2022-09-16 23:10:53

I'm setting up Travis-CI for my project, and oddly, I can't import my project:

我正在为我的项目设置travi - ci,奇怪的是,我不能导入我的项目:

$ python tests/tests.py
Traceback (most recent call last):
  File "tests/tests.py", line 11, in <module>
    from my_module.lib.importer import build_module_list
ImportError: No module named my_module.lib.importer

In production, I just create a symlink like so:

在制作过程中,我只是创建了这样的符号链接:

sudo ln -s /usr/local/my_module /usr/lib/python2.7/dist-packages/my_module

But I don't know -- or want to know, really -- Travis-CI's folder structure.

但我不知道,也不想知道,travi - ci的文件夹结构。

This seems like a solved problem, but I'm new to Travis-CI. What's the best way to make this work, so my code is added as an importable module?

这似乎是一个已解决的问题,但我对travi - ci并不熟悉。什么是使这个工作的最佳方式,因此我的代码被添加为一个可导入的模块?

5 个解决方案

#1


14  

What's the best way to make this work, so my code is added as an importable module?

什么是使这个工作的最佳方式,因此我的代码被添加为一个可导入的模块?

The answer is unequivocally to use distutils (and definitely not ln).

答案是明确地使用二语(肯定不是ln)。

In production, I just create a symlink ...

在生产中,我只创建一个符号链接……

B-b-but why? The complexity to do it the Right Way™ is so low! It even fits in a few lines:

B-b-but为什么?™的复杂性做正确的方法是如此之低!它甚至可以用几句话来表达:

From The Fine Manual -- just create a setup.py like this:

从精美的手册中——创建一个设置。py是这样的:

from distutils.core import setup

setup(name='Distutils',
      version='1.0',
      description='Python Distribution Utilities',
      author='Greg Ward',
      author_email='gward@python.net',
      url='https://www.python.org/sigs/distutils-sig/',
      packages=['distutils', 'distutils.command'],
     )

Now you can do fantastic things like python setup.py install, python setup.py bdist_rpm or pip install . -- and not just in the Travis environment but for your project in general.

现在您可以做一些奇妙的事情,比如python安装。py安装python设置。py bdist_rpm或pip安装。不仅仅是在特拉维斯的环境中,而是在你的项目中。

#2


6  

To be quick, you can fix the problem more elegantly by adding the following to the before_script stage:

为了快速解决这个问题,您可以在before_script阶段添加以下内容:

export PYTHONPATH=$PYTHONPATH:$(pwd)

The better way(but with a little more effort) is what Brian Cain has suggested, namely, write a setup.py file and add pip install . to the install stage.

更好的方法(但需要更多的努力)是Brian Cain提出的,也就是写一个设置。py文件并添加pip安装。安装阶段。

Or if you're using a makefile you can have command that does it as the following one.

或者,如果您使用的是makefile,您可以拥有以下命令。

test:
    $(shell export PYTHONPATH=$PYTHONPATH:$(pwd))
    python setup.py test

#3


1  

This is certainly not optimal, but it worked. In my .travis.yml file, I added the following line to the install attribute:

这当然不是最佳选择,但它确实奏效了。在我.travis。在yml文件中,我在install属性中添加了如下一行:

 - ln -s `pwd` $(dirname `which python`)/../lib/python2.7/site-packages/my_module

This basically finds the directory where Python is installed and then adds my_module as a symlink in there. Happy to hear a better answer, cause this one feels super fragile.

它基本上找到了安装Python的目录,然后在其中添加my_module作为符号链接。很高兴听到更好的答案,因为这个感觉超级脆弱。

Update: See the answer by @Brian Cain for a much better solution.

更新:请参见@Brian Cain的答案,以获得更好的解决方案。

#4


1  

In complement of @Brian-Cain answer, you can also use setuptools instead of distutils. As of writing, distutils is being phased out, and setuptools is being used as a replacement, even though setuptools is not yet in standard library.

作为@Brian-Cain的补充,您还可以使用setuptools来代替distutils。在撰写本文时,distutils正在逐步淘汰,setuptools正在被用作替代工具,尽管setuptools还没有出现在标准库中。

from setuptools import setup, find_packages

setup(name='Foo',
      version='0.0.1',
      description='Python Distribution Utilities',
      author='',
      author_email='',
      url='',
      packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
     )

For a quick tutorial on making a setup.py with setuptools: https://packaging.python.org/tutorials/distributing-packages/

为了一个快速的教程关于建立一个设置。py setuptools:https://packaging.python.org/tutorials/distributing-packages/

For a quick real example: https://github.com/pypa/sampleproject/blob/master/setup.py

对于一个快速的真实示例:https://github.com/pypa/sampleproject/blob/master/setup.py。

#5


0  

It's more likely necessary to add /home/travis/.local/lib/python2.7/site-packages/ to PYTHONPATH in before_script using export PYTHONPATH=$PYTHONPATH:/home/travis/.local/lib/python2.7/site-packages/.

在before_script中,更有可能需要使用export PYTHONPATH=$PYTHONPATH:/home/travis/.local/lib/python2.7/site-packages/。

#1


14  

What's the best way to make this work, so my code is added as an importable module?

什么是使这个工作的最佳方式,因此我的代码被添加为一个可导入的模块?

The answer is unequivocally to use distutils (and definitely not ln).

答案是明确地使用二语(肯定不是ln)。

In production, I just create a symlink ...

在生产中,我只创建一个符号链接……

B-b-but why? The complexity to do it the Right Way™ is so low! It even fits in a few lines:

B-b-but为什么?™的复杂性做正确的方法是如此之低!它甚至可以用几句话来表达:

From The Fine Manual -- just create a setup.py like this:

从精美的手册中——创建一个设置。py是这样的:

from distutils.core import setup

setup(name='Distutils',
      version='1.0',
      description='Python Distribution Utilities',
      author='Greg Ward',
      author_email='gward@python.net',
      url='https://www.python.org/sigs/distutils-sig/',
      packages=['distutils', 'distutils.command'],
     )

Now you can do fantastic things like python setup.py install, python setup.py bdist_rpm or pip install . -- and not just in the Travis environment but for your project in general.

现在您可以做一些奇妙的事情,比如python安装。py安装python设置。py bdist_rpm或pip安装。不仅仅是在特拉维斯的环境中,而是在你的项目中。

#2


6  

To be quick, you can fix the problem more elegantly by adding the following to the before_script stage:

为了快速解决这个问题,您可以在before_script阶段添加以下内容:

export PYTHONPATH=$PYTHONPATH:$(pwd)

The better way(but with a little more effort) is what Brian Cain has suggested, namely, write a setup.py file and add pip install . to the install stage.

更好的方法(但需要更多的努力)是Brian Cain提出的,也就是写一个设置。py文件并添加pip安装。安装阶段。

Or if you're using a makefile you can have command that does it as the following one.

或者,如果您使用的是makefile,您可以拥有以下命令。

test:
    $(shell export PYTHONPATH=$PYTHONPATH:$(pwd))
    python setup.py test

#3


1  

This is certainly not optimal, but it worked. In my .travis.yml file, I added the following line to the install attribute:

这当然不是最佳选择,但它确实奏效了。在我.travis。在yml文件中,我在install属性中添加了如下一行:

 - ln -s `pwd` $(dirname `which python`)/../lib/python2.7/site-packages/my_module

This basically finds the directory where Python is installed and then adds my_module as a symlink in there. Happy to hear a better answer, cause this one feels super fragile.

它基本上找到了安装Python的目录,然后在其中添加my_module作为符号链接。很高兴听到更好的答案,因为这个感觉超级脆弱。

Update: See the answer by @Brian Cain for a much better solution.

更新:请参见@Brian Cain的答案,以获得更好的解决方案。

#4


1  

In complement of @Brian-Cain answer, you can also use setuptools instead of distutils. As of writing, distutils is being phased out, and setuptools is being used as a replacement, even though setuptools is not yet in standard library.

作为@Brian-Cain的补充,您还可以使用setuptools来代替distutils。在撰写本文时,distutils正在逐步淘汰,setuptools正在被用作替代工具,尽管setuptools还没有出现在标准库中。

from setuptools import setup, find_packages

setup(name='Foo',
      version='0.0.1',
      description='Python Distribution Utilities',
      author='',
      author_email='',
      url='',
      packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
     )

For a quick tutorial on making a setup.py with setuptools: https://packaging.python.org/tutorials/distributing-packages/

为了一个快速的教程关于建立一个设置。py setuptools:https://packaging.python.org/tutorials/distributing-packages/

For a quick real example: https://github.com/pypa/sampleproject/blob/master/setup.py

对于一个快速的真实示例:https://github.com/pypa/sampleproject/blob/master/setup.py。

#5


0  

It's more likely necessary to add /home/travis/.local/lib/python2.7/site-packages/ to PYTHONPATH in before_script using export PYTHONPATH=$PYTHONPATH:/home/travis/.local/lib/python2.7/site-packages/.

在before_script中,更有可能需要使用export PYTHONPATH=$PYTHONPATH:/home/travis/.local/lib/python2.7/site-packages/。