I'm new to packaging in Python. I've tried to specify my non-python files within setup.py's 'scripts' argument, and also specifying the file within MANIFEST.in, however after I package the file using python setup.py build sdist and install using pip, only the files with the .py extension make it to the site-packages/my_package directory.
Am I missing something?
15 minutes later I find the answer. sdist only includes the *.py files. I just changed the command to use bdist_wheel and all the files I needed were included.
Related
We're having a Python 2 product we publish as RPMs. The RPMs include only the pyo/pyc files, and the running scripts refer to certain pyo files to run the product. We use setuptools to package these RPM:
python setup.py bdist_rpm --release=... --requires=... --bdist-base=...
We're now porting out product to Python 3.6, and found that pyc files no longer reside in the same location as the py files. They are located in a __pycache__ folder under the folder of the py files, and moreover, they will not work if the corresponding py file does not exist. There is still support in legacy behavior if the pyc file is replacing the py file, and is not in __pycache__.
The documentation shows that the python compileall can accept a -b flag that:
Write the byte-code files to their legacy locations and names, which
may overwrite byte-code files created by another version of Python.
The default is to write files to their PEP 3147 locations and names,
which allows byte-code files from multiple versions of Python to
coexist.
However, since we use setuptools, we do not control the compilation command line.
Questions:
Is there a way to tell setuptools to compile the code with the -b flag and keep the legacy location of pyc files?
Is there a better way in Python 3 to publish source-less RPMs and still be able to call the pyc file from a script?
I have run setup.py and installed, my script/package it all appears in Python27\Lib\site-packages in a folder with the relevant .egg.info file.
However, after adding site-packages to my PATH I cannot run the relevant scripts.
python: cant open file 'mypackage': [Errno 2] No such file or directory
What have I missed?
Essentially I would like to install the package, several modules are included in a folder with __init and __main files. Once installed I would like to just run
python mypackage inputfile.txt
however mypackage is not found. I will use optparse for taking in the txt files, but that is irrelevant as I can't get the packages installed.
As a rule, scripts do not go in site-packages. On the MS-windows platform they generally go in C:\Python27\Scripts\.
So on MS-Windows you probably want to add C:\Python27\;C:\Python27\Scripts\ to your $PATH.
Additionally, you have to associate the .py filetype with python.
In general, the whole chapter Using Python on Windows of the official CPython documentation should be considered required reading for users of Python on Windows.
Python has ability to "pseudoinstall" a package by running it's setup.py script with develop instead of install. This modifies python environment so package can be imported from it's current location (it's not copied into site-package directory). This allows to develop packages that are used by other packages: source code is modified in place and changes are available to rest of python code via simple import.
All works fine except that setup.py develop command creates an .egg-info folder with metadata at same level as setup.py. Mixing source code and temporary files is not very good idea - this folder need to be added into "ignore" lists of multiple tools starting from vcs and ending backup systems.
Is it possible to use setup.py develop but create .egg-info directory in some other place, so original source code is not polluted by temporary directory and files?
setup.py develop creates a python egg, in-place; it does not [modify the] python environment so package can be imported from it's current location. You still have to either add it's location to the python search path or use the directory it is placed in as the current directory.
It is the job of the develop command to create an in-place egg, which may include compiling C extensions, running the 2to3 python conversion process to create Python3 compatible code, and to provide metadata other python code may be relying on. When you install the package as an egg in your site-packages directory, the same metadata is included there as well. The data is certainly not temporary (it is extracted from your setup.py file for easy parsing by other tools).
The intent is that you can then rely on that metadata when using your package in a wider system that relies on the metadata being present, while still developing the package. For example, in a buildout development deployment, we often use mr.developer to automate the process of fetching the source code for a given package when we need to work on it, which builds it as a develop egg and ties it into the deployment while we work on the code.
Note that the .egg-info directory serves a specific purpose: to signal to other tools in the setuptools eco-system that your package is installed and available. If your package is a dependency of another egg in your setup, then that dependency is satisfied. pip and easy_install and buildout will not try and fetch the egg from somewhere else instead.
Apart from creating the .egg-info directory, the only other thing the command does, is to build extensions, in-place. So the command you are looking for instead is:
setup.py build_ext --inplace
This will do the exact same thing as setup.py develop but leave out the .egg-info directory. It also won't generate the .pth file.
There is no way of generating only the .pth file and leave out the .egg-info directory generation.
Technically speaking, setup.py develop will also check if you have the setuptools site.py file installed to support namespaced packages, but that's not relevant here.
The good manner is to keep all source files inside special directory which name is your project name (programmers using other languages keep their code inside src directory). So if your setup.py file is inside myproject directory then you should keep the files at myproject/myproject. This method keeps your sources separated from other files regardless what happen in main directory.
My suggestion would be to use whitelist instead of blacklist -- tell the tools to ignore all files excluding these which are inside myproject directory. I think that this is the simplest way not to touch your ignore lists too often.
Try the --install-dir option. You may also want to use --build-dir to change building dir.
I'm uploadig my package to PyPi with this command:
python setup.py sdist upload
This command generate some files and folders, is there any option to delete this files after upload?
The sdist command calls the build command which by default puts files in a build subdirectory. You probably want to keep that around (i.e. not care about it) to speed up future builds.
sdist then puts the distribution files in a dist subdirectory by default. python setup.py sdist -d $TMP (or the %something% equivalent environment variable for Windows) can be used to put the file in a temporary directory instead, so that they’re wiped out at the next boot.
If you really care about the build dir, try this: python setup.py build -b $TMP sdist -d $TMP. sdist should be clever enough to find the files created by build.
distutils docs: http://docs.python.org/distutils
command help: python setup.py build --help
Log into PyPI, and click on your package in the gray box in the upper right corner of the screen. Click "files" in the list to the right of the appropriate version. That will load a new page. Check off the files you want to delete and click the gray "Remove" button.
I'm trying to include some data files in a python package using the setuptools package_data option. I'm then accessing the files with pkg_resources. This works perfectly when the python .egg file is installed as-is (i.e. still zipped). But when the egg file is unzipped during installation, the data files are not installed.
In other words, if I run:
python setup.py bdist_egg
cd dist
sudo easy_install -z EnrichPy-0.1.001-py2.6.egg
then the egg file is installed (with the data safely zipped inside) and everything works.
On the other hand, if I run
sudo easy_install -Z EnrichPy-0.1.001-py2.6.egg
then the data files are not installed. I have a directory called
EnrichPy-0.1.001-py2.6.egg/enrichpy/ under dist-packages, but it contains only my source files, not my data files.
Can anyone suggest what I need to do to get the package_data files to be installed when easy_install unzips the egg file?
Notes:
The package is available at http://github.com/roban/EnrichPy
I can test it by running:
import enrichpy.yields
enrichpy.yields.Data_vdHG().data
If that exits without errors, then pkg_resources is finding the installed data.
Problem solved, thanks to help from P.J. Eby on the distutils-sig email list:
http://mail.python.org/pipermail/distutils-sig/2010-November/017054.html
Just needed to rename files to avoid the '..' string.