python: import error: no module named - python

Out of nowhere my scripts aren't finding locations anymore, that worked previously.
The only thing i changed was to add a environmental variable in Windows because i didn't get access python via the command line. But also after deleting it and resetting the "PATH" variable the problem is the same.
The error only occurs for modules in my project directory which i import via "from... import..."
no problem with "import sys" and so on.
I don't understand what's going on. I use eclipse and Python 2.7.
Update: I "auto configured" in the interpreter menu so that PYTHONPATH was rebuilt, i used File-->Restart but i won't damn work! I even removed the system environmental variable from windows. What is wrong with my setup?
Update2: Now i even reinstalled Eclipse but the f***ing errormessage is still there. It's driving me crazy! Anybody with more tips?
Update3: The problem occured after my laptop shut down because the battery was low. During that it was executing a script. Maybe it has sth. to do with the forced shutdown.

Ok, i found the solution.
Somehow the inner structure of my project got messed up, that the relative path in "from main.path1.path2 import ..." wasn't valid anymore.
Either it worked like "from path1.path2 import..." or (what i did) I created a project and included again the folder structure, but in the way that it works.

Related

PyCharm errors on valid same-directory imports

This is somethings that has been bothering me since I started using PyCharm to program in Python. I have a two .py files, in the same directory, main.py and external.py. Inside main.py, I have import external at the top. PyCharm marks this as an error, but it runs fine both in the new zsh MacOS terminal and PyCharm itself, and I can use all the things declared in external.py as expected.
I've played around with it a bit, and (to my very limited knowledge) it seems that PyCharm detects imports like Python2. Thats a guess, though, as I am unfamiliar with that version.
Why does PyCharm do this, or am I the one to blame? If it's not my fault, how can I fix it?
My file structure is as follows:
Project-|
|-external.py
|-main.py
I want to use things from external.py in main.py, and I can, but PyCharm gives it a red underline.
From given description, it correctly imported your external module.
Did you create a folder inside your project folder?
When using subfolders for your main.py/external.py files, Pycharm might not by default correctly detect your import statement.
Pycharm should give you an error message for said import statement.
Maybe the error is not connected to the import statement but to your pycharm setup e.g. correctly setting up your python interpreter.
If you provide more information regarding your folder structure or the error message, that might help.
Please try to mark directory containing your python files as Sources Root, see https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html

eclipse+pydev can't find cuda library for tensorflow

I am trying to debug some computer vision code (say dcgan) built on top of tensorflow. I installed the tf_0.10 in virtualenv (say, py1) and I use eclipse+pydev as the IDE. The problem is that the debugger can't find the cuda library so I get the error like the following,
ImportError: libcudart.so.8.0: cannot open shared object file: No such file or directory
On the other hand, the code works correctly on ubuntu command line and py1 in the virtualenv has no problem finding the cuda library.
In my eclipse+pydev setup, I set a py intepreter as py1,
by setting eclipse menu->window->preference->intepreters->python intepreter as
/home/zyuan/tensorflowr010/bin/python2.7 (where /home/zyuan/tensorflowr010 is the root of virtualenv)
Also the system PYTHONPATH for the above intepreter is,
/home/zyuan/tensorflowr010/lib/python2.7
/home/zyuan/tensorflowr010/lib/python2.7/lib-dynload
/home/zyuan/tensorflowr010/local/lib/python2.7/site-packages
/home/zyuan/tensorflowr010/lib/python2.7/site-packages
/home/zyuan/tensorflowr010/lib
Then I chose py1 and the intepreter for my computer vision code dcgan,
by setting dcgan -> properities -> pyDev-interpreter/grammer as py1 and add >external Librarie /usr/local/cuda/lib64 on pyDev-PYTHONPATH
Do I miss anything else?
Can you run it from the command line? If not, that'd be the first step, if you already can, my suggestion is starting Eclipse from that same shell and then checking if all environment variables when you launch it match the ones from the shell.
As a note, the PYTHONPATH is a special variable in PyDev made up from the files you add in the interpreter and in source folders in projects, so, to check the PYTHONPATH my suggestion is creating a program with:
import sys
print('\n'.join(sorted(sys.path)))
and run that from the command line and from inside Eclipse to compare what may be different (and fix the configuration on PyDev accordingly).
Also, after it works in the command-line, it may be nice launching Eclipse from that same shell (so that it will inherit all the env vars you defined in that shell, which may save you time from having to check many other env vars which may be important too).
I ran into a similar problem where my GPU was successfully accessed by tensorflow on the command line but not eclipse. I have 2 things to try to resolve this sitation.
Restart Eclipse - If changes have been made to the paths since you last opened eclipse, it may simply have a stale view of the environment
Within Runtime Configurations, set the necessary environment variables, in particular CUDA_PATH, to force location of the correct current path, especially if you may have multiple versions of CUDA lying around.
Your current status can be checked with:
import os
print(os.environ)
However, please note that in situation 1 where eclipse is stale, you may still get the correct environment variables printed out, even if eclipse is not aware of the current state of affairs.

PyCharm cannot find library

I am using PyCharm 5 to run a Python 2.7 (Anaconda) script in Ubuntu. My script imports a module with import tensorflow, but this causes the error ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory. So, it seems that the library libcudart.so.7.0 is needed by this module, but it cannot be found.
Now, I have seen that this library is on my machine in /usr/local/cuda-7.0/targets/x86_64-linux/lib. So, in PyCharm, I went to Settings->Project Interpreters->Interpreter Paths. This had a list of paths, such as /home/karnivaurus/Libraries/Anaconda/python2.7. I then added to this list, the path mentioned above which contains the required library.
However, this did not fix the problem. I still get an error telling me that libcudart.so.7.0 cannot be found. If I run my script from the shell though (python myfile.py), then it runs fine.
How can I tell PyCharm where to find this library?
I have noticed that if I have print sys.path in my script, the paths it prints out are entirely different to those in Settings->Project Interpreters->Interpreter Paths... should they be the same?
I came across this problem just recently using a remote debugger, however I believe it's still the same solution. I just added the following to the Environment Variables section in the Run/Debug Configuration options found in Run > Edit Configurations... dialog: LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
The path to your cuda library seems strange to me. I would expect it to be /usr/local/cuda-7.0/lib64 or /usr/local/cuda-7.0/lib.
Did you follow all of the cuda installation procedure?
If you type env on the command line, do you see a path to cuda in your LD_LIBRARY_PATH?
Update from comments below:
The issue is that PyCharm was invoked from the desktop, and wasn't getting the right environment variables. Solution is to either:
invoke from the command line,
create a script to set environment and then invoke, and make a link to that script on the desktop,
or set environment variables on the desktop item
While some of these answers are correct, and could work, I haven't seen what the OP specifically asked for, and that is where to set environments for the python console. This can be accomplished inside pycharm at:
File > Settings > Build,Execution,Deployment > Console > Python Console
In the options there, you'll find a place to define Environment Variables. Set LD_LIBRARY_PATH there.
Edit your pycharm.desktop, specify the environment variable in exec, like below:
[Desktop Entry]
Version=1.0
Type=Application
Name=Pycharm
Exec=env LD_LIBRARY_PATH=:/usr/local/cuda/lib64:/usr/local/cuda/lib64 /home/cwh/software/pycharm-2016.1.4/bin/pycharm.sh
Icon=/home/cwh/software/pycharm-2016.1.4/bin/pycharm.png
Name[zh_CN]=Pycharm
so pycharm will find cuda
Have you selected the right python interpreter in your project's settings?
See here.
I had a similar issue and changing the interpreter solved it without having to create a new icon.
The following works for me on Community edition 2019.3
To set globally for a project:
Open File/Settings/Project/Project Interpreter
click on the cog icon next to the interpreter
choose show all
click on the little folder with tree icon bottom right
add the path to "Interpreter Paths"

python unable to find module for import (on Mac)

I'll preface this question by saying that I've barely used python before, and never before on Mac OS, so I am fully ready to accept that I'm probably doing something rather silly!
I've been sent two python projects, one of which I need to run. When I open the project I'm interested in (in TextWrangler), and run, I get the following error:
context.py:16: ImportError: No module named fetch_command
Well.. fetch_command is a module in the other program, which is in the same directory (/Users/myname) as the program that I am trying to run. The (scant documentation for the applications suggests:
"I wouldn't try installing into your python installation dirs,
I'd install to some home directory or prefix and set up your
PYTHONPATH and PATH (or use virtualenv)"
and so I have tried (and succeeded - I've tested by calling echo $PATH and echo $PYTHONPATH) adding Users/myname to PATH and PYTHONPATH. This did nothing. I then tried adding /Users/myname/other_python_app/src to PATH and PYTHONPATH, but this also hasn't worked. Anyone know what I'm doing wrong..?
Thanks a lot in advance!
Ah, I understand the issue now. It's not that you can't find it in your Python project, but rather in your text editor. There should be a menu entry to set your PYTHONPATH from within TextWrangler (I tried looking for the documentation but the site seems to be down). Oftentimes, these editors don't respect the PYTHONPATH variable or do so only on restart.
This is a bit of a hack in your case, but try adding an empty file __init__.py so Python knows that this directory contains a module.

importing matplotlib in eclipse

I'm running pydev in Ecplise (Python 2.7). Numpy and tkinter work fine, but I installed matplotlib and for some reason when I try
from matplotlib import *
I get the response it's not found. I added the folder with the module to PythonPath but keep getting same message. What am I doing wrong?
from pydev tutorial (in my experience, the two most common cases related with your problem):
1)
The most common error is having a problem in the environment variables
used from the shell that spawned Eclipse, in a way that for some
reason when getting the variables of one interpreter, it gathers the
info from another interpreter (thus mixing the interpreter and the
actual libraries).
2)
If you add something to your python installation, you need to either
add it manually as a 'new folder' in the System PYTHONPATH (if it's
still not under a folder in the PYTHONPATH) or (recommended) remove
your interpreter and add it again, then, press apply.

Categories