Setup.py and python project setup

I have looked through the setup.py documentation and am still experiencing some difficulties with what, in my opinion, should be fairly simple.

I broke it down into a simple example project that I am trying to run, my layout of the project directory is as follows:

myproject setup.py src\ main.py extern\ __init__.py mytest.py 

MyProject / setup.py:

 #!/usr/bin/env python from distutils.core import setup setup(name = "myproject", package_dir = {'':"src"}, packages = ["extern"], scripts = ["src/main.py"], ) 

MyProject / SRC / main.py:

 #! /usr/bin/env python import extern.mytest as mytest mytest.print_test() 

MyProject / src / exebp / mytest.py:

 #!/usr/bin/env python def print_test(): print "YAY" 

myproject / src / extern / _init_.py is empty.

I run setup.py like:

 setup.py install --prefix ~/local 

setup.py will complete without errors and move main.py to ~ / local / bin, however when I start, I get the following error:

 ImportError: No module named extern.mytest 

Any idea what I'm doing wrong? Thanks!

+4
source share
1 answer

The problem is that the module is not under sys.path , and thus it cannot be found with the import statement.

In my case, the extern module was installed under ~/local/lib/python2.7/site-packages/extern . However, note that during installation, the installation path was arbitrarily set to ~/local .

To fix this, you can set your PYTHONPATH variable to the place where the module is installed, or add this path to sys.path in main.py

Alternatively, instead of:

 setup.py install --prefix ~/local 

using:

 setup.py install --user 

This will be installed in your custom site packages directory ( ~/.local on my platform) and python will be able to find the package without any problems. However, you may have to change the PATH environment variable to include ~/.local/bin .

+7
source

Source: https://habr.com/ru/post/1395323/


All Articles