I have the current 'noise' package 2.2 in python3.8
It appears to have a circular import problem;
I do not believe I have any similarly named files in my own folders.
My code:
import noise
Result>
import noise
File "C:\Python\Python38\lib\site-packages\noise\__init__.py", line 12, in
from . import _perlin, _simplex
ImportError: cannot import name '_perlin' from partially initialized module 'noise' (most likely due to a circular import) (C:\Python\Python38\lib\site-packages\noise\__init__.py)
From a Github issue regarding this problem:
What's happening is that Python prefers to look for modules in relative paths before absolute paths. Since you can import noise from the repo directory, that means it's probably already in your python path(an absolute path).
Say this repository lives in foo/noise/ and now you are in foo/ if you go up a directory and try to run import noise. Python will see that the local directory noise/ as a module because it has an init.py file and try to import the directory instead of the module installed in your python path. Since this module requires the _noise and _simplex C extensions to be compiled, this wont work.
I'd recommend removing or renaming this repo directory from your project and use pip to install noise if you haven't already. (pip install noise)
If you really want to install from source and not install into your system, then, in the noise/ repository, run python setup.py build to build locally and grab dist/lib*/noise/ and put that directory into your project directory.
Alternatively use python setup.py install in the noise/ repository to install from source into your Python path.
Related
This question already has answers here:
Relative imports for the billionth time
(12 answers)
Closed last year.
So basicly I want to acces a created module from a folder on the parent directory from the folder I am
Currently I'm at Twitter.py and I want to access /utils/magic_eden.py
On the __init__.py file I've got:
from .magic_eden import MagicEden
from .open_sea import OpenSea
from .tools import Tools
Now inside the Twitter.py file im trying to import these classes of the module by doing:
from utils import MagicEden
But im getting ModuleNotFoundError: No module named 'utils'.
I've tried so many sort of things but none worked. What should I do?
(btw if I execute the __init__.py file I get ImportError: attempted relative import with no known parent package)
From what I see, it seems that utils is a utility package logically separate from the code in Twitter.py. Given this, what you want is to cause your utils package to be on your Python search path (sys.path) so that you can import that package as a separate entity (no relative paths). If you don't want to configure your environment to place utils on the Python search path, you can do it in your Twitter.py file. Here's how that looks:
import os
import sys
here = os.path.dirname(__file__)
sys.path.append(os.path.join(here, '..'))
import utils
utils.MagicEden.hello()
utils.OpenSea.hello()
utils.Tools.hello()
The first line of code computes the full path to the directory containing Twitter.py. The second line then computes the path to the parent directory of that directory and adds that path to sys.path, the Python search path. After doing this, import utils will work, giving you access to that package and everything imported in that package's __init__.py file.
I created three small files for magic_eden.py, open_sea.py, and tools.py, each containing something that looks like this:
class MagicEden:
#staticmethod
def hello():
print("Hello from MagicEden!")
I can then run Twitter.py, and I get the following result with no additional configuration:
Hello from MagicEden!
Hello from OpenSea!
Hello from Tools!
There's nothing wrong with using the above solution during early development. But you will likely at some point want to remove the code that is hacking sys.path and instead install your module in a more official way. There's a way to do this from the start so that you never have to change code on either side when you want to install your module in the official way...
What I do in situations like this is create a setup.py file in my package that lets me build it as an installable package. There are many docs and tutorials on the net that explain how to do this. Here's just one of them: https://python-packaging-tutorial.readthedocs.io/en/latest/setup_py.html
Once you've done this, what you can do is install your package in "development mode" (pip install -e <package file>). What this does is install the package so that your system can find it (adds it to sys.path as a package), but installs links to the original sources rather than installing copies of them. In this mode, when you make changes to that package's source files, the changes take immediate effect in the installed module. So now you have the best of both worlds. Your package is installed in the official way such that you don't need to do anything special in the code that imports that package (no need to modify sys.path), and yet you can still make changes directly to the sources for that package and not have to keep reinstalling the package to see those changes take affect.
When you're done messing with a package in this way, you can reinstall it in the regular mode (pip install <package file>) and isolate changes to the sources for that package from uses of the package elsewhere on your system. At this point, you need to rebuild and reinstall the package to see any changes you've made to its sources.
You're attempting to import from a "sibling" folder.
To do this, you'll need to add __init__.py to your Twitter/ and parent src/ folders.
Then you'll need to import the path as Twitter.py doesn't know about any parent structure in this setup. You can then import from the utils module which is in the path.
import sys
sys.path.append('..')
from utils.magic_eden import MagicEden
assuming MagicEden is a class in your magic_eden.py file.
Can I import a python module from a distant folder? (Possible duplicate of How to import a Python module from a sibling folder?)
In general, the git repo should have a requirements.txt if it is for general use. If it has one then you can run pip install requirements.txt
It is also fairly easy for the repo owner to generate this file. At the root of their project they can run pip freeze > requirements.txt. pip freeze lists all current dependencies and so this command outputs the result to requirements.txt.
As for your second point, it really depends on the package structure. If they want to expose the code in the package they may have imported it in one of the top level __init__.py files. Otherwise, you can always directly import by following the paths.
For example if your structure is:
project
folder
subfolder
module
And module has a function called foo then you can import foo like so: from project.folder.subfolder.module import foo. Of course this assumes each of these directories has its own __init__.py file
I could suggest places to look for packages using
import sys
sys.path.append('/path/to/your/module/address/')
Source: Import a file from another directory
I am using Python 3.6.5, installed via miniconda. My issue is arising from the fact that I'm pip installing a package that has the same namespace as a local package. After pip installing this package, I can no longer import from the local package. I receive a ModuleNotFoundError error. The namespaces need to stay this way, if possible.
Here is my directory structure:
/root
stuff
- __init__.py
- my_stuff.py
app.py
init.py
__import__('pkg_resources').declare_namespace(__name__)
app.py
from stuff.my_stuff import my_fun
This works fine until I pip install the package with the same namespace, "stuff". After pip installing the package, the import statement, from stuff.my_stuff import my_fun throws the following error: ModuleNotFoundError: No module named 'stuff.my_stuff'. I kind of understand why. When importing modules in Python, it will look for built-in modules first, then sys.path, PYTHONPATH etc...
Here's the part thats really confusing me. If I create another arbitrary local module, like some_stuff, as shown below:
/root
stuff
- __init__.py
- my_stuff.py
some_stuff
- __init__.py
- more_stuff.py
app.py
and if I then run:
app.py
from some_stuff.more_stuff import more_fun
from stuff.my_stuff import my_fun
Everything works as expected. i.e. if I import some_stuff.more_stuff before stuff.my_stuff, everything works. But not vice versa. Solely importing stuff.my_stuff causes the ModuleNotFoundError.
app.py
# The code above works, but this causes the error
from stuff.my_stuff import my_fun
What is causing this behaviour? How can I solve this issue of locally referencing a package with the same namespace as one that was pip installed?
Edit:
I continued experimenting and noticed that when I remove all __init__.py files, everything works as expected. I came across this post: Since Python 3.3, a folder without an __init__.py can be considered part of an implicit namespace package. I'm still confused about the behaviour mentioned above though.
This SO question should answer your question
I am still putting the original answer here for convenience.
It's not possible to change "import path" (installed name) by specifying arguments to pip. You can, however, make some changes to the package after installing:
use pip install -e git+http://some_url#egg=some-name that way even if both packages have the same import path, they will be saved under different directories (using some-name provided after #egg=). After this you can go to the source directories of packages (usually venv/src/some-name) and rename some folders to change import paths
fork the repository, make changes, then install the package from that repository. Or you can publish your package on PyPI using different name and install it by that name
use pip download to put one of the packages in your project, then rename folders as you like
I was getting an error when I wanted to install a certain Python package in a specific folder (using pip install -t), and then import a module from that package.
I posted the issue on the package's Github and it turns out I can't install a package in a certain folder; that it isn't supported for the package to be installed that way.
Even without google collab, I can reproduce the import error when trying to install sklearn in some specific folder with pip install -t sklearnFolder then importing it as sklearnFolder.sklearn.manifold.
That is not a supported way of installing / using scikit-lean. One reason why it wouldn't work is that a few modules in scikit-learn use absolute imports (e.g. from sklearn import something) which will fail with such setup.
You should either install it with pip install or if you want to have it in some specific folder, clone the repo to that folder, then run pip install -e , in both cases it will be imported as sklearn.
From https://github.com/scikit-learn/scikit-learn/issues/11656
I don't quite get the explanation.
I thought something like
from folderName.package import module
is the same as
from package import module
Because they're both absolute imports. As in, they both completely specify the path of the imported module. So there is something off about my understanding but I don't know what it is.
In an import you do not specify the folderName to prefix a package. If the package is installed or in the python path, then you just use the package name to import.
# Assume the below structure is under a directory (folder) called /app/home/packages.
reservation/ This is your top-level pacakge
__init__.py Initialize the package
hotels/ Subpackage for hotel reservations
__init__.py
slots.py
bid.py
demand.py
...
restaurents/ Another Subpackage under hotels
__init__.py
cuisine.py
hours.py
tableslots.py
...
rewards/ Subpackage for rewards
__init__.py
points.py
discounts.py
membersonly.py
...
As the package is under /app/home/packages, then the following import is NOT valid as you prefix the folder name.
from packages.reservation import hotels
The correct way to import is from the actual package which has the package initialization __init__.py. If you see in the example, reservation folder has __init__.py.
from reservation import hotels
If you want to import the sub module under the hotels, then you will prefix with the package:
from reservation.hotels import restaurents
Alternatively you can import directly the sub module but you will have to prefix with the package when you use it:
import reservation.hotels.restaurents
I am building a python library, yet can't seem to solve this rather trivial import error. My folder structure is as follows:
./examples/somecode.py
./library/code/specific/__init__.py and other files for this submodule
my somecode.py looks something in the lines of
from library.code import specific
... do things with specific ...
I get specific not defined error.
but only if I am in the examples folder. Interestingly enought, the code works if I start the interpreter from the ./ folder.
What am I doing wrong?
EDIT:
the library is installed with python3 setup.py install