How do I install/import module to python on mac - python

I haven't used python since my undergrad days so I'm incredibly rusty. I'm trying to install CERN's ROOT module. It's already installed on the mac terminal but I wish to access it in python too. I have it saved in site-packages which I have checked is the correct directory where all of my other accessible modules: numpy, scipy, matplotlib etc are housed.
Folder is called rootpy and includes the __init__.py file along with the ROOT.py and ROOT.pyc files, and other files, but when I try to import it under name rootpy or ROOT I receive the error:
ImportError: No module named rootpy
or,
ImportError: No module named ROOT
Does anybody know how to import the package?

Related

How to resolve Python Module Not Found Error?

I am facing a problem in importing modules in python. I looked for a solution and found this. but this did not worked either.
My Directory is as follows
->MyScrapper --->MyScrapper ----->db_connection.py --->Video_Scrapper -----> video_scrapper.py --->Blogs_Scrapper -----> blogs_scrapper.py
I want to import db_connection.py in video_scrapper.py as well as blogs_scrapper.py. I have tried to import it as from MyScrapper.db_connection import DBConnection. It throws a ModuleNotFoundError: No Module named 'MyScrapper'. I have also tried using from db_connection import DBConnection and import DBConnection but none of them worked.
Please Help!!
When importing a module in python, it will search the module in this order:
build-in module
script in the current directory
PATHPYTHON
installation-dependent default
So, you need to add the MyScrapper module to the search path. If you use a virtual environment, you can add your project root directory into the PYTHONPATH, by modify the PYTHONPATH in the Scripts\activate.bat file, like this:
set PYTHONPATH=%VIRTUAL_ENV%\src;%PYTHONPATH%
I use the virtual environment, and my project struct is:
|--Include
|--Lib
|--Scripts
|--src
My source files are in the src directory.

Import package from parent folder [duplicate]

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.

cannot import from partially initialized module 'noise'

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.

ModuleNotFoundError: No module named 'libs.strings'

This is the error I am facing right, I google it a lot but solutions did not work, I am using python 3.7 in my django application.
Code:
from libs.strings import *
Error:
ModuleNotFoundError: No module named 'libs.strings'
As I have checked the libs module from pypi. The libs module doesn't seem to have strings. So doing pip install libs wont solve your problem.
Maybe libs could be a directory in your project you are trying to access. If so, then:
Check Proper path for libs folder.
In order to define libs as a single package, add empty __init__.py file in each folder; so the whole directory system act as a python module.

Python library path

I have a python file "testHTTPAuth.py" which uses module deliciousapi and is kept in "deliciousapi.py".
I have kept the files like
testHTTPAuth.py
lib
deliciousapi.py
But when i run: "python testHTTPAuth.py" it's giving error
import deliciousapi
ImportError: No module named deliciousapi
How can handle these python libraries? Because later I have put the code together with libraries as Google app. So I can't keep the library in normal library path.
You need to add the 'lib' directory to your path - otherwise, Python can't find your source. The following (included in a module such as testHTTPAuth.py) will do that:
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib')
Ned's suggestion of changing your imports may work, but if anything in the lib directory imports submodules with absolute paths (most large modules do this), then it'll break.
If you add an empty __init__.py to your lib directory, you can change your import statement to:
from lib import deliciousapi

Categories

Resources