When I import pyBullet, it immediately prints a line with the time when it was build:
In [1]: import pybullet
pybullet build time: Jun 19 2020 04:01:58
Is there a way to prevent this?
Change the source code. Jokes aside, I think there is no obivious way to prevent this from happening. Since it happens when you import the module, there is not much you can do in configuration of pybullet. Because it is literally the first thing you do with it.
Maybe you can reroute stdout during the import of the module. Which is a duplicate question already answered here: Stop Python Module from Printing
Check these solutions on a similar problem with pygame.
Why when import pygame, it prints the version and welcome message. How delete it?
Based on this, I suggest to try:
import contextlib
with contextlib.redirect_stdout(None):
import pybullet
full credit to MadPhysicist
Related
I was going through the import statement in Python and learnt that Python follows an import-only-once rule.
This means that two import statements in a given file referring to the same module won't reimport that module.
I just wanted to know, what exactly would be the reason Python uses this rule? How does it any way make Python programs more efficient?
At the end of the day, if a programmer imports the same module twice, he is responsible himself for the inefficiency (if any inefficiency is caused). How does two imports cause inefficiency?
If you have a package named pkg like:
print('importing...')
def hi():
print('Hi')
even though you import pkg multiple times like
import pkg
import pkg
import pkg
you will see only one "importing..." string printed. Because Python already knows that pkg is imported, Python does not import the package again. What will happen if Python import the same package again? Wasting time for importing just to rewrite what is already on the memory.
If pkg is like:
print('importing...')
someone = 'there'
def hi():
print('Hi, {}~'.format(someone))
you may want to import it multiple times due to the variable someone. Then you can do something like:
import sys
import pkg as pkg_1
sys.modules.pop('pkg') # let Python not know the package is already imported
import pkg as pkg_2
sys.modules.pop('pkg')
pkg_1.someone = 'John Doe'
pkg_1.hi()
pkg_2.someone = 'Foo Bar'
pkg_2.hi()
It does not look good, right? Designing a package not necessarily to be imported multiple time like:
print('importing...')
def hi(someone):
print('Hi, {}~'.format(someone))
is much better.
import loads a package/module into memory in order to ensure access to its symbols. Once it is loaded it makes little sense to repeat it. It would be like learning a poem by heart several times in a row.
Multiple imports typically do not happen in a single file. They happen when a package consists of several files. Each file needs to import all of its depencencies, the progammer has no other choice. Frequently used modules are thus referenced many times quite naturally. As explained above, it is fully sufficient to import only once.
i put import twice in my code and i realized that when you put import twice in your document just the first code run and the otherrun just like a comment !
when you import once a module it occur all the document it help you don't overwrite import in your documention
Hello everyone im currently learning python and i im having some problems importing modules and packages. Actually i think is more of a problem with vscode.
i have this package called "paquete" with a module (funciones) that i want to import to my "main" with some fuctions in it to test if it all works correctly but i still getting "emphasized items and unresolved-import" warnings.
but for some reason it works just fine.
is more of a annoying thing.
EDIT:
module with the function "funcion"
the warning that appears in the main folder "prueba" is "emphasized items"
i tried what u guys told me to do but it stills shows the warnings
As you are trying to import a specific function from module in python
You should use in this manner:
from paquete import funciones
If you want to import full module then use:
import paquete
I can't tell whats in the funciones file. But normally this yellow import lines are telling you that you import functions, which you dont use.
Try this instead if you only want
funcion
to be imported.
from paquete.funcions import funcion
This is also better because you import only the functions you need, not all of the functions you declared in the other file. Also all imports of the other file will be loaded into your file if you import with an asterix.
The issue is you are doing all of this from within a directory named prueba. If you changed the import to from prueba.paquete.funciones import * it should work after you add a __init__.py file to your prueba directory. The other option is to use a relative import: from .paquete.funciones import *.
But do note that using import * is strongly discouraged when you are not working within the REPL. It's much better to import to the module and then reference things off the module, e.g. from prueba.paquete import funciones, from .paquete import funciones, or import prueba.paquete.funciones. That way you know exactly where things in your code came from without having to read the top of your file.
pip3 intall "name"
Use Pycharm, rather than Vscode
A coworker send me a script that starts like this:
from ourlibA import *
from ourlibB import *
import random
import time
from datetime import datetime
print datetime.now()
when I invoke the script like that, it works nicely.
But I am using pycharm, and after make a few edits, I also used its "organize import" feature, which turned the imports into:
import random
import time
from datetime import datetime
from ourlibA import *
from ourlibB import *
print datetime.now()
And when I now run that changed script, I end up with:
print datetime.now()
AttributeError: 'module' object has no attribute 'now'
Now I am simply baffled, leading to these questions:
What exactly could be going on here? The import for datetime is still there, so where is that error coming from?
Obviously, the problem must be coming out of the fact that the second examples imports our own libraries last, not first. But how exactly can I find out which part of our libraries causes this?
I understand that this isn't a full mcve, but these private libraries are huge, and private. I am more asking about how to approach such an "import order" issue in python in general.
( I am using python 2.7.17 )
I took a "brute force" approach, and simply, manually reviewed the import statements of the private libraries (recursively), to identify one that does
import datetime
Thus: when that library is imported last, it conflicts with the previous from datetime import datetime statement.
Two "quick solutions" (more: dirty hacks) work, by changing the client script:
removing from datetime import datetime
using the "full path" print datetime.datetime.now() instead
The "better" solution: to step back, and get rid of both from X import * wildcard statements. It takes a bit of time to identify all the specific names that are required, but it is definitely worth spending that time.
As you may know from my previous posts, I'm learning Python. And this time I have a small error which I think is with this build of Python itself. When using the following:
import random
number = random.randint(1,10000)
Python gives me this error:
File "C\Users\name\Documents\Python\random.py", line 5, in (module)
print random.random()
TypeError: 'module' object is not callable
Every time I try to run it. Me no understand. Any help would be much appreciated!
EDIT: The two lines of code I'm trying to run:
import random
print random.randint(1,100)
That's it. And it gives me the same error.
By naming your script random.py, you've created a naming conflict with the random standard library module.
When you try to run your script, the directory containing the script will be added to the start of the module import path. So when your script does import random, you're effectively running a second copy of the script as the random module.
When the random module runs import random, it means that random.random will also be a reference to your module. So when you attempt to call the random.random() standard library function, you're actually attempting to call the module object resulting in the error you got.
If you rename your script to something else, the problem should go away.
Even I faced the same problem. I have renamed my python file from random.py to shuffle.py. This didn't work out. Then I changed the version then it worked. This may help a bit.
Python version : 3.6.7
replace
import random;
to
import random2;
I am using pycharm and I had to take the additional step to import the methods from random. In my case:
import random
from random import choice
The simple answer: Change your filename from "random.py" to something else as it is conflicting with random library.
I set pydoctor loose on my source code:
pydoctor *.py
and the auto-generated code seems nice, except for one of my modules just has a page saying "engine : module documentation - Undocumented". The module itself has a docstring as well as the class in it and several methods in it. It doesn't even list the methods or classes in engine.py. The only thing that stands out about engine.py other than sheer size is the number of modules it imports, but I don't see why that should be a problem.
import pygame, pygame.event, pygame.image, pygame.display
import EntityTypes, threading, main
import sys, time, events, collision, player, traceback
from functools import wraps
Thanks for any help.
somehow I see this a very long time after the question is asked... I can't help without seeing the module though. The only thing I can think of is that engine.py's docstring isn't being recognized as such for some reason.