Eclipse - Pydev, doesn't recognize not imported methods - python

I have Eclipse Version: 4.2.1 and PyDev version 2.7.1 installed on my 64-bit Ubuntu.
I am using Python 2.7 and I have this problem with Eclipse that it doesn't recognize my un-imported methods. For example if I write a code like this:
def main():
myfiles = os.listdir('src')
if __name__ == '__main__':
main()
print'done!'
I get this error:
Traceback (most recent call last):
File "tset.py", line 5, in <module>
main()
File "tset.py", line 2, in main
myfiles = os.listdir('src')
NameError: global name 'os' is not defined
which is a pretty obvious error because I didn't write "import os" at the beginning of my code. My problem is that Eclipse doesn't highlight these errors for me anymore. Either I have to find them by myself or I will find out about them when I run my code.
My Eclipse was working fine before but I don't know what did I change that this happened.
I should also mention that I have "lib" folder in my project and in that folder I have a few of my own modules and I have added the "lib" folder to PYTHONPATH of my project. And the code that I am running is in another folder named "test" and that's not in the PYTHONPATH.

The problem was that my code was in a folder like this:
/MyProject/src/test/test.py
But the "src" folder was not in the PYTHONPATH. That is why Eclipse was not recognizing them as a source code and was not analyzing them.
To fix this go to:
Eclipse -> Project -> Properties -> PyDev-PYTHONPATH -> Source Folders
and add "/MyProject/src" to source folders. (I only had "/MyProject/src/lib" in my source folders)

In addition to the answer by #183.amir, if one of your apps is symlinked (not an actual directory), you need to add it to PYTHONPATH separately (with the steps described above).

Related

Python Code Runs in Spyder but not Anaconda Prompt -- ModueNotFoundError

I am in need of some help with the below. Using Python 3 code on Windows 10. Written in Spyder over Anaconda install and custom environment.
I have some code that runs in Spyder but not Anaconda Prompt. It gets a ModuleNotFoundError. The directory/file structure is like this:
my_python
smb_bau
etl
init.py
etl_globals.py
init.py
my_config.conf
my_connections.py
my_definitions.py
init.py
(underscores round init not displaying for some reason)
Some notes before the problematic code:
It's just test code for now that will be replaced with the real hive code later.
The code works when all the files are in one foler.
Th my_ prefixes are just to avoid any word that might cause issues while testing.
In my_connections I have this code:
class hive_connection:
def __init__(self):
self.my_name = None
self.my_town = None
import configparser
from smb_bau.my_definitions import CONFIG_PATH
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
my_details = config["my_details"]
self.my_name = my_details["my_name"]
self.my_town = my_details["my_town"]
And this is what's in my_definitions:
import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = os.path.join(ROOT_DIR, 'my_config.conf')
(I gratefully stole this from another thread to solve the issue of getting config info from a different folder).
The file I run is etl_globals which contains this code:
from smb_bau.my_connections import hive_connection
hive = hive_connection()
print(hive.my_name, hive.my_town)
When I run etl_globals in Spyder it works perfectly. But it fails in Anaconda Prompt.
I open up Anaconda Prompt, activate my environment, navigate to the etl folder, and enter python etl_globals.py
The error message I get is:
Traceback (most recent call last):
File "etl_globals.py", line 1, in
from smb_bau.my_connections import hive_connection
ModuleNotFoundError: No module named 'smb_bau'
I don't understand why the import of modules works in Spyder but not Anaconda Prompt.
The overarching goal is to be able to not have all the files in one folder.
Any help would be gratefully appreciated.
Many thanks.
Edit:
Just to be sure what the issue is I simplified it to create a file called my_functions (in the smb_bau folder) with this in:
def fn_test():
print(5)
And a file in the etl folder called my_test with this in:
from smb_bau.my_functions import fn_test
fn_test()
And got the same error:
Traceback (most recent call last):
File "my_test.py", line 1, in
from smb_bau.my_functions import fn_test
ModuleNotFoundError: No module named 'smb_bau'
Why does from smb_bau.my_functions import xxx work in Spyder but not Anaconda prompt?
Also, this may be relevant: the only reason there is a folder called my_python at the top is that I was getting an error saying no module named smb_bau. So I moved it all down a level and then it would accept smb_bau as a module but only in Spyder).
I also just checked PYTHONPATH and the root folder is there.
I have only been using Python for a week so I'm sure there are other bit of my code that aren't great -- but it does work in the spyder client.
Thanks and sorry if any of this is unclear.

Python3 ModuleNotFoundError when running from command line but works if I enter the shell

I think I'm missing something obvious here. I cloned this repo, and now have this directory structure on my computer:
When I try to run python baby_cry_detection/pc_main/train_set.py, I get a ModuleNotFoundError.
Traceback (most recent call last):
File "baby_cry_detection/pc_main/train_set.py", line 10, in <module>
from baby_cry_detection.pc_methods import Reader
ModuleNotFoundError: No module named 'baby_cry_detection'
However, if I type python and enter the interactive shell and then type the command
from baby_cry_detection.pc_methods import Reader
it imports just fine with no error. I'm completely baffled. I'm using a virtualenv and both instances are using the same python installation, and I haven't changed directories at all.
I think sys.path could be the reason that the module is not found when python command is executed. Here is how we can check if that is indeed the case:
In the train_set.py file, add import sys; print(sys.path). Looking at the error, the path may contain /path/to/baby_cry_detection/baby_cry_detection/pc_main. If that is the case, then we have found the issue which is that baby_cry_detection.pc_methods will not be found in the directory that sys.path is looking into. We'll need to append the parent baby_cry_detection directory to sys.path or use relative imports. See this answer.
The reason that python prompt successfully imports the module could be because the prompt is started in the correct parent directory. Try changing the directory to baby_cry_detection/pc_main/ and try importing the module.

Embeddable Python - ModuleNotFoundError for project folders

I'm trying to create an embedded python for my project.
I downloaded the portable python 3.7.6
Copied the folders from my main python installation (Lib, Scripts, include, DLLs etc..)
Added them to the PTH file like so :
python37.zip
.
Lib
Lib/site-packages
Scripts
include
DLLs
doc
libs
Tools
tcl
# Uncomment to run site.main() automatically
#import site
and when I tested it, I tried to run a project with the following structure :
By running the command (using the embedded python):
G:\GitHub\VRoscopy\VRoscopy\Conversion\python\python.exe main.py
I get the following error :
> Traceback (most recent call last):
> File "mymain.py", line 1, in <module>
> import folder.module as m ModuleNotFoundError: No module named 'folder'
However, when I try running the system python everything works normally.
Hope anyone can help me, thanks
The problem was solved by adding the project files to the PTH file, and uncommenting the "import site" like follows :
python37.zip . ../invesalius
../invesalius/* # <----- added this line
Lib
Lib/site-packages
Scripts
include
DLLs
doc
libs
Tools
tcl
# Uncomment to run site.main() automatically
import site

How do I replicate the way PyCharm is running my Python 3.4 project at the command line?

My project looks like this:
running-pycharm-project-at-cmd
- main.py
- c
- run_project.py
- z
- __init__.py
- the_module.py
- y
- __init__.py
- template.md
- the_module_module.py
- the_support_function.py
The contents of the .py files are shown below:
main.py
from c.run_project import run
print('running main.py...')
run()
c/run_project.py
from c.z.the_module import the_module_function, the_module_write_function
def run():
print('Running run_project.py!')
the_module_function()
# write a file:
the_module_write_function(read_file='./z/y/template.md', write_file='../README.md')
if __name__ == '__main__':
run()
c/z/the_module.py
from c.z.y.the_module_module import the_module_module_function
def the_module_function():
print('the_module_function is running!')
the_module_module_function()
pass
def the_module_write_function(read_file, write_file):
with open(read_file, 'r') as fid:
with open(write_file, 'w') as fid_out:
contents = fid.read()
contents.replace('{}', 'THE-FINAL-PRODUCT!')
fid_out.write(contents)
c/z/y/the_module_module.py
from .the_support_function import this_support_data
def the_module_module_function():
print('The module module function is running!')
print("Here is support data: {}".format(this_support_data))
pass
c/z/y/the_support_function.py
this_support_data = "I am the support data!"
GitHub repo to make replication easier: running-pycharm-project-at-cmd
The problem: in Pycharm load up the project with running-pycharm-project-at-cmd as the root. Then I right click and run the run_project.py file. Everything works fine. I cannot figure out how to run run_project.py from the command line in the same way. Creating main.py was a workaround suggested elsewhere, but it fails due to the relative references to the template file (in actual project, the y folder is a git submodule and therefore cannot have absolute references in it).
If you copy your main.py file into the c folder and rename it __init__.py, then you could run:
$ python -m c
The -m argument tells python to run a module or package (in this case c). Python will look in the c's folder for an __init__.py file and run that. You just need to ensure that the folder c is either in your PYTHONPATH or is a subfolder of the current working directory.
I circled back to this and was able to get it working. Note that I am on Windows 7, and some of this might be platform dependent. In order to get this working without changing any code, the key is setting PYTHONPATH environment variable before running. PyCharm does this automatically (by default, but it is configurable in the Run Configuration options).
Steps:
Recreating the issue:
Clone the github repo to G:\TEST.
Navigate to G:\TEST\running-pycharm-project-at-cmd-master\c
python run_project.py
Traceback (most recent call last):
File "run_project.py", line 1, in <module>
from c.z.the_module import the_module_function, the_module_write_function
ImportError: No module named 'c'
Simplest Solution (if able to run Python from the script location):
set the PYTHONPATH environment variable before running. e.g.
SET PYTHONPATH=G:\TEST\running-pycharm-project-at-cmd-master
Now python run_project.py
Running run_project.py!
the_module_function is running!
The module module function is running!
Here is support data: I am the support data!
To run from a remote location (this is definitely Windows specific):
Create a .bat file that navigates to correct working directory before running Python. i.e.:
START cmd /K "cd G:\TEST\running-pycharm-project-at-cmd-master\c & python run_project.py"
G:\TEST\myotherlocation
run_it.bat
Running run_project.py!
the_module_function is running!
The module module function is running!
Here is support data: I am the support data!

how to provide 'make directory as source root' from pyCharm to terminal

I have some problems with migrating to production:
cabox#box-codeanywhere:~/workspace/PEP$ python ./dev_scrapers/jordan.py
Traceback (most recent call last):
File "./dev_scrapers/jordan.py", line 3, in <module>
from utils import create_entity, create_id, custom_opener
ImportError: No module named utils
i have used pyCharm with button 'make directory as source root'
how to execute such command in terminal?
You should add your source root directory to PYTHONPATH:
export PYTHONPATH="${PYTHONPATH}:/your/source/root"
You can set the environment variable PYTHONPATH within the terminal as suggested by the accepted answer. This has to be done every time you start a terminal. In the case you use a virtual environment, you can place the variable assignment, like export PYTHONPATH="/your/source/root", in the file venv/bin/activate. Where venv stands for the name of the virtual environment directory.
I was able to set directory as root (and fix the unresolved import issue) doing the following:
from pycharm>Settings>Project>Project Structure select your project and from the file tree, select your django project directory then click the blue folder Source button to define the directory as your source.

Categories