Python Package Module Not Found SVN Import - python

Imported the SVN Package, going to the following PYTHONPATH confirmed by import sys >>> sys.path:
\ProgramData\Anaconda2\lib\site-packages
This is where all packages are stored and other packages within this directory will import appropriately in Py.
When I open Py in Terminal, and Import SVN, it is producing an import error, No Module Name. Tested this in Terminal, IPython, Bash, Jupyter Notebook.
The only thing I can ascertain is this is an issue perhaps with a third party library...out of ideas at this point. There does not appear to be another option for a SVN module, need this for script to grep SVN. If there's another way to do this without this package, welcome other ideas as well.
Thank you in advance for any help or ideas.

Related

Module import error even though sys.path shows the directory?

I am having trouble installing a package through its directory (not possible to install through pip, only folder available) on my mac.
I am using a python 2.7 environment on Conda and did the following after reading many posts and tutorials:
decompressed the file with the modules I need ("/Users/personal/python_files/PyOPC-0.1/PyOPC")
used the command conda develop "path/to/PyOPC" on the terminal
added the following line to the bottom of the ~/.bash_profile file: export PYTHONPATH="/path/to/PyOPC"
checked my sys.path through python2 -c "import sys; print('\n'.join(sys.path))" on the terminal
When I do the latter, I can see the path to PyOPC listed there, so I thought I wouldn't have problems importing any modules.
Nonetheless, when I run my code I get the following:
from basic import BasicXDAServer
ImportError: No module named basic
basic.py is a file inside the folder PyOPC/servers. If I move "basic" to the main PyOPC folder, I get a different import error referring to an import happening within basic.py...
Here is the complete directory I am referring to available on Github: https://github.com/ibh-systems/pyopc/tree/master/PyOPC
Is there a problem with the structure of the directory itself or did I do something wrong as I installed it. Thanks for the help.

Import does not look into pythonpath when importing personal modules

I have build my own package named "XXX" on my computer by setting a __init__.py file in the C:\path\to\module\XXX folder, which contain my python script. Then I added C:\path\to\module\XXX to my pythonpath manually. When I run python on a cmd prompt and try to import the module it fails. When I print sys.path, it contains C:\path\to\module\XXX. If I try to load some other personnal modules, the same problem occures but when I try to load module not of my own, installed with pip (i.e. numpy) it works perfectly. So my guess is that for some reason python is not looking into my pythonpath anymore to load module.
I am using anaconda with python 3.6.5 with a vitual environnement on windows 7. Python is installed with anaconda via conda-forge repo.
I have tested this issue with multiple approach, first by running the script below with Pycharm and then with Spider. Then I used anaconda prompt to run the commands manualy activating my virtual environnement first. Finally I ran the commands on a DOS prompt with python. Whatever the way, the results where the same. This error just occured after installing fbprohete in my virtual environnement. Before that I was able to import my modules. I tried to remove that module but the problem remains the same. A lot of mess append during the installation of fbprohet. After the installation I ran a conda clean command to clean the environnement but it didn't help. I suspect that something is corrupted now but I would like to avoid reinstalling everything. If you have an idea where to look, which module in particular should be corrupted, I will be glad to know more about it.
import sys
sys.path
['', 'C:\path\to\module\XXX', ...]
import XXX
ModuleNotFoundError: No module named 'XXX'
Whith the script above, I expect the module to be loaded without error since the path to the package is in the pythonpath and the __init__.py is in the folder C:\path\to\module\XXX
Thanks!
So it was a bit silly actually. I should have replaced C:\path\to\module\XXX by C:\path\to\module in the pythonpath because __ini__.py is in the C:\path\to\module\XXX folder. Thanks Giacomo Alzetta for your comment.

Hidden Markov Models (HMM) in Python

I am working with Hidden Markov Models in Python. For that I came across a package/module named hmmpytk. The problem is hmmpytk isnt pre-installed and when I download the hmmpytk module, i only get codes without the installation file. I use windows operating system. If I run a code simply with "from hmmpytk import hmm_faster" I get an Import error. ..so no idea how i get started with hmmpytk.
You need to make sure that the folder hmmpytk (and possibly also lame_tagger) is "in the directory containing the script that was used to invoke the Python interpreter." See the documentation about the Python path sys.path
EDIT: Alternatively, you can make sure that those folders are on your Python path. To see the folders in your path, type import sys; sys.path - the CWD is the first entry in the path if you started the interactive interpreter.
You probably want PIP or easy_install to install python packages.

"ImportError: No module named" when trying to run Python script

I'm trying to run a script that launches, amongst other things, a python script. I get a ImportError: No module named ..., however, if I launch ipython and import the same module in the same way through the interpreter, the module is accepted.
What's going on, and how can I fix it? I've tried to understand how python uses PYTHONPATH but I'm thoroughly confused. Any help would greatly appreciated.
This issue arises due to the ways in which the command line IPython interpreter uses your current path vs. the way a separate process does (be it an IPython notebook, external process, etc). IPython will look for modules to import that are not only found in your sys.path, but also on your current working directory. When starting an interpreter from the command line, the current directory you're operating in is the same one you started ipython in. If you run
import os
os.getcwd()
you'll see this is true.
However, let's say you're using an ipython notebook, run os.getcwd() and your current working directory is instead the folder in which you told the notebook to operate from in your ipython_notebook_config.py file (typically using the c.NotebookManager.notebook_dir setting).
The solution is to provide the python interpreter with the path-to-your-module. The simplest solution is to append that path to your sys.path list. In your notebook, first try:
import sys
sys.path.append('my/path/to/module/folder')
import module_of_interest
If that doesn't work, you've got a different problem on your hands unrelated to path-to-import and you should provide more info about your problem.
The better (and more permanent) way to solve this is to set your PYTHONPATH, which provides the interpreter with additional directories look in for python packages/modules. Editing or setting the PYTHONPATH as a global var is os dependent, and is discussed in detail here for Unix or Windows.
Just create an empty python file with the name __init__.py under the folder which showing error, while you running the python project.
Make sure they are both using the same interpreter. This happened to me on Ubuntu:
$ ipython3 -c 'import sys; print(sys.version)'
3.4.2 (default, Jun 19 2015, 11:34:49) \n[GCC 4.9.1]
$ python3 -c 'import sys; print(sys.version)'
3.3.0 (default, Nov 27 2012, 12:11:06) \n[GCC 4.6.3]
And sys.path was different between the two interpreters. To fix it, I removed Python 3.3.
The main reason is the sys.paths of Python and IPython are different.
Please refer to lucypark link, the solution works in my case. It happen when install opencv by
conda install opencv
And got import error in iPython, There are three steps to solve this issue:
import cv2
ImportError: ...
1. Check path in Python and iPython with following command
import sys
sys.path
You will find different result from Python and Jupyter. Second step, just use sys.path.append to fix the missed path by try-and-error.
2. Temporary solution
In iPython:
import sys
sys.path.append('/home/osboxes/miniconda2/lib/python2.7/site-packages')
import cv2
the ImportError:.. issue solved
3. Permanent solution
Create an iPython profile and set initial append:
In bash shell:
ipython profile create
... CHECK the path prompted , and edit the prompted config file like my case
vi /home/osboxes/.ipython/profile_default/ipython_kernel_config.py
In vi, append to the file:
c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append("/home/osboxes/miniconda2/lib/python2.7/site-packages")'
]
DONE
Doing sys.path.append('my-path-to-module-folder') will work, but to avoid having to do this in IPython every time you want to use the module, you can add export PYTHONPATH="my-path-to-module-folder:$PYTHONPATH" to your ~/.bash_profile file.
This is how I fixed it:
import os
import sys
module_path = os.path.abspath(os.getcwd() + '\\..')
if module_path not in sys.path:
sys.path.append(module_path)
Before installing ipython, I installed modules through easy_install; say sudo easy_install mechanize.
After installing ipython, I had to re-run easy_install for ipython to recognize the modules.
If you are running it from command line, sometimes python interpreter is not aware of the path where to look for modules.
Below is the directory structure of my project:
/project/apps/..
/project/tests/..
I was running below command:
>> cd project
>> python tests/my_test.py
After running above command i got below error
no module named lib
lib was imported in my_test.py
i printed sys.path and figured out that path of project i am working on is not available in sys.path list
i added below code at the start of my script my_test.py .
import sys
import os
module_path = os.path.abspath(os.getcwd())
if module_path not in sys.path:
sys.path.append(module_path)
I am not sure if it is a good way of solving it but yeah it did work for me.
I have found that the solution to this problem was extensively documented here:
https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/
Basically, you must install the packages within the Jupyter environment, issuing shell commands like:
!{sys.executable} -m pip install numpy
Please check the above link for an authoritative full answer.
This kind of errors occurs most probably due to python version conflicts. For example, if your application runs only on python 3 and you got python 2 as well, then it's better to specify which version to use.
For example use
python3 .....
instead of
python
Had a similar problem, fixed it by calling python3 instead of python, my modules were in Python3.5.
This problem occurs due to the different versioning - e.g. if the Python installed on your machine is installed in a folder called path_to_lib/python3.6 but your notebook is running in Python 3 - the spacing in the naming matters!
How to solve it?
When creating a new jupyter notebook, just choose the Python with the same versioning as yours (watch out for spaces!). See the attached image.
I found yet another source of this discrepancy:
I have ipython installed both locally and in commonly in virtualenvs. My problem was that, inside a newly made virtualenv with ipython, the system ipython was picked up, which was a different version than the python and ipython in the virtualenv (a 2.7.x vs. a 3.5.x), and hilarity ensued.
I think the smart thing to do whenever installing something that will have a binary in yourvirtualenv/bin is to immediately run rehash or similar for whatever shell you are using so that the correct python/ipython gets picked up. (Gotta check if there are suitable pip post-install hooks...)
Solution without scripting:
Open Spyder -> Tools -> PYTHONPATH manager
Add Python paths by clicking "Add Path".
E.g: 'C:\Users\User\AppData\Local\Programs\Python\Python37\Lib\site-packages'
Click "Synchronize..." to allow other programs (e.g. Jupyter Notebook) use the pythonpaths set in step 2.
Restart Jupyter if it is open
This is probably caused by different python versions installed on your system, i.e. python2 or python3.
Run command $ pip --version and $ pip3 --version to check which pip is from at Python 3x. E.g. you should see version information like below:
pip 19.0.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
Then run the example.py script with below command
$ python3 example.py
Happened to me with the directory utils. I was trying to import this directory as:
from utils import somefile
utils is already a package in python. Just change your directory name to something different and it should work just fine.
This answer applies to this question if
You don't want to change your code
You don't want to change PYTHONPATH permanently
Temporarily modify PYTHONPATH
path below can be relative
PYTHONPATH=/path/to/dir python script.py
import sys
sys.path.append('/Users/{user}/Library/Python/3.7/lib/python/site-packages')
import ta
If anyone comes across this issue using conda with Jupyter Notebook in MSVS Code, the solution is to make sure you're using the correct kernel. The kernel is in a box in the top right corner of the interface and looks like this:
I pointed mine to the version of Python that also matched my application path -- problem solved!
This is what worked for me: I just changed my working directory inside my notebook
import os
os.chdir("my/path/to/module")
os.getcwd()
I have a similar issued with my Jupyter Lab setup which I resolved by checking the Jupyter Lab log on opening. This informed me that the virtual environment (pipenv) couldn't locate the Jupyter Lab so it used a shared version (from an earlier installation of Python).
I created a requirements.txt and discovered I hadn't installed Jupyter Lab in this new environment. Installing it resolved the import error.
Remove pathlib and reinstall it. Delete the pathlib in sitepackages folder and reinstall the pathlib package by using pip command:
pip install pathlib

Python error "ImportError: No module named"

Python is installed in a local directory.
My directory tree looks like this:
(local directory)/site-packages/toolkit/interface.py
My code is in here:
(local directory)/site-packages/toolkit/examples/mountain.py
To run the example, I write python mountain.py, and in the code I have:
from toolkit.interface import interface
And I get the error:
Traceback (most recent call last):
File "mountain.py", line 28, in ?
from toolkit.interface import interface
ImportError: No module named toolkit.interface
I have already checked sys.path and there I have the directory /site-packages. Also, I have the file __init__.py.bin in the toolkit folder to indicate to Python that this is a package. I also have a __init__.py.bin in the examples directory.
I do not know why Python cannot find the file when it is in sys.path. Any ideas? Can it be a permissions problem? Do I need some execution permission?
Based on your comments to orip's post, I guess this is what happened:
You edited __init__.py on windows.
The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
You used WinSCP to copy the file to your unix box.
WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
The missing __init__.py (now called __init__.py.bin) means python doesn't understand toolkit as a package.
You create __init__.py in the appropriate directory and everything works... ?
Does
(local directory)/site-packages/toolkit
have a __init__.py?
To make import walk through your directories every directory must have a __init__.py file.
I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:
(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)
Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the mountain.py file, and trying to call the toolkit.interface.py module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is the toolkit directory.
When you are in the toolkit directory, enter this line of code on your command line:
export PYTHONPATH=.
This sets your PYTHONPATH to ".", which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn't just look in your current directory, but in all the directories that are in your current directory).
After you've set your PYTHONPATH in the step above, run your module from your current directory (the toolkit directory). Python should now find and load the modules you specified.
On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:
.:/usr/local/lib/python
(Mind the .: at the beginning, so that it can search on the current directory, too.)
It may also be in other locations, depending on the version:
.:/usr/lib/python
.:/usr/lib/python2.6
.:/usr/lib/python2.7 and etc.
You are reading this answer says that your __init__.py is in the right place, you have installed all the dependencies and you are still getting the ImportError.
I was facing a similar issue except that my program would run fine when ran using PyCharm but the above error when I would run it from the terminal. After digging further, I found out that PYTHONPATH didn't have the entry for the project directory. So, I set PYTHONPATH per Import statement works on PyCharm but not from terminal:
export PYTHONPATH=$PYTHONPATH:`pwd` (OR your project root directory)
There's another way to do this using sys.path as:
import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')
You can use insert/append based on the order in which you want your project to be searched.
Using PyCharm (part of the JetBrains suite) you need to define your script directory as Source:
Right Click > Mark Directory as > Sources Root
For me, it was something really stupid. I installed the library using pip3 install but was running my program as python program.py as opposed to python3 program.py.
I solved my own problem, and I will write a summary of the things that were wrong and the solution:
The file needs to be called exactly __init__.py. If the extension is different such as in my case .py.bin then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.
Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do which python, and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.
To mark a directory as a package you need a file named __init__.py, does this help?
an easy solution is to install the module using python -m pip install <library-name> instead of pip install <library-name>
you may use sudo in case of admin restrictions
To all those who still have this issue. I believe Pycharm gets confused with imports. For me, when i write 'from namespace import something', the previous line gets underlined in red, signaling that there is an error, but works. However ''from .namespace import something' doesn't get underlined, but also doesn't work.
Try
try:
from namespace import something
except NameError:
from .namespace import something
Yup. You need the directory to contain the __init__.py file, which is the file that initializes the package. Here, have a look at this.
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.
If you have tried all methods provided above but failed, maybe your module has the same name as a built-in module. Or, a module with the same name existing in a folder that has a high priority in sys.path than your module's.
To debug, say your from foo.bar import baz complaints ImportError: No module named bar. Changing to import foo; print foo, which will show the path of foo. Is it what you expect?
If not, Either rename foo or use absolute imports.
You must have the file __ init__.py in the same directory where it's the file that you are importing.
You can not try to import a file that has the same name and be a file from 2 folders configured on the PYTHONPATH.
eg:
/etc/environment
PYTHONPATH=$PYTHONPATH:/opt/folder1:/opt/folder2
/opt/folder1/foo
/opt/folder2/foo
And, if you are trying to import foo file, python will not know which one you want.
from foo import ... >>> importerror: no module named foo
My two cents:
Spit:
Traceback (most recent call last):
File "bash\bash.py", line 454, in main
import bosh
File "Wrye Bash Launcher.pyw", line 63, in load_module
mod = imp.load_source(fullname,filename+ext,fp)
File "bash\bosh.py", line 69, in <module>
from game.oblivion.RecordGroups import MobWorlds, MobDials, MobICells, \
ImportError: No module named RecordGroups
This confused the hell out of me - went through posts and posts suggesting ugly syspath hacks (as you see my __init__.py were all there). Well turns out that game/oblivion.py and game/oblivion was confusing python
which spit out the rather unhelpful "No module named RecordGroups". I'd be interested in a workaround and/or links documenting this (same name) behavior -> EDIT (2017.01.24) - have a look at What If I Have a Module and a Package With The Same Name? Interestingly normally packages take precedence but apparently our launcher violates this.
EDIT (2015.01.17): I did not mention we use a custom launcher dissected here.
Fixed my issue by writing print (sys.path) and found out that python was using out of date packages despite a clean install. Deleting these made python automatically use the correct packages.
In my case, because I'm using PyCharm and PyCharm create a 'venv' for every project in project folder, but it is only a mini env of python. Although you have installed the libraries you need in Python, but in your custom project 'venv', it is not available. This is the real reason of 'ImportError: No module named xxxxxx' occurred in PyCharm.
To resolve this issue, you must add libraries to your project custom env by these steps:
In PyCharm, from menu 'File'->Settings
In Settings dialog, Project: XXXProject->Project Interpreter
Click "Add" button, it will show you 'Available Packages' dialog
Search your library, click 'Install Package'
Then, all you needed package will be installed in you project custom 'venv' folder.
Enjoy.
Linux: Imported modules are located in /usr/local/lib/python2.7/dist-packages
If you're using a module compiled in C, don't forget to chmod the .so file after sudo setup.py install.
sudo chmod 755 /usr/local/lib/python2.7/dist-packages/*.so
In my case, the problem was I was linking to debug python & boost::Python, which requires that the extension be FooLib_d.pyd, not just FooLib.pyd; renaming the file or updating CMakeLists.txt properties fixed the error.
My problem was that I added the directory with the __init__.py file to PYTHONPATH, when actually I needed to add its parent directory.
For me, running the file as a module helped.
Instead of
python myapp/app.py
using
python -m myapp.app
It's not exactly the same but it might be a better approach in some cases.
If you are using a setup script/utility (e.g. setuptools) to deploy your package, don't forget to add the respective files/modules to the installer.
When supported, use find_packages() or similar to automatically add new packages to the setup script. This will absolutely save you from a headache, especially if you put your project aside for some time and then add something later on.
import setuptools
setuptools.setup(
name="example-pkg",
version="0.0.1",
author="Example Author",
author_email="author#example.com",
description="A small example package",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
(Example taken from setuptools documentation)
I had the same problem (Python 2.7 Linux), I have found the solution and i would like to share it. In my case i had the structure below:
Booklet
-> __init__.py
-> Booklet.py
-> Question.py
default
-> __init_.py
-> main.py
In 'main.py' I had tried unsuccessfully all the combinations bellow:
from Booklet import Question
from Question import Question
from Booklet.Question import Question
from Booklet.Question import *
import Booklet.Question
# and many othet various combinations ...
The solution was much more simple than I thought. I renamed the folder "Booklet" into "booklet" and that's it. Now Python can import the class Question normally by using in 'main.py' the code:
from booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass
From this I can conclude that Package-Names (folders) like 'booklet' must start from lower-case, else Python confuses it with Class names and Filenames.
Apparently, this was not your problem, but John Fouhy's answer is very good and this thread has almost anything that can cause this issue. So, this is one more thing and I hope that maybe this could help others.
In linux server try dos2unix script_name
(remove all (if there is any) pyc files with command find . -name '*.pyc' -delete)
and re run in the case if you worked on script on windows
In my case, I was using sys.path.insert() to import a local module and was getting module not found from a different library. I had to put sys.path.insert() below the imports that reported module not found. I guess the best practice is to put sys.path.insert() at the bottom of your imports.
I've found that changing the name (via GUI) of aliased folders (Mac) can cause issues with loading modules. If the original folder name is changed, remake the symbolic link. I'm unsure how prevalent this behavior may be, but it was frustrating to debug.
another cause makes this issue
file.py
#!/bin/python
from bs4 import BeautifulSoup
if your default python is pyyhon2
$ file $(which python)
/sbin/python: symbolic link to python2
file.py need python3, for this case(bs4)
you can not execute this module with python2 like this:
$ python file.py
# or
$ file.py
# or
$ file.py # if locate in $PATH
Tow way to fix this error,
# should be to make python3 as default by symlink
$ rm $(which python) && ln -s $(which python3) /usr/bin/python
# or use alias
alias python='/usr/bin.../python3'
or change shebang in file.py to
#!/usr/bin/...python3
After just suffering the same issue I found my resolution was to delete all pyc files from my project, it seems like these cached files were somehow causing this error.
Easiest way I found to do this was to navigate to my project folder in Windows explorer and searching for *.pyc, then selecting all (Ctrl+A) and deleting them (Ctrl+X).
Its possible I could have resolved my issues by just deleting the specific pyc file but I never tried this
I faced the same problem: Import error. In addition the library've been installed 100% correctly. The source of the problem was that on my PC 3 version of python (anaconda packet) have been installed). This is why the library was installed no to the right place. After that I just changed to the proper version of python in the my IDE PyCharm.
I had the same error. It was caused by somebody creating a folder in the same folder as my script, the name of which conflicted with a module I was importing from elsewhere. Instead of importing the external module, it looked inside this folder which obviously didn't contain the expected modules.

Categories

Resources