w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Identify directories that are packages in Mac OS X with Python
OS X packages (and bundles) are usually defined by their extension. Simply create a directory with an .app extension to see it be presented as a (broken) application in the Finder. The official documentation lists the following ways to define bundles: The Finder considers a directory to be a package if any of the following conditions are true: The directory has a known filename extension: .app, .bundle, .framework, .plugin, .kext, and so on. The directory has an extension that some other application claims represents a package type; see “Document Packages.” The directory has its package bit set. The preferred way to specify a package is to give the package directory a known filename extension. For the most part, Xcode takes care of this for you by providing tem

Categories : Python

Keeping Python packages with the same top-level name in different directories
There are several options, one of which is imp: import imp foo = imp.load_source('module.name', '/path/to/file.py') foo.MyClass() (my source) Another is with importlib Relative: importlib.import_module('.sub1', 'fs1.top') Absolute: importlib.import_module('fs1.top.sub1') (my source)

Categories : Python

Python CGIHTTPServer Default Directories
My suspicions were correct. The examples from which this code is derived showed the wrong way to set the default directory to be the same directory in which the server script resides. To set the default directory in this way, use: handler.cgi_directories = ["/"] Caution: This opens up potentially huge security holes if you're not behind any kind of a firewall. This is only an instructive example. Use only with extreme care.

Categories : Python

How to get current user's default movie directory and other default directories?
If you just want to get access to common directories like Documents, Desktop, Movies, etc., -[NSFileManager URLsForDirectory:inDomains:] is the API you're looking for. You'll want to use the NSUserDomainMask for the domains parameter and one of the NSSearchPathDirectory constants (like NSMoviesDirectory) for the directory parameter. If you're actually trying to get the list of folders the current user has in the "Favorites" section of their Finder sidebar (users can customize this list), this Stack Overflow question may have some answers for you.

Categories : Osx

python packages -- overriding built-in packages and the dreaded "Module xxxx was already imported" warning
Rename myprogramxlrd to myprogramfrozen_xlrd. Then import it with try: import xlrd except ImportError: import frozen_xlrd as xlrd Alternatively, you could tell Python to silently ignore this particular UserWarning: import warnings warnings.filterwarnings("ignore", message="Module xlrd was already imported", category=UserWarning) Place this early on in the program, before scikits gets imported.

Categories : Python

Does virtualenv isolate python itself + python packages, or just python packages?
Virtualenv installs python, but it's installed in the bin directory of the virtualenv you created. Therefore you need to run it with ./bin/python. You can also "activate" the virtualenv by running source bin/activate Which will put the virtualenvs bin directory first in the path (and do some other trickery I think) which will make the virtualenvs Python the default Python, so you can start it with just python. But this is not necessary.

Categories : Python

Testing Python Packages
You can use pip -e install <path-to-package> to install a package in editable mode. Then, you can make changes to the source code, and not have to install it again. This is best done, as always, in a virtualenv, so it is isolated from the rest of your system.

Categories : Python

Where shall I put my self-written Python packages?
I'd use the home scheme for this: http://docs.python.org/2/install/#alternate-installation-the-home-scheme

Categories : Python

How to list only down-most directories in Python?
This will print out only those directories that have no subdirectories within them for dirpath, dirnames, filenames in os.walk(mydir): if not dirnames: print dirpath, "has 0 subdirectories and", len(filenames), "files"

Categories : Python

python | heroku | how to access packages over ssh
I had the same issue before I wanted to use django-avatar and the version in PyPI is old and doesn't support Django 1.5 Custom User . The simple solution is to download the package and use it as a regular app as if it was part of your project then just git add . and push it and it works ! It might not be the best idea but it just works .

Categories : Python

Python - download packages from own server
From the docs, you can use the: --use-mirrors --mirrors <url> flag in pip to specify which mirror to use. From command line, you can also speicify mirrors. For example: pip install -i http://d.pypi.python.org/simple $PACKAGE

Categories : Python

Installing python packages in nitrousio
You could try pip install --user praw https://pip.pypa.io/en/latest/user_guide.html#user-installs

Categories : Python

Installing Python Packages - IronPython
You have to run IronPython with the command line option -X:FullFrames. I'm not sure, though, how to set that up in the VS. Maybe you can manually run it: C:path oipy64.exe -X:FullFrames path opip.py install whaterver_you_want

Categories : Python

Proper permissions for python packages
It happens when sdist tarball is generated with a restrictive umask or when files were created without others/nobody read/execute permissions. A simple workaround is to broak the umask bits and chmod files before tar'ing umask 0022 && chmod -R a+rX . && python setup.py sdist upload

Categories : Python

Python: How to import sub-modules, from packages with the same name?
You need to have _init_.py files within those directories to make python treat then as a package instead of plain directories. Refer to this discussion to learn more about init.py files. Note:I have edited my previous answer by removing the irrelevant content based on the discussion with the poster of the query.

Categories : Python

How do I search online URL's and directories with python
In general you can use regular expressions to find strings that match a certain pattern, but for the example application you give this might be a little bit too heavy. For a local folder you can use the built-in string functions: for file_path in os.listdir(r"C: emp"): if file_path.startswith("w1020"): print(file_path) For a website, you might do well to search for a pre-made module. However, this tutorial gives a few examples that you can combine with the code above.

Categories : Python

Compile exe with misc directories into python
Try py2exe. Theres some more discussion here and here

Categories : Python

Python - how to match directories using fnmatch
I would just use glob: import glob print "glob", glob.glob('./The Portland*/*') There are some tricks you can play though if you really want to use os.walk for one reason or another... For example, lets assume that the top directory only contained more directories. Then you can make sure you only recurse into the correct ones by modifying the subs list in place: for root,subs,files in os.walk('.'): subs[:] = fnmatch.filter(subs,'The Portland*') for filename in files: print filename Now in this case, you will only recurse into directories which start with The Portland and then you will print all of the filenames in there. . + The Portland Show | Foo | Bar + The Portland Actors | Benny | Bernard + Other Actors | George + The LA Show | Batman I

Categories : Python

python and dynamic imports of sub directories
A good way to do dynamic imports is using imp.load_source(): import imp module = imp.load_source( 'mymodule', module_full_path ) in your case it will be something like: module = imp.load_source( 'file1', '.parsersfile1.py') methodToCall = getattr( module, 'filemethod' ) methodToCall( data ) Make sure you replace 'file1' and '.parsersfile1.py' with your desired module name and the correct path to its source file.

Categories : Python

importing system in python - import packages
import dir1 Imports dir1's __init__.py. You can access whatever is there using dir1.my_var_from_dir1_init. You cannot access the modules, only what's on dir1's __init__. from dir1 import * Imports the modules specified on the __all__ variable defined on dir1's __init__.py. If there isn't such variable, then it imports all of dir1's modules. You can access them directly, like main_code1.myvar. from dir1 import maincode Assuming it's a typo and you actually have a maincode module or class, it imports the maincode module/class from dir1. You can access it directly like mentioned above. Note that each option imports dir1's __init__.py, implicitly or explicitly. If you import the modules on __init__.py, then using import dir1 will allow you to use dir1.module. To import dir3's mainc

Categories : Python

Mixing functions and subpackages in Python packages
Solution #1: Try the following layout: lib/ __init__.py Statistic.py Transform __init__.py bins.py In this case Transform.fft goes inside lib/Transform/__init__.py and Transform.bins.extent inside lib/Transform/bins.py Solution #2: If you wish to keep __init__.py short and clean, you can also create a separate Python-module (like fft.py) and import it in __init__.py as follows: from fft import * In which case you can also use: from lib.Transform import fft

Categories : Python

using script as both library and executable in python packages
Make it a normal .py module so it can be imported, and define an entry point. Automatic script creation will take care of creating a script in bin that will call the entry point method.

Categories : Python

Calling modules from different packages in a python Project
Within the __init__.py file, put: from myfile import * for instance: pythonds/test/parens.py pythonds/test/__init__.py Where the __init__.py would contain: from parens import * That's the dirty quick-fix version for older python versions. Considering you have multiple subdirectories something along the lines of (see blow) would be better for all python versions: In your __init__.py in the folder test place: __all__ = ['tacos','falaffels'] from test.tacos import factory from test.falaffels import stand If you have the following folder structure the above should work: main.py /test/ __init__.py tacos.py falaffels.py Also just noticed that this might be a duplicate of: How do I write good/correct __init__.py files

Categories : Python

Python: I can't import a module even though it's in site-packages
I extracted the package and placed it in site-packages: No, that's the wrong way of "installing" a package. Python packages come with a setup.py script that should be used to install them. Simply do: python setup.py install And the module will be installed correctly in the site-packages of the python interpreter you are using. If you want to install it for a specific python version use python2/python3 instead of python.

Categories : Python

read and write files to different directories Python
You probably want to check the directory the file is operating within or check the current working directory: import os cur_dir= os.getcwd() top_dir = os.path.dirname(cur_dir) # perform operations in current directory # do some stuff in top directory

Categories : Python

Importing python modules from multiple directories
I can see at least one problem with your code. The line... if os.path.isdir(d): ...won't work, because os.listdir() returns relative pathnames, so you'll need to convert them to absolute pathnames, otherwise the os.path.isdir() will return False because the path doesn't exist (relative to the current working directory), rather than raising an exception (which would make more sense, IMO). The following code works for me... import sys import os # Directories to search for packages root_path_list = ("/modules", "/other_modules",) # Make a backup of sys.path old_sys_path = sys.path[:] # Add all paths to sys.path first, in case one package imports from another for root_path in root_path_list: sys.path.insert(0, root_path) # Add new packages to current scope for root_path in root_pa

Categories : Python

Using os.walk() to recursively traverse directories in Python
There are more suitable functions for this in os package. But if you have to use os.walk, here is what I come up with def walkdir(dirname): for cur, _dirs, files in os.walk(dirname): pref = '' head, tail = os.path.split(cur) while head: pref += '---' head, _tail = os.path.split(head) print(pref+tail) for f in files: print(pref+'---'+f) output: >>> walkdir('.') . ---file3 ---file2 ---my.py ---file1 ---A ------file2 ------file1 ---B ------file3 ------file2 ------file4 ------file1 ---__pycache__ ------my.cpython-33.pyc

Categories : Python

building python packages on one linux distro and running them from another
There will not be any problem if you change the way of packaging. Ubuntu needs a different type of packaging while centos needs another type. Build your package and pack it in such a way as normal centos packages and then use it in centos

Categories : Python

How to structure python packages without repeating top level name for import
Follow following structure : bagoftricks ── bagoftricks │ ├── __init__.py │ └── bagoftricks.py ├── README.md └── setup.py and then you should be able to use it as : from bagoftricks.bagoftricks import levenshtein, geofind but after you make the change in folder structure do :- pip uninstall <your package name mostly mentioned in setup.py> and reinstall the package meanwhile check your setup.py #!/bin/env python import os.path from setuptools import setup, find_packages def current_path(file_name): return os.abspath(os.path.join(__file__, os.path.pardir, file_name)) setup( name = 'bagoftricks', version = '0.1', include_package_data = True, packages=find_packages(), ) setup might have some other

Categories : Python

Organising python packages avoiding relative imports
lets say that i have this folder structure.. package1 |_ init.py |_ constants.py |_ package2 |_ init.py |_ constants.py then you can add import os import sys sys.path.append(os.path.dirname(__file__)) inside the package1/init.py then you can do from package1 import constansts as con1 from package1.package2 import constansts as con2

Categories : Python

Creating a __version__ attribute for python packages without getting into trouble
You haven't said how you get the version number into setup.py, but don't try to import it with the usual import my_package approach. You package hasn't been installed when you run setup.py! My usual approach is to define the version number in setup.py and have setup.py (re)generate the package's top level __init__.py file when the version number changes. This is easy to do if the __init__.py file contains no other code.

Categories : Python

Python 2.7 not searching folders inside of site-packages
For Linkedin authentication backend, you should import this way, from social.backends.linkedin import LinkedinOAuth for OAuth2, from social.backends.linkedin import LinkedinOAuth2

Categories : Python

Sphinx doesn't find Python packages when using autodoc
I don't know what the "absolute path to testDoc" is, but from the Sphinx output I can see that the testDoc directory structure is testDoc/t/docs. The docs directory is where conf.py is. For the module search path to be set up properly, you need to go two levels up from conf.py: sys.path.insert(0, os.path.abspath("../.."))

Categories : Python

python walk directory tree with excluding certain directories
root gives the entire path and not just the root from where you started. The docs makes it a bit more clear to what it's doing: for dirpath, dirnames, filenames in os.walk('/usr/lib'): print dirpath See the docs here

Categories : Python

How to write a portable program in python that accesses particular directories?
I would use the user's home folder and use the os.path module. Let's say you have a program named myapp and a database called db.sqlite. import os.path path = os.path.expanduser('~/.myapp/db.sqlite') path would then be expanded to /home/user/.myapp/db.sqlite on a UNIX-based system (and similar for Mac) and C:UsersuserApplication DataRoaming.myappdb.sqlite (or something similar, not on a windows machine) on a Windows machine.

Categories : Python

Python: How to choose file with a GUI but prevent browsing to other directories?
I don't think this is possible with the standard file dialogs. But you could write your own. Just use a treeview widget to display all files (and relevant information) in the directory. The User can multi select the files and you can delete them after the user dismisses the dialog.

Categories : Python

Python listing directories in Windows XP from Linux host
Windows sharing is implemented using smb protocol. Windows Explorer and most of the Linux file managers (like Nautilus) make it transparent to the user, so it is easy to do common file operations on filesfolders shared through smb. However, Linux (and thus Python that runs on top of it) does not add this abstraction by default on file system level (though you can mount smb share as part of your fs). So, in the end, to access those files you can: mount your share using mount -t cifs (man or google for details) and then access your share from Python as usual folder (to my mind this is rather kludgy solution) use library that deals specifically with smb, like pysmb (here is the relevant docs section) and do your file operations with it's help. Hope this will help.

Categories : Python

Finding the Files with the Same Name and Sub-Directories But Different Contents in Two Main Directories
You want something like this: for i in /home/Rash/A/*.{cpp,h}; do j=${i//A/B/} if [ -e $j ]; then echo comparing $i and $j diff $i $j fi done

Categories : Linux

Create new virtualenv with selected previously installed python packages
Assuming you installed most of your global packages using pip, you may be able to list all current installed packages in your global installation using this: pip freeze You can further capture this list into a text file: pip freeze > requirements.txt Edit requirements.txt to preserve only packages you want to carry forward to your new installation. Activate your new blank virtual and install your subset of packages: pip install -r requirements.txt

Categories : Python

cannot load sub-packages in a python package using `__all__` in the `__init__.py` file
That is not what __all__ does. __all__ is a list of the members of the module that will be visible when the module is imported. Using import in the __init__ files as you were doing before is probably the best way to load packages, unless you have some special reason why that wouldn't work.

Categories : Python



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.