AttributeError: module 'os' has no attribute 'chroot' - python

Below is my very basic code running in spyder & python is choking, what am I doing wrong?
import csv,os,sys
path = os.getcwd()
print (path)
os.chroot(path)
I get following error message:
os.chroot(path)
AttributeError: module 'os' has no attribute 'chroot'

One possibility is that your operating system is Microsoft Windows, for which os.chroot() is not available.

Did you name your file os.py? If you did, it is shadowing the stdlib os module. Change the name of your file and delete os.pyc or __pycache__.
Do print os or similar inside the script to see the file path Python is using to get the os module.

I have checked your code, and there is no any ERROR
but i see this message:
PermissionError: [Errno 1] Operation not permitted: '/home/beenj/Documents'
means we must run python3 with sudo ==> sudo python3
then enter above code...
or run compiled Python application (after completing ) in SuperUserDO (sudo)

Related

How to resolve "ImportError: No module named magic "

I am trying to emulate a firmware using fermadyne and while trying to extract the zip file using "./sources/extractor/extractor.py -b Netgear -sql 127.0.0.1 -np -nk "WNAP320 Firmware Version 2.0.3.zip" images" I get the following error,
"File "./sources/extractor/extractor.py", line 17, in
import magic
ImportError: No module named magic"
I tried installing magic for python3 but the problem still persists. Any suggestions?

Python3 ModuleNotFoundError when running from command line but works if I enter the shell

I think I'm missing something obvious here. I cloned this repo, and now have this directory structure on my computer:
When I try to run python baby_cry_detection/pc_main/train_set.py, I get a ModuleNotFoundError.
Traceback (most recent call last):
File "baby_cry_detection/pc_main/train_set.py", line 10, in <module>
from baby_cry_detection.pc_methods import Reader
ModuleNotFoundError: No module named 'baby_cry_detection'
However, if I type python and enter the interactive shell and then type the command
from baby_cry_detection.pc_methods import Reader
it imports just fine with no error. I'm completely baffled. I'm using a virtualenv and both instances are using the same python installation, and I haven't changed directories at all.
I think sys.path could be the reason that the module is not found when python command is executed. Here is how we can check if that is indeed the case:
In the train_set.py file, add import sys; print(sys.path). Looking at the error, the path may contain /path/to/baby_cry_detection/baby_cry_detection/pc_main. If that is the case, then we have found the issue which is that baby_cry_detection.pc_methods will not be found in the directory that sys.path is looking into. We'll need to append the parent baby_cry_detection directory to sys.path or use relative imports. See this answer.
The reason that python prompt successfully imports the module could be because the prompt is started in the correct parent directory. Try changing the directory to baby_cry_detection/pc_main/ and try importing the module.

Warning: Error in py_run_file_impl: NameError: name '__file__' is not defined

I'm trying to run a python file named tss.py via R in Shiny. I'm successful in running this file. But it is giving me error when I run a python file via user Interface of Shiny. I am getting no error when I run this tss.py in Pycharm. Do anyone know how can I resolve this problem?
Files path:
D:\PycharmProjects\Tasks\applications\tss.py
D:\PycharmProjects\Tasks\server.R
Server.R:
observeEvent(input$action,{
py_run_file("applications/tss.py")
})
tss.py:
import os
import sys
sys.path.append(os.path.split(os.path.dirname(os.path.realpath(__file__)))[0])
print("Mayday! Mayday!")
Error:
Warning: Error in py_run_file_impl: NameError: name '__file__' is not defined
76: <Anonymous>
Please don't mark this question as duplicate. I'm getting this in R not in Python. Do anyone know how can I resolve this?
I don't know why __file__ isn't defined, probably a bug of the launcher. It normally happens when the package is built-in or when the program has been run throught cx_freeze or py2exe, which isn't the case here.
In your case, a workaround would be to use sys.argv[0] as this value. It works here because it's the main program you're running. With an auxiliary package it wouldn't work (but __file__ maybe would :))
So I propose to add this to your tss.py file
import sys # must be done before
try:
__file__
except NameError:
__file__ = sys.argv[0]
so if __file__ exists, well, let it be, else define it as the filepath of the script that is running.

Eclipse PyDev AttributeError: 'module' object has no attribute

I am trying to connect to the shopify api but am having difficulty connecting when using Eclipse+PyDev. When connection via python in a bash shell the same commands work OK
to install:
pip3 install --upgrade ShopifyAPI
shopify.py (my code)
import shopify
shop_url = "https://APIKEY:PASSWORD#mystore.myshopify.com/admin/products.json
shopify.ShopifyResource.set_site(shop_url)
The reference to shopify.ShopifyResouce.. throws the following in PyDev:
AttributeError: 'module' object has no attribute 'ShopifyResource'
I think it may be due to relative imports in the shopify module (the same code works fine in a terminal).
In shopify.py: (shopify API)
from shopify.resources import *
in shopify.resources: (shopify API)
from ..base import ShopifyResource
When I run
from shopify.base import ShopifyResource
ShopifyResource.set_site(shop_url)
I get ImportError: No module named 'shopify.base'; 'shopify' is not a package
Any ides how I can fix this?
The problem might be you created a shopify.py file in your IDE rename that file and that error will be solved

ipython notebook lost after deleting a folder

My ipython notebook was in a given folder that I deleted from the terminal in the same time, and he get lost.
doing pwd or cd gives me :
OSError: [Errno 2] No such file or directory
ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.
Unfortunately, your original traceback can not be constructed.
How can I solve this without restartint the kernel ? (which I guess will solve the problem.)
Problem solved :
import os
os.chdir("/Full/path/from/root/to/my/existing/folder")

Categories