Unable to import from file in higher/lower directory? - python-3.10

When I try to import my Function from a lower directory it doesn't work.
It works if both file's are in the same directory but if they are not, it fails saying there is no Module named "GameFunctions"
So this would work
./game.py
./GameFunctions.py
But this wouldn't
./etc/game.py
./GameFunctions.py
I tried using the code
from GameFunctions import * when both were in the same directory. it worked
Tried the same thing while they were in different directories
but I got the error
Traceback (most recent call last):
File "d:\Projects\MyScripts\PYTHON\Test\tes\game.py", line 1, in <module>
from GameFunctions import *
ModuleNotFoundError: No module named 'GameFunctions' ```
It can apparently read the file though, since it causes a problem if I rename a def in GameFunctions, it says the def is not defined in game.py

Ohk a much easier solution do one thing write your input statement like this
from parentdirectory.GameFunctions import *
This should solve your error

you can add folder of GameFunctions.py file to the python path:
import sys
sys.path.append('/Path/to/GameFunctions-Folder')
from GameFunctions import *

Related

Module for python3.6.2 (Spyder environment) from c source code

I'm quite new to python that I always use by writing script in spyder and running them in its Ipython console with python3.6.2 .
I'm trying to write a simple module from a "swig_example.c" file following a couple of swig tutorial (http://www.swig.org/tutorial.html, http://www.swig.org/Doc1.3/Python.html#Python_nn6).
My aim is to be able to run a script "main_python.py" which should look like:
import swig_test
print(swig_test.fact(4))
where fact is a function defined in the original c source.
The source file is "swig_example.c":
/* File: swig_example.c */
#include "swig_example.h"
int fact(int n) {
if (n == 0) {
return 1;
}
else {
return n * fact(n-1);
}
}
The header file is as simple as:
/* File: swig_example.h */
int fact(int n);
and the interface one:
/* File: swig_example.i */
%module swig_test
%{
#include "swig_example.h"
%}
%include "swig_example.h"
When in the terminal I run:
swig -python swig_example.i
a "swig_example_wrap.c" and a "swig_test.py" files are created.
How should I proceed to have my "main_python.py" working?
(It now returns a "No module named '_swig_test' error).
I would like to have some script (maybe using distutils?) so that each time I modify the .c source I can easily update the module without changing the "main_python.py" file.
If you have any solution which uses Xcode instead of spyder it would be well accepted.
I think that the question could be useful to many that are new to python (and Mac actually...) and try to use it while not throwing away their previous works...
EDIT:
I -partially- solved the problem. Now the main point remain Spyder.
I create the files ".c", ".h" and ".i" the way I described. Then, following this post (Python.h not found using swig and Anaconda Python) I create, in the same folder, my "setup.py" file:
from distutils.core import setup, Extension
example_module = Extension('_example', sources=['example.c','example.i'])
setup(name='example', ext_modules=[example_module], py_modules= .["example"])
Then, In anaconda navigator I open the terminal of the environment I'm working in, move to the right folder and run:
python setup.py build_ext --inplace
If now I open spyder everything works the desired way. But If I now want to modify my C source, say add a new function, problems arises. I modify the ".c", ".h" and ".i" files annd thenn re-run in the terminal the previous line. The "example.py" file result to e correctly modified (it innncludes the attribute of the new function), but when try to import the module in spyder (import example) changes are nnot registered and an error message "_example has no attribute "new function" is given in the Ipython console unless I restart Spyder itself.
Is there faster way to fix it? (maybe this is the interaction mentioned in the comments... )
Thank you all :-)

Python3.5 import program.py error

can not import a python program in the same folder
C:\Users\User_Unknowed\Desktop\Marvin
So i have started to just look if it where the fun.py that could be wrong or it can't just be find which now print error so i need help.
My code on test67.py:
start of code
import os.path
if not os.path.exists(fun.py):
print("Alarm")
if os.path.exists(fun.py):
print("import")
end of code
Anything of an solution would be lovely. Answer as quickly as possible please, thx.
To import a file inside of the same directory use:
import fun
To locate the file in a specified directory use os.walk:
import os
def find(nameofFile, path):
for root, dirs, files in os.walk(path):
if nameofFile in files:
return os.path.join(root, nameofFile)
if you find this file is not in the same directory you can use sys.path.insert() to add the path of the file you want to import:
import sys
sys.path.insert(0, pathtoFile)
from fun import *
Hope this helps.

How to import file from outside /components in react

My folder structure is as follows:
In my App.Js (which is under the components folder), I have:
import variables from '/src/EnvVariables/variables.json';
However, I get an error:
You attempted to import /src/EnvVariables/variables.json which falls
outside of the project src/ directory. Relative imports outside of
src/ are not supported. You can either move it inside src/, or
add a symlink to it from project's node_modules/.
I have also tried:
import variables from './src/EnvVariables/variables.json';
import variables from '/src/EnvVariables/variables.json';
import variables from '/src/EnvVariables/variables.json';
import variables from '/EnvVariables/variables.json';
import variables from './EnvVariables/variables.json';
All either give the above error, or say "can't resolve file".
This file is in a folder that is under /src, as they said. But it still tells me it must be under the /src/ directory. How can I import my variables.json file? I don't want to just put it under "components", i prefer to better organize it.
Have you tried to import with the relative path? (with one or more ../ sections)
import variables from "../EnvVariables/variables.json"
Of course, if you prefer absolute paths you can setup https://webpack.js.org/configuration/resolve/, but I believe for first, you should try the first solution
Do ../../ before path
source={require('../../assets/eating.png')}
if you just need to import your any file to a component : (just follow this steps)
Simply you can create a .env file in src directory .
In that .env file you need to add this line only NODE_PATH = src/
Then you can write import "Home.css" (your file name will be here) to a component from src, it will take it easily.
Run the program again , then you will get the same error, but take
it easy and just you have to do fully start your program with the npm start from
a new terminal.

Python script to fetch an executable file from local repository git

I have an access to the local repository (git) of my company with this url -> "gitolite#10.10.10.55:/Intel/BareRepos/lteue.git" and my python script need to execute any file (.c & .h files) of this project and create a binary. After which the script has to run this binary file.
The code which I have wrote is:
import os
os.system("git clone gitolite#10.10.10.55:/Intel/BareRepos/lteue.git")
os.system("cd /home/saicharan/Documents/lteue")
os.system("gcc somefile.c")
os.system("./a.out")
I am getting the error like this
gcc: error: helloworld.c: No such file or directory
gcc: fatal error:no input files compilation terminated.
sh: ./a.out: No such file or directory
Please help me out with this.
Fortunately I got the solution for this:
os.system("git clone gitolite#10.10.10.55:/Intel/BareRepos/lteue.git")
os.chdir("path/to/file")
os.system("gcc path/to/file/somefile.c")
os.chdir("path/to/file")
os.system("./somefile.out")
and its working fine.!
You can make it even more simpler.
import os
os.system("git clone gitolite#10.10.10.55:/Intel/BareRepos/lteue.git")
os.chdir("path/to/file")
os.system("gcc path/to/file/somefile.c && ./somefile.out")
It Works.!

undefined symbol: PyExc_ImportError when embedding Python in C

I'm developing a C shared library that makes a call to a python script.
When I run the application I get this error:
Traceback (most recent call last):
File "/home/ubuntu/galaxy-es/lib/galaxy/earthsystem/gridftp_security/gridftp_acl_plugin.py", line 2, in <module>
import galaxy.eggs
File "/home/ubuntu/galaxy-es/lib/galaxy/eggs/__init__.py", line 5, in <module>
import os, sys, shutil, glob, urllib, urllib2, ConfigParser, HTMLParser, zipimport, zipfile
File "/usr/lib/python2.7/zipfile.py", line 6, in <module>
import io
File "/usr/lib/python2.7/io.py", line 60, in <module>
import _io
ImportError: /usr/lib/python2.7/lib-dynload/_io.so: undefined symbol: PyExc_ImportError
If I try to import the module io from console works fine instead:
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import galaxy.eggs
>>>
During the compilation of library I've used this compiler option as suggest here :
Embedding python in C, undefined symbol: PyExc_ImportError
In addition I've added also the compiler options obtained from
python-config --includes|--libs|--cflags|--ldflags
Here you can find the log of makefile of library http://pastebin.com/348rhBjM
Thanks a lot, any help will be apreciated.
#user1515248 solution is a link-only solution which are discouraged. i am writing this answer to expand on the links he gave and provide a more fleshed out answer (that also backs up the link he gave).
The link, https://mail.python.org/pipermail/new-bugs-announce/2008-November/003322.html, says:
I have been given the following workaround: in mylib.c, before
PyInitialize() I can call dlopen("libpython2.5.so", RTLD_LAZY | RTLD_GLOBAL);
This works, but I believe that lib-dynload/*.so should depend on
libpython2.5.so.1 so this hack should not be necessary.
I am using Ubuntu 8.04 with Python version 2.5.2-2ubuntu4.1.
All I had to do was add a single line of code:
// new line of code
void*const libpython_handle = dlopen("libpython2.6.so", RTLD_LAZY | RTLD_GLOBAL);
PyInitialize();
p.s.
I am on CentOS-6.
p.p.s.
My PyInitialize() is wrapped in a class and so dlopen()/PyInitialize() is done in the constructor and dlclose()/PyFinalize() is done in the destructor.
I've found the solution. Maybe can be useful for someone else.
It's a bug of python as written here http://mail.python.org/pipermail/new-bugs-announce/2008-November/003322.html
I've used the solution posted here http://www.cilogon.org/gsi-c-authz
I use such workaround: explicit linking of plugins from lib-dynload directory (it's simply, then explicit dlopen in code). Example with datetime.so:
cmake:
SET ( CMAKE_SHARED_LINKER_FLAGS "/usr/lib/python2.7/lib-dynload/datetime.so" )
or just add /usr/lib/python2.7/lib-dynload/datetime.so as linker parameter to
gcc in command line:
g++ -shared -o libfoo.so foo.o -lbar -lzab /usr/lib/python2.7/lib-dynload/datetime.so

Resources