I have run setup.py and installed, my script/package it all appears in Python27\Lib\site-packages in a folder with the relevant .egg.info file.
However, after adding site-packages to my PATH I cannot run the relevant scripts.
python: cant open file 'mypackage': [Errno 2] No such file or directory
What have I missed?
Essentially I would like to install the package, several modules are included in a folder with __init and __main files. Once installed I would like to just run
python mypackage inputfile.txt
however mypackage is not found. I will use optparse for taking in the txt files, but that is irrelevant as I can't get the packages installed.
As a rule, scripts do not go in site-packages. On the MS-windows platform they generally go in C:\Python27\Scripts\.
So on MS-Windows you probably want to add C:\Python27\;C:\Python27\Scripts\ to your $PATH.
Additionally, you have to associate the .py filetype with python.
In general, the whole chapter Using Python on Windows of the official CPython documentation should be considered required reading for users of Python on Windows.
Related
I am new to learning Python and VS Code. Tried to make portable python version from embeddable version. Deleted the python._pth file, and tried to install python get-pip.py. But nothing happens.
I did it by analogy by downloading the full installation distribution. Installed, copied the folder to the desired drive, deleted the installed one. Python starts up. But at least with the help of VS Code, at least by itself, it installs the libraries to the user's folder on the system drive along the path C:\Users\username\AppData\Roaming\Python\Python39
Naturally, when I run Python on another computer, there are no libraries installed
In addition, if I do not set two directories in Windows PATH for Python itself and scripts, then many libraries also do not work. I need to go into the scripts folder and install the same pip from there.
Maybe you need to somehow prescribe where to put it during installation? But how? Something like #ScriptDir
I also write the following lines in settings.json:
"python.defaultInterpreterPath": "f:\\portable\\winpython3-9\\python.exe",
"python.defaultInterpreterPath": "d:\\portable\\winpython3-9\\python.exe",
Then Python works. Though libraries all the same puts in the directory of the user. But VS Code writes a warning that there are duplicate lines in the settings.json file. How can I replace these lines so that I don't have to repeat them for every drive?
And how to make the libraries installed in the directory with Python?
And what other ways are there to solve the problem of working together portable VS Code + portable Python
Solved this problem. I found a portable Python here at this link.
http://winpython.github.io/
Just downloaded the installation (ziped)file. Then in VS Code I specified the path to the interpreter and everything works.
Nothing needs to be written in settings.json
I have a project in PyCharm with multiple Python files, as well as some text files, which I want to export into .exe via PyInstaller (I'm on Win 7). The project uses some external modules installed inside venv via pip (PyInstaller is one of them).
I want to keep things simple and not use any modules outside venv in the build, for obvious reasons. However, when I try to execute the PyInstaller tool on the project folder, I get a permission error: PermissionError: [Errno 13] Permission denied. Using PyInstaller on the main.py inside the project builds it just fine at first glance, but running the .exe in cmd shows an error loading Python DLL. How do I do it right? Something tells me the spec should be created beforehand to include all files and modules used in the project.
To answer the question in the title, the Arguments you specify in PyCharm are passed to pyinstaller.exe when you execute the tool. So it should be the path to main.py or the path to your .spec file, not $FilePath$ which is a macro that gets replaced by the file that is currently open in PyCharm.
You should definitely have a .spec file, that is committed to version control. This makes your PyInstaller builds more consistent, and allows others to reproduce them easily. See https://pyinstaller.readthedocs.io/en/stable/spec-files.html
I want to add a specific library path only to python2. After adding export PYTHONPATH="/path/to/lib/" to my .bashrc, however, executing python3 gets the error: Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python 3.x!
I think it is due to that python2 and python3 share the common PYTHONPATH variable.
So, can I set different PYTHONPATH variables respectively for python2 and python3. If not, how can I add a library path exclusively to a particular version of python?
PYTHONPATH is somewhat of a hack as far as package management is concerned. A "pretty" solution would be to package your library and install it.
This could sound more tricky than it is, so let me show you how it works.
Let us assume your "package" has a single file named wow.py and you keep it in /home/user/mylib/wow.py.
Create the file /home/user/mylib/setup.py with the following content:
from setuptools import setup
setup(name="WowPackage",
packages=["."],
)
That's it, now you can "properly install" your package into the Python distribution of your choice without the need to bother about PYTHONPATH. As far as "proper installation" is concerned, you have at least three options:
"Really proper". Will copy your code to your python site-packages directory:
$ python setup.py install
"Development". Will only add a link from the python site-packages to /home/user/mylib. This means that changes to code in your directory will have effect.
$ python setup.py develop
"User". If you do not want to write to the system directories, you can install the package (either "properly" or "in development mode") to /home/user/.local directory, where Python will also find them on its own. For that, just add --user to the command.
$ python setup.py install --user
$ python setup.py develop --user
To remove a package installed in development mode, do
$ python setup.py develop -u
or
$ python setup.py develop -u --user
To remove a package installed "properly", do
$ pip uninstall WowPackage
If your package is more interesting than a single file (e.g. you have subdirectories and such), just list those in the packages parameter of the setup function (you will need to list everything recursively, hence you'll use a helper function for larger libraries). Once you get a hang of it, make sure to read a more detailed manual as well.
In the end, go and contribute your package to PyPI -- it is as simple as calling python setup.py sdist register upload (you'll need a PyPI username, though).
You can create a configuration file mymodule.pth under lib/site-packages (on Windows) or lib/pythonX.Y/site-packages (on Unix and Macintosh), then add one line containing the directory to add to python path.
From docs.python2 and docs.python3:
A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.
I found that there is no way to modify PYTHONPATH that is only for python2 or only for python3. I had to use a .pth file.
What I had to do was:
make sure directory is created in my home: $HOME/.local/lib/python${MAJOR_VERSION}.${MINOR_VERSION}/site-packages
create a .pth file in that directory
test that your .pth file is work
done
For more info on `.pth. file syntax and how they work please see: python2 docs and python3 docs.
(.pth files in a nutshell: when your python interpreter starts it will look in certain directories and see the .pth file, open those files, parse the files, and add those directories to your sys.path (i.e. the same behavior as PYTHONPATH) and make any python modules located on those directories available for normal importing.)
If you don't want to bother with moving/adding documents in lib/site-packages, try adding two lines of code in the python2.7 script you would like to run (below.)
import sys
sys.path = [p for p in sys.path if p.startswith(r'C:\Python27')]
This way, PYTHONPATH will be updated (ignore all python3.x packages) every time you run your code.
I want to add Python as a scripting language to my game. If instead of distributing PY script, I distribute the PYC compiled files with my game, will the user still need Python installed, or will the DLL be sufficient?
Not only would they still need the Python interpreter, but the compiled python byte-code files are not guaranteed to work across different platforms.
You do need an executable to actually load the files into the VM. Fortunately it doesn't need to be very complex.
pyinstaller can be used to convert the .py file into an executable file
to install the pyinstaller
pip install pyinstaller
and to convert the .py file let's say file.py
pyinstaller file.py
this will make two new folders in the same directory
build and dist. dist folder contains all the required dll and file.exe to run the python code without python installed.
I am new to Python and mostly used my own code. But so now I downloaded a package that I need for some problem I have.
Example structure:
root\
externals\
__init__.py
cowfactory\
__init__.py
cow.py
milk.py
kittens.py
Now the cowfactory's __init__.py does from cowfactory import cow. This gives an import error.
I could fix it and change the import statement to from externals.cowfactory import cow but something tells me that there is an easier way since it's not very practical.
An other fix could be to put the cowfactory package in the root of my project but that's not very tidy either.
I think I have to do something with the __init__.py file in the externals directory but I am not sure what.
Inside the cowfactory package, relative imports should be used such as from . import cow. The __init__.py file in externals is not necessary. Assuming that your project lies in root\ and cowfactory is the external package you downloaded, you can do it in two different ways:
Install the external module
External Python packages usually come with a file "setup.py" that allows you to install it. On Windows, it would be the command "setup.py bdist_wininst" and you get a EXE installer in the "dist" directory (if it builds correctly). Use that installer and the package will be installed in the Python installation directory. Afterwards, you can simply do an import cowfactory just like you would do import os.
If you have pip or easy_install installed: Many external packages can be installed with them (pip even allows easy uninstallation).
Use PYTHONPATH for development
If you want to keep all dependencies together in your project directory, then keep all external packages in the externals\ folder and add the folder to the PYTHONPATH. If you're using the command line, you can create a batch file containing something like
set PYTHONPATH=%PYTHONPATH%:externals
yourprogram.py
I'm actually doing something similar, but using PyDev+Eclipse. There, you can change the "Run configurations" to include the environment variable PYTHONPATH with the value "externals". After the environment variable is set, you can simply import cowfactory in your own modules. Note how that is better than from external import cowfactory because in the latter case, it wouldn't work anymore once you install your project (or you'd have to install all external dependencies as a package called "external" which is a bad idea).
Same solutions of course apply to Linux, as well, but with different commands.
generally, you would use easy_install our pip to install it for you in the appropriate directory. There is a site-packages directory on windows where you can put the package if you can't use easy_install for some reason. On ubuntu, it's /usr/lib/pythonX.Y/dist-packages. Google for your particular system. Or you can put it anywhere on your PYTHONPATH environment variable.
As a general rule, it's good to not put third party libs in your programs directory structure (although there are differing opinions on this vis a vis source control). This keeps your directory structure as minimalist as possible.
The easiest way is to use the enviroment variable $PYTHONPATH. You set it before running your scripts as follows:
export $PYTHONPATH=$PYTHONPATH:/root/externals/
You can add as many folders as you want (provided their separate by :) and python will look in all those folders when importing.