Import does not look into pythonpath when importing personal modules - python

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.

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.

What should I set my pythonpath to

I downloaded a python module from online and want to use it in IDLE, but I keep getting an error message saying it's not found.
My python path is "/Library/Frameworks/Python.framework/Versions/2.7/bin". If I want to be able to run modules I downloaded from online into IDLE, what should I set my python path to to be able to do that?
I am running OsX 10.10.5
Create a directory somewhere under your home directory. Let's say it's ~/pylib. Copy the module to ~/pylib. Before running IDLE execute the command
export PYTHONPATH=~/pylib
You should now be able to import the module.
If a module you have downloaded was properly packaged, it should come with the installation routine. Conventionally, it is called setup.py. If you see this file, you can just do
python setup.py install
That should take care of most packages available online and you don't even need to figure out where it was installed to.
If you really need to check/set the paths, you can check the directories as reported by
import sys
sys.prefix
sys.exec_prefix
These are the paths that point to the directories where stuff is installed (prefix/lib).
If you are looking into custom or user installation (using --user flag), check this folder: userbase/lib/python<VERSION>/site-packages
More details in the documentation.

Python works in PyCharm but not from terminal

I recently figured out how to import modules for unittesting in python. As a solution to this, I use:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from Dev.test import someclass
This works fine while running in PyCharm and I get the expected output. However, when I run from terminal I run into an error:
ImportError: No module named Dev.test
I have the init files where they are supposed to be but I'm lost as to why this is working in PyCharm but not from the terminal. I have not changed my path or anything in PyCharm as this code is supposed to be able to run with minimal modifications on other machines. Any idea as to why this is happening and what I might be able to do to fix it?
My folder structure is as follows
-Current
-Dev
-__init__.py
-test
- __init__.py
-someclass.py
-Tests
-__init__.py
-someunittest.py
I have tried running someunittest from the main folder as well as with a complete path but it only works in PyCharm
sys.path.append(os.getcwd()[:os.getcwd().index('Dev')])
I added this to my imports and it seems to have solved the problem. However, this doesn't seem like it would be the right way to do it; it will do for now.
When running a script from within PyCharm, it runs it in an environment with PYTHONPATH set to the list of all the folders that are marked "Sources Root" (with a blue folder icon) in the project explorer.
Outside of PyCharm, PYTHONPATH is not normally set. The first entry in sys.path refers to the current working directory where the script was run from. As long as you run your script with your terminal's working directory as the folder containing Dev, it should be able to find the Dev.test module, regardless of the extra entry added to sys.path.
Once you get the working directory correct, you should be able to remove the sys.path hack.
What #codewarrior has said about the PyCharm setting its own PYTHONPATH is correct. But sys.path didn't have my current working directory. So to get around this problem, I updated my PYTHONPATH (or you can edit sys.path).
Setting PYTHONPATH
export PYTHONPATH=$PYTHONPATH:`pwd` (OR your project root directory)
Updating sys.path
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.
HTH.
Pycharm uses a virtual environment. When you try run your program in your terminal this enviroment isn't active.
You need to build or upload your enviroment Pycharm with your libraries.
cd to the directory project and write in terminal:
source venv/bin/activate
I too have had this issue - and the PYTHONPATH setting set by PyCharm did seem to be the issue.
My alternative (as I was nearly finished writing the code) was to generate a setup.py - and install the classes/structure in my local virtual Python environment.
I would recommend trying out $ pip install . in your source directory. This will install your own packages for your project.
To add to similar answers here, PyCharm is doing some extra config for you before running your script. If adding your sources root to PYTHONPATH doesn't work then examine your run configuration in PyCharm for the script in question, there will likely be some more behind the scenes magic at play.
I had similar problem. I think the problem is that Pycharm modifies PYTHONPATH so before running your script:
cd to the file where python file resides
run export PYTHONPATH=.
run the script
You can also create "main" python file where you set the python path and then call the other modules

Python cannot find module even when path is appended to sys.path

Having some weird troubles installing python modules on my work computer (read: no admin/root rights), I'm using 2.7.5. I downloaded and unpacked the tarball and ran 'setup.py', but it had no effect: When I open the python shell, it can't find the module (this specific one is fuzzywuzzy). However, if I right click -> edit with IDLE the setup.py, and then run the shell from that file, it loads and works perfectly fine. Or, if I then open a new file from that shell, use the module and run it, it works fine. -__-
I've tried using:
import sys
sys.path.append('path here')
to append the location where the module is installed, but this doesn't help, nor does the path stay in the sys.path list when I close/reopen the shell.
This is actually driving me insane. Can anyone help? I'm relatively new to programming and python.
The best and easy way provided by python to install/uninstall packages is to use PIP.
use this
python -m pip install packagename==version
same way to uninstall
python -m pip uninstall packagename==version
if you are using windows you need to set path variable first usually python file will be in path C:\Python27 to set path variable
PATH=%PATH%;C:\Python27;

"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

Categories

Resources