sys

How to resolve “ValueError: attempted relative import beyond top-level package”

为君一笑 提交于 2019-12-29 08:20:33
问题 I have the following problem with my project, help me please! Here is the structure of my package: /pkg /pkg/__init__.py /pkg/sub1/__init__.py /pkg/sub2/__init__.py /pkg/sub1/foo1.py /pkg/sub2/foo2.py Here is implementation of foo1.py: from ..sub2 import foo2 def f(): print("Hello!") When I run foo1 I get error: ValueError: attempted relative import beyond top-level package . I can solve it doing the following adjustment: import sys import os sys.path.append(os.path.abspath(os.path.pardir))

Get a string in Shell/Python using sys.argv

时光怂恿深爱的人放手 提交于 2019-12-25 18:22:29
问题 I'm beginning with bash and I'm executing a script : $ ./readtext.sh ./InputFiles/applications.txt Here is my readtext.sh code : #!/bin/bash filename="$1" counter=1 while IFS=: true; do line='' read -r line if [ -z "$line" ]; then break fi echo "$line" python3 ./download.py \ -c ./credentials.json \ --blobs \ "$line" done < "$filename" I want to print the string ("./InputFiles/applications.txt") in a python file, I used sys.argv[1] but this line gives me -c. How can I get this string ? Thank

Python: Importing a graph

随声附和 提交于 2019-12-25 06:28:36
问题 So I have this code here, which basically runs Dijkstra's Algortihm on a graph in Python, and then prints the distance and moves it takes to get from start to end. My problem is, I can't figure out how to import a graph as a .txt file using sys.argv and have it read it and run the algorithm on it. Here is my code, with a graph and start 'a' and end 'b' already filled into it. (It should work). import sys def shortestpath(graph,start,end,visited=[],distances={},predecessors={}): """Find the

command line - int() argument must be a string, a bytes-like object or a number, not 'list'

可紊 提交于 2019-12-24 15:53:35
问题 I got a very basic code: import sys file = sys.argv[0] arg = int(sys.argv[1:]) def multiplier(x): if arg < 1: print('Put an argument in') else: totals = 1 for i in range(1,x+1): totals *= i return totals print(multiplier(arg)) And I'm trying to run this from the command line and I keep getting this error: File "program.py", line 4, in <module> arg = int(sys.argv[1:]) TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' I understand the error, but I'm new to

How to Import Multiple Python Modules from Other Directories

家住魔仙堡 提交于 2019-12-24 06:13:27
问题 Python newbie here. Any and all help on python structure would be much appreciated! MAIN QUES: How to Import Multiple Python Modules from Other Directories I've reassembled the repository for a project so that modules for specific tasks are within individual folders according to type of task instead of just one huge folder. Because my modules were all in one folder, I used to be able to import easily in this fashion: import sys sys.path.insert(0,'/home/myname/folder_name/all_modules') Now, I

Overwrite printed line in python 3.x on mac

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 04:23:39
问题 I'm attempting to write a program that involves the use of a coin flip, heads or tails, but so that it will print 'heads' then be replaced by 'tails' and continue doing this until it decides on an answer. At the moment, when I run the program, it prints the 'heads' or 'tails' on the next line every time. This happens on both Idle and Terminal. I've tried using carriage return (\r), backspace (\b) and sys.stdout.write() and .flush() but neither are working, it just keeps printing on the next

python command line arguments in main, skip script name

拈花ヽ惹草 提交于 2019-12-19 05:18:26
问题 This is my script def main(argv): if len(sys.argv)>1: for x in sys.argv: build(x) if __name__ == "__main__": main(sys.argv) so from the command line I write python myscript.py commandlineargument I want it to skip myscript.py and simply run commandlineargument through commandlineargument(n) so I understand that my for loop doesn't account for this, but how do I make it do that? 回答1: Since sys.argv is a list, you can use slicing sys.argv[1:] : def main(argv): for x in argv[1:]: build(x) if _

knitr - error when importing python module

爱⌒轻易说出口 提交于 2019-12-19 05:15:06
问题 I am having trouble when running the python engine in knitr. I can import some modules but not others. For example I can import numpy but not pandas. {r, engine='python'} import pandas I get the error. Quitting from lines 50-51 (prepayment.Rmd) Error in (knit_engines$get(options$engine))(options) : Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named pandas Calls: <Anonymous> ... process_group.block -> call_block -> block_exec -> in_dir ->

ctypes in python size with the `sys.getsizeof(Var)` method vs `ctypes.sizeof(Var)`

半腔热情 提交于 2019-12-18 08:33:09
问题 I have a question about variable size in python, I'm using the the Ctypes because i want a 1 byte number, but when I tried to check it's size in python (via sys.getsize ) it said it was 80 byte but when i checked with the ctypes (via ctypes.sizeof ) it said it was 1 byte only, can someone tell me what is the difference and why is there 2 different sizes? is it because python is using an object or wrapper? and when its sent to c it views the real size? import sys import ctypes print("size in

Why does not os.system(“cd mydir”) work and we have to use os.chdir(“mydir”) instead in python? [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-17 21:11:05
问题 This question already has answers here : Subprocess changing directory (6 answers) Closed last year . I tried doing a "pwd" or cwd, after the cd, it does not seem to work when we use os.system("cd"). Is there something going on with the way the child processes are created. This is all under Linux. 回答1: The system call creates a new process. If you do system("cd .. , you are creating a new process that then changes its own current working directory and terminates. It would be quite surprising