Atom 'script' package gives me ModuleNotFoundError with Python - python

My folder layout is the following:
folder
config.py
mainScript.py
Inside the mainScript.py file i run the following line:
from config import a, b, c
When I run mainScript.py with the Atom package script Then I get the following error:
Traceback (most recent call last):
File "/var/folders/3q/ytqgpk6d7bl69td5z2jxqpfm0000gn/T/atom_script_tempfiles/beda7210-249b-11eb-97fc-47e8e768ffb0", line 12, in
from config import a, b, c
ModuleNotFoundError: No module named 'config'
However, when I run it in my regular macOS terminal it runs properly. Both the terminal and Atom Script are running python3.
Why is it not properly identifying the location of the config.py file? How do I get it to import it correctly.

Always use it like this:
from .config import a, b, c

Related

Getting conda to apply to python script called by other python script

I have a script, A, that is importing another python script, B from the directory 'scripts'.
Both have the line :
from pymol import cmd, stored
And the conda environment I use to call script A has pymol activated.
However, pymol is failing to load within B, despite loading fine in A:
Traceback (most recent call last):
File "A", line 7, in <module>
import B
File "scripts/B", line 6, in <module>
from pymol import cmd,
stored ModuleNotFoundError: No module named 'pymol'
I've tried adding pymol to the path in B with
sys.path.append(os.path.abspath("/path/to/mambaforge/envs/pymol/bin"))
but am still getting the same error.
My question is should I be expecting this to work? Or is there something I need to do that I don't realise in order to get a conda environment to apply to a python script called by another script within the desired environment?

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

Why I can import the package in the parent directory in PyCharm, however, it reports an error in the terminal?

Why I can import the package in the parent directory in PyCharm, however, it reports an error in the terminal?
I will use the following toy code to show my problem. For example, the project structure is like the following:
In hello.py is:
def func():
print("hello")
In test.py is
import mypackage.hello
mypackage.hello.func()
Now if I run test.py in PyCharm, it runs perfectly and prints hello. However, if I use the terminal and cd to test directory, and run command python test.py, it reports the following error:
Traceback (most recent call last):
File "test/test.py", line 1, in <module>
import mypackage.hello
ModuleNotFoundError: No module named 'mypackage'
PS: I use the same environment in both PyCharm and terminal.
Question:
Why does it show different results in PyCharm IDE and terminal?
In general, what's the correct style to import a package in the parent directory such that I can run both in the terminal and IDE?

Python, importing custom libraries from subdirectory, working in IDE, not in shell

For sake of organization, I've placed all my custom libraries into a subdirectory. When I run my script file in the spyder IDE, it imports everything as intended, and runs smoothly, but when I execute the script from shell, it fails to import. (I have created an __init__.py file in the modules/ directory).
Also, this is being run on Linux.
Here is my code:
import os
dirpath = os.path.dirname(__file__)
sys.path.append('{}/modules/'.format(dirpath))
#custom libraries in dirpath/modules/
import data_pull
import features
import create_listings
example = data_pull.mp(pool)
data = features.generate(example)
Shell output:
Traceback (most recent call last):
File "data_manager.py", line 15, in
import data_pull
ModuleNotFoundError: No module named 'data_pull'

Strange Python Import Error

I am new to Python. I am getting ImportError and seem to have tried everything that's in the documentation and the various notes in this site and other
My code is structured as follows:
vsm
|
|______bin
| vsmx.py
|______site-packages
__init__.py
|
|_____libs
__init__.py
monitor.py
In monitor.py I have a function named getStr and the two __init__.py files are empty
I have the PYTHONPATH set to vsm/site-packages & vsm/site-packages/libs. When I run from command line, python bin/vsmx.py, I get:
Traceback (most recent call last):
File "bin/vsmx.py", line 15, in <module>
from libs.monitor import getStr
File "/var/src/vsm/bin/vsmx.py", line 15, in <module>
from libs.monitor import getStr
ImportError: No module named monitor
However, when I try to run this interactively, it seems to work. I tried on both windows and linux using python 2.6.1.
Any pointers will be much appreciated
ImportError: No module... is usually a very (obscure) error meaning that you have circular imports.
Module a.py:
import b
Module b.py:
import a
Then main.py:
import a
This should cause ImportError: No module named a, because a is importing b and not ready when b tries to import it.

Categories