How to run a file in pycharm? - python

I have a project that I imported into pycharm. It looks like this:
The file grabber.py references class Fetcher within fetcher.py like this:
from grabber.fetcher import Fetcher
from the root folder Automaton I can run grabber.py from the command line with this command:
python -m grabber.grabber
But, I want to run grabber.py from pycharm. When I try I get this error:
Traceback (most recent call last):
File "C:/Automaton/grabber/grabber.py", line 1, in <module>
from grabber.fetcher import Fetcher
File "C:\Automaton\grabber\grabber.py", line 1, in <module>
from grabber.fetcher import Fetcher
ImportError: No module named 'grabber.fetcher'; 'grabber' is not a package
How do I get pycharm to run the file the same way I can from the command line?

My problem was pretty silly. I was having the problem because I had a package grabber and a file within it called grabber.py That name collision caused the problem. I renamed grabber.py and it works.

Related

Absolute/relative import in Python: ModuleNotFoundError and more

This is my project structure:
- config
- data
- src
- resources
- db
- test
N.B.: I am using Python 3.9 and every folder that contains a .py file also has a __init__.py file
All the scripts I want to run are located in the /src folder and they used code from other scripts placed in the /src/resources folder (which is basically acting like a library).
Some of these scripts also read YAML files from the /config folder
Here is the problem, I cannot find a way to properly run these scripts from the command line, I am always getting errors like:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/runpy.py", line 185, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/local/lib/python3.8/runpy.py", line 111, in _get_module_details
__import__(pkg_name)
File "/home/pi/crypto/src/ethMessage.py", line 4, in <module>
import update_db
File "/home/pi/crypto/src/update_db.py", line 1, in <module>
from db.mysql_main import insertValueAndFee
File "/home/pi/crypto/src/db/mysql_main.py", line 6, in <module>
from src.resources.parser import read_yaml
ModuleNotFoundError: No module named 'src'
I tried both with relative and absolute import, right now absolute import is what I am using (e.g. from src.resources.parser import read_yaml)
Which is the proper way to run scripts from the command line?
EDIT:
As you suggested, I added
sys.path.append( os.path.abspath(os.path.dirname(__file__)+'/..') )
to all the main scripts, and I am still getting a similar error:
Traceback (most recent call last):
File "src/ethMessage.py", line 6, in <module>
import update_db
File "/home/pi/crypto/src/update_db.py", line 1, in <module>
from db.mysql_main import insertValueAndFee
File "/home/pi/crypto/src/db/mysql_main.py", line 6, in <module>
from src.resources.parser import read_yaml
ModuleNotFoundError: No module named 'src'
To clarify, I am running my script from the global folder, which in my case is named "crypto".
I am also open to change the project structure with one that doesn't create problems.
If you want to refer to all of those packages by their root name, then all you have to do is add that folder to the Python path. So, for main program scripts in src, just add something like this:
import os
import sys
sys.path.append( os.path.abspath(os.path.dirname(__file__)+'/..') )
Now, the parent directory of your script will be on the path, no matter where you run it from. Now you can say
from src.resources.parser import read_yaml
If someone is still looking for a solution, I highly recommend not to bother with Python's imports: they are probably the worst part of the whole language.
Instead, if you want to use some files as a library, you should use setuptools to create a package from those files.
Then, you can install it locally or publish it on PyPi.
This way, you can import your library in a script just like another third-party module, (e.g. requests, selenium, ...), and things will work, instead of giving you a headache because a file is in a directory instead of another.

No Module Found python (run outside IDE)

I'm trying to run a script outside pycharm and this is what happens
my command prompt when i try to run the main.py
C:\Users\Beatriz\PycharmProjects\Tese\Main> python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
from View.view import *
ModuleNotFoundError: No module named 'View'
C:\Users\Beatriz\PycharmProjects\Tese\Main>
I put each file in a package like this:
files_packaging
Still it seems not recognize the module "View"
my main code:
from View.view import *
if __name__ == "__main__":
View()
The best way is to move the contents of the Main folder outside of it, directly inside the Test folder where all the other folders are. That way you can directly invoke from View.view import *.
To run the script:
cd PycharmProjects/Tese
python main.py

Python error - “ImportError: No module named”

First of all this is what my directory looks like:
.:
ref.py main.py utility
./utility:
file_manip.py
When I execute main.py I get this error:
>>> python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
import utility.file_manip as fm
ImportError: No module named utility.file_manip
I cannot for the life of me get to the bottom of this. Clearly utility.file_manip exists...
You need a blank __init__.py file in your utility directory in order for Python to see it as a package. See the tutorial.

Python, how to import a .py file

I have a directory with two .py files in my C:\Python27\word_data called main.py and gethtml.py.
I want to import gethtml.py in my main.py, print def from that file, and I tried to do like this:
import gethtml
print gethtml.getHtmlText()
When I run this in a Python shell I get an error:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
execfile("word_rank/main.py")
File "word_rank/main.py", line 3, in <module>
import gethtml
ImportError: No module named gethtml
What am I missing?
You could check that the working directory for your python session is the directory containing your two python files. You can get Python to report the location of the current working directory as follows:
import os
print(os.getcwd())
Python will look in the current working directory (and in some directories in PATH) for the file you are trying to import. Not being able to find the file would give the error above.

ImportError: Cannot open shared object file in Python

I am trying to run a python script which depends on other modules, however I ran into this:
bash-3.2$ PYTHONPATH=/my/path/tables-2.3.1/build/lib.linux-x86_64-2.7/ ./fastcluster.py
Traceback (most recent call last):
File "./fastcluster.py", line 5, in <module>
import tables
File "/my/path/tables-2.3.1/build/lib.linux-x86_64-2.7/tables/__init__.py", line 59, in <module>
from tables.utilsExtension import getPyTablesVersion, getHDF5Version
ImportError: libhdf5.so.7: cannot open shared object file: No such file or directory
bash-3.2$ ls libhdf5.so.7
libhdf5.so.7
bash-3.2$
No such file or directory libhdf5.so.7 ? But when I 'ls' it, it's there, right in my directory. So what's going on here?
The loader isn't looking there. Either put it in one of the standard locations for libraries, add the directory to the loader configuration, or set $LD_LIBRARY_PATH before running Python.

Categories