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

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?

Related

Running python file within a shell script : module not found error

I'm beginner to both bash and python.
I'm working in Ubuntu env.
to be short, I've created a shell script 'format_data.sh' that runs python file 'proc_ko.py' within it.
#!/bin/bash
...
python path/to/python/file/proc_ko.py
...
And the python file 'proc_ko.py' imports a module called khaiii
from khaiii import KhaiiiApi
api = KhaiiiApi()
...
But when I try to execute 'format_data.sh', I get this import error from python file.
Traceback (most recent call last):
File "media/sf_projet/pe/pe/PROGRAMME/SCRIPTS/proc_ko.py", line 5, in
from khaiii import KhaiiiApi
ImportError: No module named khaiii
which doesn't occur when I execute python file independently.
Python file 'proc_ko.py' itself doesn't have any error and 'khaiii' is well installed.
so I don't understand why import error occurs only through the shell script.
If u need more details to figure out, I'll be happy to provide. Thanks in advance for help.

Atom 'script' package gives me ModuleNotFoundError with 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

Arbitrary strings in bash throw python errors?

Something in my setup of my shell causes arbitrary strings like "krmpfl" or "u45g5svtJ7" to create a Python error:
$> krmpfl
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
I would expect bash (and not python!) to throw an error of the kind "Unknown command krmpfl. Did you mean...", but any non-recognized command is for some reason passed to python. I am confused.
Does anyone have an idea on how to debug this or how to move forward? I've tried type krmpfl but this (correctly) echoes bash: type: krmpfl: not found
My setup:
Win10 using Ubuntu 18.04 within WSL
ConEmu as a console
Bash-it
Python 3.8
Click (python package) installed to simplify creating commands
If your current shell function defines a function named command_not_found_handle, bash runs that for a non-existent command rather than immediately failing with a "command not found" error. In your case, that function exists and calls /usr/lib/command-not-found, which appears to be a Python script that tries to download (or at least suggest you download) a package with apt_pkg, but you don't have that Python module installed, which leads to the Python exception.

When calling a python script via the terminal, ImportError

When calling a python script from within Pycharm my script runs successfully. However when I call the same script via my terminal I get an import error:
Macs-MacBook:src macuser$ python ./run_events.py
Traceback (most recent call last):
File "./run_events.py", line 3, in <module>
from functions import return_ga_data
File "/Users/macuser/PycharmProjects/ops-google-extract/src/functions.py", line 2, in <module>
import connect
File "/Users/macuser/PycharmProjects/ops-google-extract/src/connect.py", line 4, in <module>
from oauth2client.service_account import ServiceAccountCredentials
ImportError: cannot import name 'ServiceAccountCredentials'
I am not using an environment. Also I'm using python 3.7.
All my python scripts are in the same directory. My terminal's pwd is the same directory.
Tried:
Tried calling the script with python3 ./run_events.py but I get the same result.
Per an SO post about paths I added this to the top of connect.py:
import sys
sys.path.append('/Users/macuser/PycharmProjects/ops-google-extract/src/functions.py')
I still got the same result.
Why can I run the file without an import error from within my IDE but not via the terminal when using ./run_events.py?
Do you have python 2 installed as well? Type python --version in the terminal and see what you get? My guess is Pycharm might be configured to use python 3 while your default python in terminal is python 2, so your python 2 is lacking those modules that was installed for python 3. So when you execute your script in the terminal it's using python 2. If that's the case you can try,
py -3 ./myscript.py

Run a Python script from Python prompt such that variables are loaded into the interactive environment

Say I have a (somewhat pointless) Python script
#!/usr/bin/python
a = 5
Is there a way to run this script from the interactive prompt such that after running if I type a I get
>>> a
5
and not
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
More generally, if my script calculates something through a series of steps, is there a way to access that something after the script has finished (in the same kind of manner).
Import it:
from yourscriptname import a
Each and every .py file in python is a module, and you can simply import it. If the file is called foo.py, import foo.

Categories