Change package name when running setup.py - python

I have forked a repo and now I have cloned it. When running the setup.py file inside, Python installs the package inside site-packages as an obscure name, that of which importing this within a Python file will not be viable.
For example, I fork and clone a repo called foo. I can also see this in the setup.py file:
setup(
name='foo',
version='3.3.0-rc6',
packages=find_packages('src'),
package_dir={'': 'src'},
include_package_data=True,
When I run python setup.py install, I find the package has been installed as foo-3.3.0rc6-py3.6.egg. I do not want to have to import the package as this name in every one of my projects utilizing it.
How can I just change the package name to foo (when running/installing via setup.py), so that I can run import foo and not import foo-3.3...?
I feel I can not just rename it, as if I wanted other users to clone the repo and not have to go through the same trouble as me. Is the package name embedded somewhere in the setup.py folder?
Let me know if you need anything else, I'm willing to have this issue resolved.

You don't have to import foo-3.3; actually you cannot import as it's SyntaxError.
You don't have to import foo-3.3 from foo-3.3.0rc6-py3.6.egg because distutils/setuptools configured correct import path for eggs. Look into easy-install.pth file and you find there ./foo-3.3.0rc6-py3.6.egg. Run python and verify sys.path — there have to be foo-3.3.0rc6-py3.6.egg entry so that import foo works.

That's just the name of the egg, and you needn't worry about it. Python knows where to look for the package, so when you do:
import foo
...it'll do the right thing.

Related

Is it possible to encapsulate sub-packages within a root level package when using setuptools

I reviewed this, this question before asking this.
My project (not created by me) has the following layout:
root_folder
- module1
- module2
- module3
- other files and folders needed for CICD
- setup.py
I'd like to package this up and use via editable install. However, setuptools installs each of the individual modules as separate packages, leading me to use them as import module1 etc. However, I'd like to use as modules under a single package as import <root_pkg>.module2 etc.
Since this project is enormous and has a number of pipelines and automation built around it, modifying the folder structure is risky and ill-advised.
The hacky solution I came up with is jump one folder up when executing the setup.py script as follows:
def _jump_one_level_up():
import os
os.chdir('..')
return 'ver_string'
setup(
name="roo_folder",
version=_jump_one_level_up(),
packages=find_packages()
)
Setup ends up calling _jump_one_level_up() to get the version and editable install works.
Now, is there a better way to do this?

Why do I get an import error when a child file is trying to access a parent file

My directory is structured like this
>project
>tests
>test_to_json.py
>app.py
>x.db
in my test_to_json.py I want to access a class from app.py
How would I import it? I've tried from ..app import myClass but that gives me ImportError: attempted relative import with no known parent package. Is there something I'm missing?
You cannot use .. relative pathing in python. That specific type of relative python is simply not allowed. . is allowed, though. The resolution for this problem is usually done by converting your project into a python package.
Extensive tutorials for doing so can be found here, but I will give an example of how to convert your project into a package.
Step 1
The new file structure should look like this:
>project
>tests
>__init__.py #Note this file
>test_to_json.py
>__init__.py #Note this file
>setup.py #Note this file
>app.py
>x.db
Step 2
Write your setup.py.
Here is an generic setup.py that should work for your project:
from setuptools import setup, find_packages
setup(
name='project_package', #the name of your desired import
version='0.0.1',
author='An Awesome Coder',
packages=find_packages(),
description='An awesome package that does something',
install_requires=[], # a list of python dependencies for your package
)
find_packages() looks for all the __init__.py files in your package to identify submodules.
Step 3
Install your package. In the folder with your new setup.py, run pip install -e . This will install your package on your computer, basically adding the files to your python system path.
Step 4
From any python terminal on your computer you should now be able to import your package using the package_name you specified.
import project_package
from project_package.app import myClass
myClass()
.
.
.

Pytest add a new file under src and get the tests to see it

A newb where pytest is concerned.
I set up my structure: my_proj\src with the app files under src, and my_proj\tests with the test files under there. Following this book, I then added files like my_proj\setup.py, which looks like this currently:
from setuptools import setup, find_packages
setup(
name='my_proj',
version='0.1.0',
license='proprietary',
...
packages=find_packages(where='src'),
package_dir={'': 'src'},
install_requires=[],
extras_require={},
entry_points={
'console_scripts': [
'py_lib = py_lib.cli:py_lib_cli',
]
},
)
Then, as per the book, I ran
> pip install -e .
... and by some magic beyond my understanding this then set up things so that under the my_proj\tests side of things, the test files were able to import .py files from under my_proj\src.
Then I wanted to add a new .py file to the src files, i.e. the application files, the files under my_proj\src. But the tests are completely unable to see it: "No module named 'xxx'".
I tried pip install -e . again. I also tried pip develop ., and I tried the --no-cache-dir flag. I even tried running setup.py directly, which apparently is not recommended.
Then I found that under my_proj\src there is a directory which has been generated, my_proj.egg-info. One of the files under there is SOURCES.txt, which appears to list all the files in the project. My new .py file under \src is indeed listed there. And yet the test framework apparently can't see it.
Finally I tried uninstalling the thing:
pip uninstall my_proj
...
Proceed (y/n)? y
...
and reinstalling
pip install -e .
Tests still can't see (i.e. import) the new file. Grrr.
Can anyone explain this and tell me how to resolve the problem?
Edit
In answer to the comment: I added a new file, under src\utils: new_file.py, like this:
# -*- coding: utf-8 -*-
conc_fh = False
line_of_stars=50*'*' + '\n'
The test file is my_proj\tests\unit\test_misc_utils.py. I am usually running pytest from my_proj\tests\unit, although I have also tried the parent and grandparent directories. In that file this is the import line:
import utils.new_file
Same fail (for the new file). Then I tried all those possible remedies again.
I also tried deleting things like the my_proj.egg-info directory, and the my_proj\tests.pytest_cache and my_proj\tests\__pycache__ directories.
I can only conclude (provisionally) that something under site-packages in the virtual environment is incapable of updating correctly.
I have even tried pip uninstall pytest and reinstall, followed by pip install -e . again. Even then: NO!
Can someone confirm that adding a file works without problems for them (ideally on a W10 OS)?
NB this is the fail output:
______________________________________ ERROR collecting unit/test_misc_utils.py _______________________________________
ImportError while importing test module 'D:\My documents\software projects\EclipseWorkspace\my_proj\tests\unit\test_misc_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
c:\users\mike\appdata\local\programs\python\python39\lib\importlib\__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests\unit\test_misc_utils.py:3: in <module>
import utils.new_file
E ModuleNotFoundError: No module named 'utils.new_file'
Later
I completely created a new project, by copying the old one, but setting up an entirely new virtual environment.
I then installed pytest again, and again ran pip install -e ., and then pytest tests/unit from the project directory.
To my utter amazement, once again, these two new files, the ones recently created (and copied in the process of making a new project), and only them, failed to be recognised and could not be imported.
Now I am totally baffled. It would appear that there is something about the nature of these files which is "defective". I've tried looking at the lines in them, for things like spurious Unicode characters. Nothing so far...
Think I finally worked it out.
The question is: what is available (for import) to the modules and files under "my_proj/tests". I believe this is determined by setup.py (as outlined in the referenced book). The most bare-boned version of this is:
from setuptools import setup
setup(
name='my_proj2',
py_modules=['my_proj2']
)
When you then run pip install -e . (note dot), this means that modules under my_proj2/src become available for import. But perhaps not everything. The downloadable files (again for the book) include a module [root]/src/tasks ... and under that is an __init__.py, which explicitly imports various variables and classes from that module, for re-export to the test classes.
This approach (including anything you want to import into tests in __init__.py of an application module) is not the only way of accessing stuff in modules under [root]/src when running tests. Any valid path under [root]/src appears to work. I have no idea why it failed on my previous attempts. Maybe the previous setup.py file (tweaked from the downloaded version) contained something complicated which interfered with things.

pip install bug with `-e` flag and `setuptools.setup(package_dir=...)` parameter?

I have what I think is a pip bug, and I want to double-check that it's not actually a mistake of mine before submitting it as a formal issue. If it's not a bug, I'd appreciate an explanation of what I'm doing wrong.
I have a project structure like so:
project/
setup.py
project_src/
__init__.py
...
common_utils/
utils_src/
__init__.py
...
I want to be able to:
import code from "project/project_src" via import project_src (this isn't the issue, I just want to be comprehensive)
import code from "project/common_utils/utils_src" via import utils_src (note this strips the "common_utils" folder from the package path name)
In order to do this, the root-level "setup.py" looks something like this (abbreviated):
# setup.py
import setuptools
setuptools.setup(
...,
packages=['project_src', 'utils_src'],
package_dir={
'project_src': 'project_src',
'utils_src': 'common_utils/utils_src',
},
...,
)
Now here's my issue. When I then install this package locally via CL as pip install project/, I can then open an interpreter and successfully run import project_src and import utils_src. But if I install via pip install -e project/, import project_src works but import utils_src triggers a ModuleNotFoundError error. (This is a huge pain as I rely on using the -e flag for development.)
Again, please let me know if this appears to be a bug, or if this is a mistake on my part.
Not a bug in pip, your mistake. You want to use common_utils/ directory as a parent dir for a package but you want utils_src as a package inside it. So change your setup.py:
package_dir={
'project_src': 'project_src',
'utils_src': 'common_utils',
},
With this utils_src will be installed as a top-level package so you can do
import utils_src
PS. They say this doesn't work with
pip install -e
I didn't test the rumor yet.
Turns out this is a long-standing issue: pypa/setuptools #230: develop mode does not respect src structure
Thanks #sinoroc for the hint # comments on this answer

What does an import statement with "..." in python mean? [duplicate]

I want to inherit from a class in a file that lies in a directory above the current one.
Is it possible to relatively import that file?
from ..subpkg2 import mod
Per the Python docs: When inside a package hierarchy, use two dots, as the import statement doc says:
When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute from . import mod from a module in the pkg package then you will end up importing pkg.mod. If you execute from ..subpkg2 import mod from within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification for relative imports is contained within PEP 328.
PEP 328 deals with absolute/relative imports.
import sys
sys.path.append("..") # Adds higher directory to python modules path.
#gimel's answer is correct if you can guarantee the package hierarchy he mentions. If you can't -- if your real need is as you expressed it, exclusively tied to directories and without any necessary relationship to packaging -- then you need to work on __file__ to find out the parent directory (a couple of os.path.dirname calls will do;-), then (if that directory is not already on sys.path) prepend temporarily insert said dir at the very start of sys.path, __import__, remove said dir again -- messy work indeed, but, "when you must, you must" (and Pyhon strives to never stop the programmer from doing what must be done -- just like the ISO C standard says in the "Spirit of C" section in its preface!-).
Here is an example that may work for you:
import sys
import os.path
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
import module_in_parent_dir
Import module from a directory which is exactly one level above the current directory:
from .. import module
How to load a module that is a directory up
preface: I did a substantial rewrite of a previous answer with the hopes of helping ease people into python's ecosystem, and hopefully give everyone the best change of success with python's import system.
This will cover relative imports within a package, which I think is the most probable case to OP's question.
Python is a modular system
This is why we write import foo to load a module "foo" from the root namespace, instead of writing:
foo = dict(); # please avoid doing this
with open(os.path.join(os.path.dirname(__file__), '../foo.py') as foo_fh: # please avoid doing this
exec(compile(foo_fh.read(), 'foo.py', 'exec'), foo) # please avoid doing this
Python isn't coupled to a file-system
This is why we can embed python in environment where there isn't a defacto filesystem without providing a virtual one, such as Jython.
Being decoupled from a filesystem lets imports be flexible, this design allows for things like imports from archive/zip files, import singletons, bytecode caching, cffi extensions, even remote code definition loading.
So if imports are not coupled to a filesystem what does "one directory up" mean? We have to pick out some heuristics but we can do that, for example when working within a package, some heuristics have already been defined that makes relative imports like .foo and ..foo work within the same package. Cool!
If you sincerely want to couple your source code loading patterns to a filesystem, you can do that. You'll have to choose your own heuristics, and use some kind of importing machinery, I recommend importlib
Python's importlib example looks something like so:
import importlib.util
import sys
# For illustrative purposes.
file_path = os.path.join(os.path.dirname(__file__), '../foo.py')
module_name = 'foo'
foo_spec = importlib.util.spec_from_file_location(module_name, file_path)
# foo_spec is a ModuleSpec specifying a SourceFileLoader
foo_module = importlib.util.module_from_spec(foo_spec)
sys.modules[module_name] = foo_module
foo_spec.loader.exec_module(foo_module)
foo = sys.modules[module_name]
# foo is the sys.modules['foo'] singleton
Packaging
There is a great example project available officially here: https://github.com/pypa/sampleproject
A python package is a collection of information about your source code, that can inform other tools how to copy your source code to other computers, and how to integrate your source code into that system's path so that import foo works for other computers (regardless of interpreter, host operating system, etc)
Directory Structure
Lets have a package name foo, in some directory (preferably an empty directory).
some_directory/
foo.py # `if __name__ == "__main__":` lives here
My preference is to create setup.py as sibling to foo.py, because it makes writing the setup.py file simpler, however you can write configuration to change/redirect everything setuptools does by default if you like; for example putting foo.py under a "src/" directory is somewhat popular, not covered here.
some_directory/
foo.py
setup.py
.
#!/usr/bin/env python3
# setup.py
import setuptools
setuptools.setup(
name="foo",
...
py_modules=['foo'],
)
.
python3 -m pip install --editable ./ # or path/to/some_directory/
"editable" aka -e will yet-again redirect the importing machinery to load the source files in this directory, instead copying the current exact files to the installing-environment's library. This can also cause behavioral differences on a developer's machine, be sure to test your code!
There are tools other than pip, however I'd recommend pip be the introductory one :)
I also like to make foo a "package" (a directory containing __init__.py) instead of a module (a single ".py" file), both "packages" and "modules" can be loaded into the root namespace, modules allow for nested namespaces, which is helpful if we want to have a "relative one directory up" import.
some_directory/
foo/
__init__.py
setup.py
.
#!/usr/bin/env python3
# setup.py
import setuptools
setuptools.setup(
name="foo",
...
packages=['foo'],
)
I also like to make a foo/__main__.py, this allows python to execute the package as a module, eg python3 -m foo will execute foo/__main__.py as __main__.
some_directory/
foo/
__init__.py
__main__.py # `if __name__ == "__main__":` lives here, `def main():` too!
setup.py
.
#!/usr/bin/env python3
# setup.py
import setuptools
setuptools.setup(
name="foo",
...
packages=['foo'],
...
entry_points={
'console_scripts': [
# "foo" will be added to the installing-environment's text mode shell, eg `bash -c foo`
'foo=foo.__main__:main',
]
},
)
Lets flesh this out with some more modules:
Basically, you can have a directory structure like so:
some_directory/
bar.py # `import bar`
foo/
__init__.py # `import foo`
__main__.py
baz.py # `import foo.baz
spam/
__init__.py # `import foo.spam`
eggs.py # `import foo.spam.eggs`
setup.py
setup.py conventionally holds metadata information about the source code within, such as:
what dependencies are needed to install named "install_requires"
what name should be used for package management (install/uninstall "name"), I suggest this match your primary python package name in our case foo, though substituting underscores for hyphens is popular
licensing information
maturity tags (alpha/beta/etc),
audience tags (for developers, for machine learning, etc),
single-page documentation content (like a README),
shell names (names you type at user shell like bash, or names you find in a graphical user shell like a start menu),
a list of python modules this package will install (and uninstall)
a defacto "run tests" entry point python ./setup.py test
Its very expansive, it can even compile c extensions on the fly if a source module is being installed on a development machine. For a every-day example I recommend the PYPA Sample Repository's setup.py
If you are releasing a build artifact, eg a copy of the code that is meant to run nearly identical computers, a requirements.txt file is a popular way to snapshot exact dependency information, where "install_requires" is a good way to capture minimum and maximum compatible versions. However, given that the target machines are nearly identical anyway, I highly recommend creating a tarball of an entire python prefix. This can be tricky, too detailed to get into here. Check out pip install's --target option, or virtualenv aka venv for leads.
back to the example
how to import a file one directory up:
From foo/spam/eggs.py, if we wanted code from foo/baz we could ask for it by its absolute namespace:
import foo.baz
If we wanted to reserve capability to move eggs.py into some other directory in the future with some other relative baz implementation, we could use a relative import like:
import ..baz
Here's a three-step, somewhat minimalist version of ThorSummoner's answer for the sake of clarity. It doesn't quite do what I want (I'll explain at the bottom), but it works okay.
Step 1: Make directory and setup.py
filepath_to/project_name/
setup.py
In setup.py, write:
import setuptools
setuptools.setup(name='project_name')
Step 2: Install this directory as a package
Run this code in console:
python -m pip install --editable filepath_to/project_name
Instead of python, you may need to use python3 or something, depending on how your python is installed. Also, you can use -e instead of --editable.
Now, your directory will look more or less like this. I don't know what the egg stuff is.
filepath_to/project_name/
setup.py
test_3.egg-info/
dependency_links.txt
PKG-INFO
SOURCES.txt
top_level.txt
This folder is considered a python package and you can import from files in this parent directory even if you're writing a script anywhere else on your computer.
Step 3. Import from above
Let's say you make two files, one in your project's main directory and another in a sub directory. It'll look like this:
filepath_to/project_name/
top_level_file.py
subdirectory/
subfile.py
setup.py |
test_3.egg-info/ |----- Ignore these guys
... |
Now, if top_level_file.py looks like this:
x = 1
Then I can import it from subfile.py, or really any other file anywhere else on your computer.
# subfile.py OR some_other_python_file_somewhere_else.py
import random # This is a standard package that can be imported anywhere.
import top_level_file # Now, top_level_file.py works similarly.
print(top_level_file.x)
This is different than what I was looking for: I hoped python had a one-line way to import from a file above. Instead, I have to treat the script like a module, do a bunch of boilerplate, and install it globally for the entire python installation to have access to it. It's overkill. If anyone has a simpler method than doesn't involve the above process or importlib shenanigans, please let me know.
Polished answer of #alex-martelli with pathlib:
import pathlib
import sys
_parentdir = pathlib.Path(__file__).parent.parent.resolve()
sys.path.insert(0, str(_parentdir))
import module_in_parent_dir
sys.path.remove(str(_parentdir))
To run python /myprogram/submodule/mymodule.py which imports /myprogram/mainmodule.py, e.g., via
from mainmodule import *
on Linux (e.g., in the python Docker image), I had to add the program root directory to PYTHONPATH:
export PYTHONPATH=/myprogram
It is 2022 and none of the answers really worked for me. Here is what worked in the end
import sys
sys.path.append('../my_class')
import my_class
My directory structure:
src
--my_class.py
notebooks
-- mynotebook.ipynb
I imported my_class from mynotebook.ipynb.
You can use the sys.path.append() method to add the directory containing the package to the list of paths searched for modules. For example, if the package is located two directories above the current directory, you can use the following code:
import sys
sys.path.append("../../")
if the package is location one directory above the current directory, you can use below code:
import sys
sys.path.append("..")
Python is a modular system
Python doesn't rely on a file system
To load python code reliably, have that code in a module, and that module installed in python's library.
Installed modules can always be loaded from the top level namespace with import <name>
There is a great sample project available officially here: https://github.com/pypa/sampleproject
Basically, you can have a directory structure like so:
the_foo_project/
setup.py
bar.py # `import bar`
foo/
__init__.py # `import foo`
baz.py # `import foo.baz`
faz/ # `import foo.faz`
__init__.py
daz.py # `import foo.faz.daz` ... etc.
.
Be sure to declare your setuptools.setup() in setup.py,
official example: https://github.com/pypa/sampleproject/blob/master/setup.py
In our case we probably want to export bar.py and foo/__init__.py, my brief example:
setup.py
#!/usr/bin/env python3
import setuptools
setuptools.setup(
...
py_modules=['bar'],
packages=['foo'],
...
entry_points={},
# Note, any changes to your setup.py, like adding to `packages`, or
# changing `entry_points` will require the module to be reinstalled;
# `python3 -m pip install --upgrade --editable ./the_foo_project
)
.
Now we can install our module into the python library;
with pip, you can install the_foo_project into your python library in edit mode,
so we can work on it in real time
python3 -m pip install --editable=./the_foo_project
# if you get a permission error, you can always use
# `pip ... --user` to install in your user python library
.
Now from any python context, we can load our shared py_modules and packages
foo_script.py
#!/usr/bin/env python3
import bar
import foo
print(dir(bar))
print(dir(foo))

Categories