Importing a module that needs an exported variable - python-3.x

I have a custom module that needs an library through an environment variable to be set correctly, which I am using in a script.
When I export from commandline and run the script, it works fine:
export FOO_LIB=/path/to/lib
python3 myscript.py
However without that, the import foo in my script throws exception:
ImportError: dlopen(/path/to/site-packages/bar.so, 2): Library not loaded: bar.dylib
In my script I tried
os.environ['FOO_LIB'] = '/path/to/lib'
import FOO
I still get the same error.
Is there a way I can do this in the python script itself without having to use bash export at all?

Related

Run python script with module/ modules already imported

I have tried nearly all solutions in my mind to do this, but the fact is I can't run a python script with modules imported already.
Here's my code for the module cls.py:
import os
def cls():
os.system('cls')
Given below is the code for opening python in cmd:
#echo off
python
pause
What I need is to open the python in the command prompt with the module cls imported. Also, when I try python -m <module> way, it doesn't show any errors, but the program ends.
Any help would be greatly appreciated and thanks in advance.
Saw this question related to mine, but it is not my problem: Run python script that imports modules with batch file
I think what you'r looking for is the interactive mode:
-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command
So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.
Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.

ModuleNotFoundError: No module named 'pandas' in a flask program

files used
data.csv - dummy data set i am using
main.py - my core flask program
problem i am facing
even though pandas is installed in my virtual environment(python3.8 - virtualenv) i am getting ModuleNotFoundError for pandas when used in main.py
additional information
There is no error in interactive environment when importing pandas
within the virtual environment.
Same error is thrown by webUI by
flask application.
Your main.py script has the shebang
#!/usr/bin/python
pointing to the system interpreter. If you execute ./main.py, your script is launched with the system interpreter, not the virtual env.
Change the shebang or execute the script with your venv python:
$ python main.py

ModuleNotFoundError: No module named 'win32com'

Actually, I have the same problem as him:
No module named win32com
I have installed pywin32 but MobaXterm tells me "No module named win32com".
However, I am able to run my program using IDLE with no error.
What's the problem?
Code:
import win32com.client
import sys, os
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut('C:/Users/Seaky/Desktop/CS 160.lnk')
os.chdir(shortcut.Targetpath)
What I am doing is that I am trying to do "cdlnk path" in the terminal using python code, where the path is a shortcut folder like the path above instead of a real path.
I used different code for running in the terminal and the IDLE but I only changed the path string from sys.argv[1] to the current one, which should not affect the result.
I have found out. The compiler of python installed in the MobaXterm doesn't have the pywin32 module.

Getting python2.7 path in django app for subprocess call

I am using linux. I am trying to run daemon from function in django views. I want to run shell command from a view in Djangp app. I am using python 2.7. Command needs python2.7 path.
My app will be like plug n play. So on system on which it is going to install may have python installed on different location. So I want to make python path dynamic.
Command will be
usr/bin/python2.7 filename.py --start
On my system path is usr/bin/python2.7.
I found follwing using os.
On python shell I tried following code & I get what I want
import os
getPyPath = os.popen('which python2.7', 'r')
pyPath = getPyPath.read()
pyPath.rstrip()
I got o/p as which is expected as below
usr/bin/python2.7
So now how to get this code is django app view function & run it so that I can get python path in a variable.
I found pythons subprocess module call using which we can run command through shell using shell=True.
So can I get above code running in django view function using subprocess call??
If not what is the other ways to get python path in variable in function django views.
Thanks in advance.
To view the full path to the current Python interpreter, use sys.executable
import sys
print(sys.executable)

Import csv from executed file raises error

I have a working python3 program that imports various standard library modules, e.g.
import re
import os
import csv
...
It runs without problem when I execute it directly. Now I want to run it through an exec call from an outside script, like this:
with open("main.py") as main:
exec(main.read())
But I get an error:
ImportError: no module named 'csv'
How can I make the executed script import all modules correctly?
I thought that all these three modules belong to the python standard library so, why the first two modules seem to be found while the third is not?

Resources